Resolve Face artifact links before posting
This commit is contained in:
parent
e99a9d53ce
commit
003a4dff45
7 changed files with 172 additions and 8 deletions
|
|
@ -56,6 +56,7 @@ import {
|
|||
resolveArticleRepoRoot,
|
||||
validateRenderedArticleMarkdown,
|
||||
} from "./repo-face-article.js";
|
||||
import { resolveRepoIdentityPostArtifactLinks } from "./repo-face-artifact-links.js";
|
||||
|
||||
const config = loadConfig();
|
||||
const REPO_IDENTITY_POST_SENTINEL = "VOIDBOT_REPO_IDENTITY_POST:";
|
||||
|
|
@ -1168,8 +1169,13 @@ async function postRepoIdentityIntent(job: JobRecord, intent: RepoIdentityPostIn
|
|||
`Coerced repo identity ${identity.id} speech for job ${job.id} from "${requestedChannelId}" to conversation channel ${channelId}.`,
|
||||
);
|
||||
}
|
||||
const sourceDocuments = await sourceArchiveRepository.listByRepo(identity.repoName).catch(() => []);
|
||||
const content = resolveRepoIdentityPostArtifactLinks({
|
||||
repoName: identity.repoName,
|
||||
content: intent.content.trim(),
|
||||
documents: sourceDocuments,
|
||||
});
|
||||
if (isOwnerDmChannelAlias(channelId)) {
|
||||
const content = intent.content.trim();
|
||||
const dmChannelId = await openOwnerDmChannel();
|
||||
const postedMessages = await postDiscordBotMessageChunks(
|
||||
dmChannelId,
|
||||
|
|
@ -1202,7 +1208,6 @@ async function postRepoIdentityIntent(job: JobRecord, intent: RepoIdentityPostIn
|
|||
return false;
|
||||
}
|
||||
|
||||
const content = intent.content.trim();
|
||||
if (content.length > 1900) {
|
||||
throw new Error(
|
||||
`Repo identity ${identity.id} SAY content is too long for one Discord message (${content.length} characters).`,
|
||||
|
|
|
|||
105
apps/worker/src/repo-face-artifact-links.ts
Normal file
105
apps/worker/src/repo-face-artifact-links.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import type { ArchivedSourceDocumentRecord } from "@voidbot/rag";
|
||||
|
||||
const KNOWLEDGEBASE_URL_RESOLVERS = new Map<string, { baseUrl: string; stripPrefix?: string }>([
|
||||
["aetherialore", { baseUrl: "https://aetheria.gamecult.org", stripPrefix: "Aetheria/" }],
|
||||
]);
|
||||
|
||||
export function resolveRepoIdentityPostArtifactLinks(input: {
|
||||
repoName: string;
|
||||
content: string;
|
||||
documents?: ArchivedSourceDocumentRecord[];
|
||||
}): string {
|
||||
const explicit = resolveExplicitKnowledgebaseReferences(input.content, input.repoName);
|
||||
if (explicit !== input.content || /\bhttps?:\/\//i.test(explicit)) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
if (!knowledgebaseResolverForRepo(input.repoName)) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
const referenced = findMentionedKnowledgebaseDocument(explicit, input.documents ?? []);
|
||||
if (!referenced) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
const url = resolveKnowledgebaseUrl(referenced.repoName, referenced.path);
|
||||
if (!url || explicit.includes(url)) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
return `${explicit}\n\nReference: ${url}`;
|
||||
}
|
||||
|
||||
export function resolveKnowledgebaseUrl(repoName: string, sourcePath: string): string | undefined {
|
||||
const resolver = knowledgebaseResolverForRepo(repoName);
|
||||
if (!resolver) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let normalizedPath = sourcePath.trim().replace(/\\/g, "/").replace(/^\/+/, "");
|
||||
if (resolver.stripPrefix && normalizedPath.toLowerCase().startsWith(resolver.stripPrefix.toLowerCase())) {
|
||||
normalizedPath = normalizedPath.slice(resolver.stripPrefix.length);
|
||||
}
|
||||
normalizedPath = normalizedPath.replace(/\.md$/i, "");
|
||||
const publishedPath = normalizedPath
|
||||
.split("/")
|
||||
.filter((segment) => segment.length > 0)
|
||||
.map((segment) => encodeURIComponent(segment.trim().replace(/\s+/g, "-")))
|
||||
.join("/");
|
||||
|
||||
return publishedPath ? `${resolver.baseUrl}/${publishedPath}` : resolver.baseUrl;
|
||||
}
|
||||
|
||||
function resolveExplicitKnowledgebaseReferences(content: string, defaultRepoName: string): string {
|
||||
return content.replace(/`([^`\n]+?\.md)`/g, (match, rawReference: string) => {
|
||||
const resolved = resolveKnowledgebaseReference(rawReference, defaultRepoName);
|
||||
return resolved ? `${match} (${resolved})` : match;
|
||||
});
|
||||
}
|
||||
|
||||
function resolveKnowledgebaseReference(reference: string, defaultRepoName: string): string | undefined {
|
||||
const trimmed = reference.trim();
|
||||
const separatorIndex = trimmed.indexOf(":");
|
||||
const repoName = separatorIndex > 0 ? trimmed.slice(0, separatorIndex).trim() : defaultRepoName;
|
||||
const path = separatorIndex > 0 ? trimmed.slice(separatorIndex + 1).trim() : trimmed;
|
||||
return resolveKnowledgebaseUrl(repoName, path);
|
||||
}
|
||||
|
||||
function findMentionedKnowledgebaseDocument(
|
||||
content: string,
|
||||
documents: ArchivedSourceDocumentRecord[],
|
||||
): ArchivedSourceDocumentRecord | undefined {
|
||||
const normalizedContent = normalizeReferenceText(content);
|
||||
const markdownDocuments = documents
|
||||
.filter((document) => document.path.toLowerCase().endsWith(".md"))
|
||||
.map((document) => ({
|
||||
document,
|
||||
label: document.title?.trim() || basenameWithoutExtension(document.path),
|
||||
}))
|
||||
.filter((entry) => entry.label.length >= 10)
|
||||
.sort((left, right) => right.label.length - left.label.length);
|
||||
|
||||
return markdownDocuments.find((entry) =>
|
||||
normalizedContent.includes(normalizeReferenceText(entry.label))
|
||||
)?.document;
|
||||
}
|
||||
|
||||
function knowledgebaseResolverForRepo(repoName: string): { baseUrl: string; stripPrefix?: string } | undefined {
|
||||
return KNOWLEDGEBASE_URL_RESOLVERS.get(repoName.trim().toLowerCase());
|
||||
}
|
||||
|
||||
function normalizeReferenceText(value: string): string {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[`*_~()[\]{}<>]/g, " ")
|
||||
.replace(/[-_/\\.:]+/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function basenameWithoutExtension(path: string): string {
|
||||
const normalized = path.replace(/\\/g, "/");
|
||||
const segment = normalized.split("/").filter(Boolean).at(-1) ?? normalized;
|
||||
return segment.replace(/\.[^.]+$/, "");
|
||||
}
|
||||
Loading…
Reference in a new issue