Something went wrong. Try again.
A local-first event pipeline for independent agents, built on Jazz.
Something went wrong. Try again.
4.7 kB · 107 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import path from "node:path";import type { AgentOutput, AgentRunInput, AgentRunner } from "./types.js";
export class DeterministicTriageRunner implements AgentRunner { readonly mode = "deterministic" as const;
async run(input: AgentRunInput, onTrace: (trace: { kind: string; data: unknown }) => Promise<void>): Promise<AgentOutput> { if (input.event.type === "stream.thought.source.atproto.commit") { return classifyAtprotoCommit(input, onTrace); } const payload = input.event.payload; const documentPath = typeof payload.path === "string" ? payload.path : "unknown"; const extension = path.extname(documentPath).slice(1) || "document"; const tags = ["filesystem", extension]; if (/(^|\/)spec(s)?\//i.test(documentPath)) tags.push("specification"); if (/(^|\/)(agent|agents)\//i.test(documentPath)) tags.push("agent-definition"); const importance = tags.includes("specification") || tags.includes("agent-definition") ? "high" : "normal"; await onTrace({ kind: "deterministic.classification", data: { documentPath, tags, importance, eventType: input.event.type }, }); return { summary: `${humanizeEvent(input.event.type)}: ${documentPath}`, tags, importance, confidence: 1, ...(tags.includes("specification") ? { recommendation: { target: "charter", reason: `A specification document changed at ${documentPath}`, proposedAction: "Re-evaluate Charter work affected by this specification change; do not edit files automatically.", }, } : {}), }; }}
async function classifyAtprotoCommit( input: AgentRunInput, onTrace: (trace: { kind: string; data: unknown }) => Promise<void>,): Promise<AgentOutput> { const payload = input.event.payload; const operation = stringField(payload.operation, "observed"); const collection = stringField(payload.collection, "unknown-collection"); const atUri = stringField(payload.atUri, input.event.externalId); const record = objectField(payload.record); const text = record && typeof record.text === "string" ? compactText(record.text) : undefined; const subject = record ? objectField(record.subject) : undefined; const subjectUri = subject && typeof subject.uri === "string" ? subject.uri : undefined; const tags = ["atproto", collection, operation]; if (collection === "app.bsky.feed.post") tags.push("post"); if (collection === "app.bsky.feed.like") tags.push("like"); if (record && typeof record.reply === "object") tags.push("reply"); if (record && Array.isArray(record.facets) && record.facets.length > 0) tags.push("facets"); if (record && typeof record.embed === "object") tags.push("embed"); await onTrace({ kind: "deterministic.atproto-classification", data: { operation, collection, atUri, tags, textChars: text?.length ?? 0, ...(subjectUri ? { subjectUri } : {}) }, }); return { summary: atprotoSummary(operation, collection, atUri, text, subjectUri), tags, importance: "normal", confidence: 1, };}
function atprotoSummary(operation: string, collection: string, atUri: string, text?: string, subjectUri?: string): string { const noun = collection === "app.bsky.feed.post" ? "post" : collection === "app.bsky.feed.like" ? "like" : collection; if (text) return `${capitalize(operation)} ${noun}: ${truncate(text, 220)}`; if (subjectUri) return `${capitalize(operation)} ${noun}: ${atUriToWebUrl(subjectUri)}`; return `${capitalize(operation)} ${noun} at ${atUri}`;}
function atUriToWebUrl(value: string): string { const match = value.match(/^at:\/\/([^/]+)\/app\.bsky\.feed\.post\/([^/]+)$/); return match ? `https://bsky.app/profile/${match[1]}/post/${match[2]}` : value;}
function compactText(value: string): string { return value.replaceAll(/\s+/g, " ").trim();}
function truncate(value: string, limit: number): string { return value.length <= limit ? value : `${value.slice(0, limit - 1).trimEnd()}…`;}
function stringField(value: unknown, fallback: string): string { return typeof value === "string" && value.length > 0 ? value : fallback;}
function objectField(value: unknown): Record<string, unknown> | undefined { return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;}
function capitalize(value: string): string { return value.charAt(0).toUpperCase() + value.slice(1);}
function humanizeEvent(type: string): string { const action = type.split(".").at(-1) ?? "observed"; return action.charAt(0).toUpperCase() + action.slice(1);}