Skip to content

Commit 73385f4

Browse files
committed
Prime the related loaders when loading tool usages
1 parent e5aad4a commit 73385f4

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/components/tools/tool-usage/tool-usage-by-container.loader.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { type LoaderContextType } from '@seedcompany/data-loader';
12
import { type ID, type Resource } from '~/common';
23
import {
34
type DataLoaderStrategy,
45
LoaderFactory,
56
type LoaderOptionsOf,
67
} from '~/core/data-loader';
8+
import { ToolLoader } from '../tool/tool.loader';
79
import { type ToolUsage } from './dto';
10+
import { ToolUsageLoader } from './tool-usage.loader';
811
import { ToolUsageService } from './tool-usage.service';
912

1013
export interface UsagesByContainer {
@@ -27,7 +30,17 @@ export class ToolUsageByContainerLoader
2730

2831
async loadMany(
2932
ids: readonly Resource[],
33+
ctx: LoaderContextType,
3034
): Promise<readonly UsagesByContainer[]> {
31-
return await this.usages.readManyForContainers(ids);
35+
const res = await this.usages.readManyForContainers(ids);
36+
37+
const canonicalUsages = await ctx.getLoader(ToolUsageLoader);
38+
const usages = res.flatMap((u) => u.usages);
39+
canonicalUsages.primeAll(usages);
40+
41+
const canonicalTools = await ctx.getLoader(ToolLoader);
42+
canonicalTools.primeAll(usages.map((x) => x.tool));
43+
44+
return res;
3245
}
3346
}

src/components/tools/tool-usage/tool-usage-by-tool.loader.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { LoaderContextType } from '@seedcompany/data-loader';
12
import { type ID } from '~/common';
23
import {
34
type DataLoaderStrategy,
@@ -6,6 +7,7 @@ import {
67
} from '~/core/data-loader';
78
import { type Tool } from '../tool/dto';
89
import { type ToolUsage } from './dto';
10+
import { ToolUsageLoader } from './tool-usage.loader';
911
import { ToolUsageService } from './tool-usage.service';
1012

1113
export interface UsagesByTool {
@@ -26,7 +28,16 @@ export class ToolUsageByToolLoader
2628
} satisfies LoaderOptionsOf<ToolUsageByToolLoader>;
2729
}
2830

29-
async loadMany(tools: readonly Tool[]): Promise<readonly UsagesByTool[]> {
30-
return await this.usages.readManyForTools(tools);
31+
async loadMany(
32+
tools: readonly Tool[],
33+
ctx: LoaderContextType,
34+
): Promise<readonly UsagesByTool[]> {
35+
const res = await this.usages.readManyForTools(tools);
36+
37+
const canonicalUsages = await ctx.getLoader(ToolUsageLoader);
38+
const usages = res.flatMap((u) => u.usages);
39+
canonicalUsages.primeAll(usages);
40+
41+
return res;
3142
}
3243
}

0 commit comments

Comments
 (0)