Merge branch 'fix/perf-q09' into fix/perf-omo-in-tree
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { existsSync } from "node:fs";
|
import { access } from "node:fs/promises";
|
||||||
import { dirname, isAbsolute, join, resolve } from "node:path";
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
||||||
|
|
||||||
import { README_FILENAME } from "./constants";
|
import { README_FILENAME } from "./constants";
|
||||||
@@ -9,17 +9,19 @@ export function resolveFilePath(rootDirectory: string, path: string): string | n
|
|||||||
return resolve(rootDirectory, path);
|
return resolve(rootDirectory, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findReadmeMdUp(input: {
|
export async function findReadmeMdUp(input: {
|
||||||
startDir: string;
|
startDir: string;
|
||||||
rootDir: string;
|
rootDir: string;
|
||||||
}): string[] {
|
}): Promise<string[]> {
|
||||||
const found: string[] = [];
|
const found: string[] = [];
|
||||||
let current = input.startDir;
|
let current = input.startDir;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const readmePath = join(current, README_FILENAME);
|
const readmePath = join(current, README_FILENAME);
|
||||||
if (existsSync(readmePath)) {
|
try {
|
||||||
|
await access(readmePath);
|
||||||
found.push(readmePath);
|
found.push(readmePath);
|
||||||
|
} catch {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current === input.rootDir) break;
|
if (current === input.rootDir) break;
|
||||||
|
|||||||
@@ -133,6 +133,32 @@ describe("processFilePathForReadmeInjection", () => {
|
|||||||
expect(output.output).toContain("# Components README")
|
expect(output.output).toContain("# Components README")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("returns a promise and finds README.md files from temp fixtures", async () => {
|
||||||
|
// given
|
||||||
|
const sourceDirectory = join(testRoot, "src")
|
||||||
|
const componentsDirectory = join(sourceDirectory, "components")
|
||||||
|
mkdirSync(componentsDirectory, { recursive: true })
|
||||||
|
writeFileSync(join(testRoot, "README.md"), "# Root README")
|
||||||
|
writeFileSync(join(sourceDirectory, "README.md"), "# Src README")
|
||||||
|
writeFileSync(join(componentsDirectory, "README.md"), "# Components README")
|
||||||
|
|
||||||
|
const { findReadmeMdUp } = await import("./finder")
|
||||||
|
|
||||||
|
// when
|
||||||
|
const promise = findReadmeMdUp({
|
||||||
|
startDir: componentsDirectory,
|
||||||
|
rootDir: testRoot,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(promise).toBeInstanceOf(Promise)
|
||||||
|
await expect(promise).resolves.toEqual([
|
||||||
|
join(testRoot, "README.md"),
|
||||||
|
join(sourceDirectory, "README.md"),
|
||||||
|
join(componentsDirectory, "README.md"),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
it("does not re-inject already cached directories", async () => {
|
it("does not re-inject already cached directories", async () => {
|
||||||
// given
|
// given
|
||||||
const sourceDirectory = join(testRoot, "src")
|
const sourceDirectory = join(testRoot, "src")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin";
|
import type { PluginInput } from "@opencode-ai/plugin";
|
||||||
import { readFileSync } from "node:fs";
|
import { readFile } from "node:fs/promises";
|
||||||
import { dirname } from "node:path";
|
import { dirname } from "node:path";
|
||||||
|
|
||||||
import type { createDynamicTruncator } from "../../shared/dynamic-truncator";
|
import type { createDynamicTruncator } from "../../shared/dynamic-truncator";
|
||||||
@@ -31,7 +31,7 @@ export async function processFilePathForReadmeInjection(input: {
|
|||||||
|
|
||||||
const dir = dirname(resolved);
|
const dir = dirname(resolved);
|
||||||
const cache = getSessionCache(input.sessionCaches, input.sessionID);
|
const cache = getSessionCache(input.sessionCaches, input.sessionID);
|
||||||
const readmePaths = findReadmeMdUp({ startDir: dir, rootDir: input.ctx.directory });
|
const readmePaths = await findReadmeMdUp({ startDir: dir, rootDir: input.ctx.directory });
|
||||||
|
|
||||||
let dirty = false;
|
let dirty = false;
|
||||||
for (const readmePath of readmePaths) {
|
for (const readmePath of readmePaths) {
|
||||||
@@ -39,7 +39,7 @@ export async function processFilePathForReadmeInjection(input: {
|
|||||||
if (cache.has(readmeDir)) continue;
|
if (cache.has(readmeDir)) continue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const content = readFileSync(readmePath, "utf-8");
|
const content = await readFile(readmePath, "utf-8");
|
||||||
const { result, truncated } = await input.truncator.truncate(
|
const { result, truncated } = await input.truncator.truncate(
|
||||||
input.sessionID,
|
input.sessionID,
|
||||||
content,
|
content,
|
||||||
|
|||||||
Reference in New Issue
Block a user