π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { WidgetManifest, WidgetOutput } from '@lobechat/types';
import { and, eq, sql } from 'drizzle-orm';
import { MetricModel } from '@/database/models/metric';
import { metrics, widgetRuns, widgetVersions } from '@/database/schemas';
import type { LobeChatDatabase } from '@/database/type';
export interface WidgetMetricScope {
id: string;
metricId: string | null;
title: string;
userId: string;
workspaceId: string | null;
}
export interface RecordWidgetMetricsParams {
manifest?: WidgetManifest | null;
observedAt: Date;
output: WidgetOutput;
runId: string;
}
export interface RecordWidgetMetricsResult {
pointsWritten: number;
/** Series the widget row should point at (`widgets.metric_id`). */
primaryMetricId?: string;
}
const DEFAULT_STAT_KEY = 'value';
const SERIES_KEY_PREFIX = 'series:';
const toNumber = (value: unknown): number | undefined => {
if (typeof value === 'number') return Number.isFinite(value) ? value : undefined;
if (typeof value === 'string' && value.trim() !== '') {
const n = Number(value.replaceAll(',', ''));
return Number.isFinite(n) ? n : undefined;
}
return undefined;
};
const pickPath = (source: unknown, path: string): unknown =>
path
.split('.')
.reduce<unknown>(
(value, key) =>
value && typeof value === 'object' ? (value as Record<string, unknown>)[key] : undefined,
source,
);
const parseTime = (t: string): Date | undefined => {
// Category labels ('Mon', 'v1.2') are not time points and never enter a trend.
if (!/^\d{4}-\d{2}-\d{2}/.test(t)) return undefined;
const date = new Date(t);
return Number.isNaN(date.getTime()) ? undefined : date;
};
/**
* Fold a successful, complete run into the widget's long-term trend in
* `metrics` / `metric_points` (subject `widget`):
*
* - `stat` β one point per run, the numeric `value` (or `manifest.metric.valuePath`)
* observed at the run's finish time. Non-numeric stats are skipped.
* - `data` β like `stat`, but only when `manifest.metric.valuePath` names the
* number to trend (e.g. `data.current.temp`).
* - `series` β one metric per series (`series:<name>`); only points with an
* ISO timestamp newer than the series' latest stored point are appended
* (serialized per series), so a script re-reporting a sliding window β even
* from two overlapping runs β does not duplicate history.
*
* list / table outputs carry no numbers to trend and write nothing.
*/
export const recordWidgetMetrics = async (
db: LobeChatDatabase,
widget: WidgetMetricScope,
params: RecordWidgetMetricsParams,
): Promise<RecordWidgetMetricsResult> => {
const { manifest, output, observedAt, runId } = params;
const metricModel = new MetricModel(db, widget.userId, widget.workspaceId ?? undefined);
const [version] = await db
.select({ contentHash: widgetVersions.contentHash })
.from(widgetRuns)
.innerJoin(widgetVersions, eq(widgetVersions.id, widgetRuns.versionId))
.where(eq(widgetRuns.id, runId));
if (!version) throw new Error('Metric run has no persisted version');
const ensure = async (params: Parameters<MetricModel['ensure']>[0]) => {
const metric = await metricModel.ensure({
...params,
metadata: { widgetContentHash: version.contentHash },
});
if (!metric) throw new Error('Metric scope unavailable');
// Legacy series may be adopted only while empty. A changed script/account/
// extraction must use a new key, even after explicitly rebinding a Goal.
const [confirmed] = await db
.update(metrics)
.set({ metadata: { widgetContentHash: version.contentHash } })
.where(
and(
eq(metrics.id, metric.id),
sql`(
${metrics.metadata}->>'widgetContentHash' = ${version.contentHash}
OR (${metrics.metadata} IS NULL AND NOT EXISTS (SELECT 1 FROM metric_points WHERE metric_id = ${metric.id}))
)`,
),
)
.returning();
if (!confirmed) throw new Error('Widget metric source changed: publish a new metric key');
return confirmed;
};
const kind = manifest?.metric?.kind ?? 'gauge';
const pointBase = {
actorId: runId,
actorType: 'system' as const,
sourceType: 'probe' as const,
};
if (output.type === 'stat' || output.type === 'data') {
const valuePath = manifest?.metric?.valuePath;
// A data output has no headline: it trends only the number valuePath names.
if (output.type === 'data' && !valuePath) return { pointsWritten: 0 };
const value = toNumber(
valuePath ? pickPath(output, valuePath) : output.type === 'stat' ? output.value : undefined,
);
if (value === undefined) return { pointsWritten: 0 };
const metric = await ensure({
key: manifest?.metric?.key ?? DEFAULT_STAT_KEY,
kind,
subjectId: widget.id,
subjectType: 'widget',
title: (output.type === 'stat' ? output.label : undefined) ?? widget.title,
unit: manifest?.metric?.unit ?? (output.type === 'stat' ? output.unit : undefined),
});
if (!metric) return { pointsWritten: 0 };
await metricModel.addPoint(metric.id, { ...pointBase, observedAt, value });
return { pointsWritten: 1, primaryMetricId: metric.id };
}
if (output.type === 'series') {
let pointsWritten = 0;
let primaryMetricId: string | undefined;
for (const series of output.series) {
const points = series.points
.map((p) => ({ observedAt: parseTime(p.t), value: p.v }))
.filter(
(p): p is { observedAt: Date; value: number } =>
!!p.observedAt && Number.isFinite(p.value),
);
if (points.length === 0) continue;
const metric = await ensure({
key: `${SERIES_KEY_PREFIX}${series.name}`,
kind,
subjectId: widget.id,
subjectType: 'widget',
title: series.name,
unit: manifest?.metric?.unit ?? output.unit,
});
if (!metric) continue;
primaryMetricId ??= metric.id;
// Overlapping runs report the same window; the append is serialized
// per series so each timestamp is written once.
pointsWritten += await metricModel.appendNewerPoints(
metric.id,
points.map((p) => ({ ...pointBase, ...p })),
);
}
return { pointsWritten, primaryMetricId };
}
return { pointsWritten: 0 };
};