mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-14 21:35:25 +00:00
36 lines
963 B
TypeScript
36 lines
963 B
TypeScript
'use client';
|
|
|
|
import { useMemo } from "react";
|
|
import { FileReference } from "./types";
|
|
import { FILE_REFERENCE_REGEX } from "./constants";
|
|
import { createFileReference } from "./utils";
|
|
import { TextUIPart } from "ai";
|
|
|
|
export const useExtractReferences = (part?: TextUIPart) => {
|
|
return useMemo(() => {
|
|
if (!part) {
|
|
return [];
|
|
}
|
|
|
|
const references: FileReference[] = [];
|
|
|
|
const content = part.text;
|
|
FILE_REFERENCE_REGEX.lastIndex = 0;
|
|
|
|
let match;
|
|
while ((match = FILE_REFERENCE_REGEX.exec(content ?? '')) !== null && match !== null) {
|
|
const [_, repo, fileName, startLine, endLine] = match;
|
|
|
|
const fileReference = createFileReference({
|
|
repo: repo,
|
|
path: fileName,
|
|
startLine,
|
|
endLine,
|
|
});
|
|
|
|
references.push(fileReference);
|
|
}
|
|
|
|
return references;
|
|
}, [part]);
|
|
};
|