π€― 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 { MetricSubjectType } from '@lobechat/types';
import { METRIC_SUBJECT_TYPES } from '@lobechat/types';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';
import { AgentModel } from '@/database/models/agent';
import { GoalModel } from '@/database/models/goal';
import { MetricModel } from '@/database/models/metric';
import { ProjectModel } from '@/database/models/project';
import { TaskModel } from '@/database/models/task';
import { WidgetModel } from '@/database/models/widget';
import type { LobeChatDatabase } from '@/database/type';
import { router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
import { GoalSubscriptionService } from '@/server/services/goal/subscriptions';
import { assertWorkspaceRowManageable } from './_helpers/assertWorkspaceRowManageable';
const metricProcedure = wsCompatProcedure.use(serverDatabase).use(async (opts) =>
opts.next({
ctx: {
metricModel: new MetricModel(
opts.ctx.serverDB,
opts.ctx.userId,
opts.ctx.workspaceId ?? undefined,
),
},
}),
);
const metricWriteProcedure = metricProcedure.use(withScopedPermission('agent:update'));
const idInput = z.object({ id: z.string() });
const subjectInput = z.object({
subjectId: z.string(),
subjectType: z.enum(METRIC_SUBJECT_TYPES),
});
const configSchema = z.object({
direction: z.enum(['higher_is_better', 'lower_is_better']).optional(),
// Values persist in numeric(20, 6); a higher declared precision would be
// a promise the column silently rounds away.
precision: z.number().int().min(0).max(6).optional(),
sampleIntervalHint: z.string().optional(),
target: z.number().optional(),
});
const definitionFields = {
config: configSchema.optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
title: z.string().optional(),
unit: z.string().optional(),
};
// These columns are nullable, so a patch must be able to clear them β dropping
// a target back to "no target" is an edit, not an omission. Omitted still
// means "leave as is"; explicit null means "unset".
const patchableDefinitionFields = {
config: configSchema.nullable().optional(),
metadata: z.record(z.string(), z.unknown()).nullable().optional(),
title: z.string().nullable().optional(),
unit: z.string().nullable().optional(),
};
function mapMetricError(error: unknown, operation: string): never {
if (error instanceof TRPCError) throw error;
console.error(`[metric:${operation}]`, error);
throw new TRPCError({
cause: error,
code: 'INTERNAL_SERVER_ERROR',
message: `Failed to ${operation} metric`,
});
}
const notFound = () => new TRPCError({ code: 'NOT_FOUND', message: 'Metric series not found' });
/**
* A metric is telemetry *about* its subject, so it is exactly as visible as
* that subject β the invariant every path here enforces.
*
* It has to be enforced here because the link is polymorphic with no FK and
* `metrics` carries no `visibility` column of its own: in workspace mode
* `buildWorkspaceWhere` degrades to a bare `workspace_id` match, which would
* otherwise let any member read (or write) the series of a coworker's private
* agent, task or project. On the write side it additionally stops a caller
* minting series against arbitrary ids β durable dangling rows, and because
* (subject_type, subject_id, key) is globally unique, a reserved slot the
* legitimate owner could never claim.
*/
const assertSubjectVisible = async (
db: LobeChatDatabase,
ctx: { userId: string; workspaceId?: string | null },
subjectType: MetricSubjectType,
subjectId: string,
): Promise<void> => {
const workspaceId = ctx.workspaceId ?? undefined;
const visible = await (() => {
switch (subjectType) {
case 'agent': {
return new AgentModel(db, ctx.userId, workspaceId).existsById(subjectId);
}
case 'goal': {
return new GoalModel(db, ctx.userId, workspaceId).findById(subjectId);
}
case 'project': {
return new ProjectModel(db, ctx.userId, workspaceId).findById(subjectId);
}
case 'task': {
return new TaskModel(db, ctx.userId, workspaceId).findById(subjectId);
}
case 'widget': {
return new WidgetModel(db, ctx.userId, workspaceId).findById(subjectId);
}
case 'workspace': {
return workspaceId === subjectId;
}
}
})();
if (!visible)
throw new TRPCError({ code: 'NOT_FOUND', message: `Metric subject not found: ${subjectType}` });
};
/**
* Load a series for a by-id path and re-check its subject: the series row
* alone proves only workspace membership, not that the caller may see what it
* measures. Doubles as orphan protection β a series whose subject was deleted
* stops resolving instead of lingering as readable, writable telemetry.
*/
/**
* Widget series are written only by the widget's own runs (see
* `services/widget/metrics`), which own the fixed `value` / `series:<name>`
* keys. Letting the generic API create, append to, edit or delete them would
* let any workspace member pre-claim those keys or rewrite a coworker's trend,
* since series ownership here widens to the whole workspace. Reads stay open.
*/
const assertSubjectWritable = (subjectType: MetricSubjectType) => {
if (subjectType === 'widget')
throw new TRPCError({
code: 'FORBIDDEN',
message: "Widget metrics are written by the widget's runs, not through the metric API",
});
};
const requireVisibleSeries = async (
db: LobeChatDatabase,
ctx: { metricModel: MetricModel; userId: string; workspaceId?: string | null },
id: string,
) => {
const series = await ctx.metricModel.findById(id);
if (!series) throw notFound();
await assertSubjectVisible(db, ctx, series.subjectType, series.subjectId);
return series;
};
export const metricRouter = router({
/**
* Append one observation. Actor attribution is server-set β a TRPC caller is
* always the authenticated user; probe runs write through their own service
* path with `system` attribution.
*/
addPoint: metricWriteProcedure
.input(
idInput.extend({
metadata: z.record(z.string(), z.unknown()).optional(),
observedAt: z.coerce.date().optional(),
sourceType: z.enum(['manual', 'api']).default('manual'),
value: z.number(),
}),
)
.mutation(async ({ input, ctx }) => {
try {
const series = await requireVisibleSeries(ctx.serverDB, ctx, input.id);
assertSubjectWritable(series.subjectType);
const point = await ctx.metricModel.addPoint(input.id, {
actorId: ctx.userId,
actorType: 'user',
metadata: input.metadata,
observedAt: input.observedAt ?? new Date(),
sourceType: input.sourceType,
value: input.value,
});
if (!point) throw notFound();
return { data: point, message: 'Point recorded', success: true };
} catch (error) {
mapMetricError(error, 'addPoint');
}
}),
deleteSeries: metricWriteProcedure.input(idInput).mutation(async ({ input, ctx }) => {
try {
const series = await requireVisibleSeries(ctx.serverDB, ctx, input.id);
assertSubjectWritable(series.subjectType);
// Workspace visibility lets any member read the series; deleting it (and
// cascading every observation) stays with the creator or an owner.
assertWorkspaceRowManageable(ctx, series.userId, 'metric series');
const deleted = await ctx.metricModel.delete(input.id);
if (!deleted) throw notFound();
return { message: 'Series deleted', success: true };
} catch (error) {
mapMetricError(error, 'deleteSeries');
}
}),
/** Series definition plus its latest observation β the "current value" read. */
getSeries: metricProcedure.input(idInput).query(async ({ input, ctx }) => {
try {
const series = await requireVisibleSeries(ctx.serverDB, ctx, input.id);
const latest = await ctx.metricModel.latestPoint(series.id);
return { data: { ...series, latestPoint: latest ?? null }, success: true };
} catch (error) {
mapMetricError(error, 'getSeries');
}
}),
/**
* The chart read: raw or bucket-aggregated points together with the render
* contract (kind / unit / config / title), so one call feeds a chart.
*/
listPoints: metricProcedure
.input(
idInput.extend({
bucket: z.enum(['hour', 'day', 'week', 'month']).optional(),
from: z.coerce.date().optional(),
limit: z.number().int().min(1).max(10_000).optional(),
to: z.coerce.date().optional(),
}),
)
.query(async ({ input, ctx }) => {
try {
await requireVisibleSeries(ctx.serverDB, ctx, input.id);
const result = await ctx.metricModel.listPoints(input.id, {
bucket: input.bucket,
from: input.from,
limit: input.limit,
to: input.to,
});
if (!result) throw notFound();
const { points, series } = result;
return {
data: {
config: series.config,
kind: series.kind,
points,
title: series.title,
unit: series.unit,
},
success: true,
};
} catch (error) {
mapMetricError(error, 'listPoints');
}
}),
/**
* The chart bundle for a tracking surface, in one RPC: the named series of a
* subject, each with its recent point window and its true first observation.
*
* Exists because the per-series alternative amplifies: a polled surface
* calling listSeries + N x listPoints costs 1+N requests and ~3N queries
* for series that cannot even affect the view. Here the caller names its
* keys (a declared acceptance contract is capped), and the whole read is
* one request over K+2 bounded queries. `firstPoint` travels separately
* because the recent window silently rebases a progress baseline once
* history outgrows it.
*/
listSeriesWithPoints: metricProcedure
.input(
subjectInput.extend({
keys: z.array(z.string().min(1).max(255)).min(1).max(50),
limit: z.number().int().min(2).max(1000).optional(),
}),
)
.query(async ({ input, ctx }) => {
try {
await assertSubjectVisible(ctx.serverDB, ctx, input.subjectType, input.subjectId);
const series = await ctx.metricModel.findByKeys(
input.subjectType,
input.subjectId,
input.keys,
);
// Tracking Goals use the exact Widget series and history read by boards.
// Alias only the contract key; identity and values remain authoritative.
const bindings =
input.subjectType === 'goal'
? await new GoalSubscriptionService(
ctx.serverDB,
ctx.userId,
ctx.workspaceId ?? undefined,
).list(input.subjectId)
: [];
const mapped = input.keys.flatMap((key) => {
const binding = bindings.find((item) => item.subscription.binding.criterionKey === key);
if (binding) return binding.metric ? [{ ...binding.metric, key }] : [];
const own = series.find((item) => item.key === key);
return own ? [own] : [];
});
const firstPoints = await ctx.metricModel.firstPointsByMetricIds(
mapped.map((item) => item.id),
);
const data = await Promise.all(
mapped.map(async (item) => ({
config: item.config,
firstPoint: firstPoints.get(item.id) ?? null,
id: item.id,
key: item.key,
kind: item.kind,
points: await ctx.metricModel.recentPoints(item.id, input.limit ?? 200),
title: item.title,
unit: item.unit,
})),
);
return { data, success: true };
} catch (error) {
mapMetricError(error, 'listSeriesWithPoints');
}
}),
listSeries: metricProcedure.input(subjectInput).query(async ({ input, ctx }) => {
try {
await assertSubjectVisible(ctx.serverDB, ctx, input.subjectType, input.subjectId);
const data = await ctx.metricModel.findBySubject(input.subjectType, input.subjectId);
return { data, success: true };
} catch (error) {
mapMetricError(error, 'listSeries');
}
}),
updateSeries: metricWriteProcedure
.input(
idInput.extend({
...patchableDefinitionFields,
kind: z.enum(['gauge', 'counter']).optional(),
}),
)
.mutation(async ({ input, ctx }) => {
try {
const { id, ...patch } = input;
const existing = await requireVisibleSeries(ctx.serverDB, ctx, id);
assertSubjectWritable(existing.subjectType);
// Definition edits rewrite the render/evaluation contract for everyone
// reading the series β creator or workspace owner only.
assertWorkspaceRowManageable(ctx, existing.userId, 'metric series');
const series = await ctx.metricModel.update(id, patch);
if (!series) throw notFound();
return { data: series, message: 'Series updated', success: true };
} catch (error) {
mapMetricError(error, 'updateSeries');
}
}),
/** Idempotent create β existing definition fields are never overwritten. */
upsertSeries: metricWriteProcedure
.input(
subjectInput.extend({
...definitionFields,
key: z.string().min(1).max(255),
kind: z.enum(['gauge', 'counter']).optional(),
}),
)
.mutation(async ({ input, ctx }) => {
try {
assertSubjectWritable(input.subjectType);
await assertSubjectVisible(ctx.serverDB, ctx, input.subjectType, input.subjectId);
const series = await ctx.metricModel.ensure(input);
if (!series)
throw new TRPCError({
code: 'CONFLICT',
message: 'Metric series slot is owned by another scope',
});
return { data: series, message: 'Series ready', success: true };
} catch (error) {
mapMetricError(error, 'upsertSeries');
}
}),
});