Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9419: add registry lookup to platform protocol #9421

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const config = {
],
setupFilesAfterEnv: [
"<rootDir>/src/testUtils/testAfterEnv.ts",
"<rootDir>/src/testUtils/injectRegistries.ts",
"<rootDir>/src/testUtils/injectPlatform.ts",
"<rootDir>/src/testUtils/extendedExpectations.ts",
"jest-extended/all",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class ModVariableSchemasVisitor extends PipelineVisitor {
): void {
super.visitBrick(position, brickConfig, extra);

const { block } = this.allBricks.get(brickConfig.id) ?? {};
const { brick } = this.allBricks.get(brickConfig.id) ?? {};

if (block?.getModVariableSchema) {
this.schemaPromises.push(block.getModVariableSchema?.(brickConfig));
if (brick?.getModVariableSchema) {
this.schemaPromises.push(brick.getModVariableSchema?.(brickConfig));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/analysisVisitors/selectorAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class SelectorAnalysis extends AnalysisVisitorWithResolvedBricksABC {

try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- wrapped in try/catch
brick = this.allBlocks.get(brickConfig.id)!.block;
brick = this.allBlocks.get(brickConfig.id)!.brick;
} catch {
return;
}
Expand Down
7 changes: 3 additions & 4 deletions src/analysis/analysisVisitors/varAnalysis/varAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ describe("Collecting available vars", () => {
[
formState.modComponent.brickPipeline[0]!.id,
{
block: {
// HtmlReader's output schema, see @/bricks/readers/HtmlReader.ts
brick: {
outputSchema,
},
},
Expand Down Expand Up @@ -819,7 +818,7 @@ describe("Collecting available vars", () => {
IdentityTransformer.BRICK_ID,
{
type: BrickTypes.TRANSFORM,
block: new IdentityTransformer(),
brick: new IdentityTransformer(),
},
],
]),
Expand Down Expand Up @@ -1164,7 +1163,7 @@ describe("Collecting available vars", () => {
CustomFormRenderer.BRICK_ID,
{
type: "renderer",
block: new CustomFormRenderer(),
brick: new CustomFormRenderer(),
},
],
]),
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/analysisVisitors/varAnalysis/varAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class VarAnalysis extends PipelineExpressionVisitor implements Analysis {
* @param blockConfig the block configuration
*/
private safeGetOutputSchema(blockConfig: BrickConfig): Schema {
const block = this.allBlocks.get(blockConfig.id)?.block;
const block = this.allBlocks.get(blockConfig.id)?.brick;

if (!block) {
return {};
Expand Down Expand Up @@ -641,7 +641,7 @@ class VarAnalysis extends PipelineExpressionVisitor implements Analysis {
const block = extra.parentNode?.id
? this.allBlocks.get(extra.parentNode.id)
: null;
const variableSchema = block?.block.getPipelineVariableSchema?.(
const variableSchema = block?.brick.getPipelineVariableSchema?.(
extra.parentNode,
extra.pipelinePropName,
);
Expand Down
12 changes: 6 additions & 6 deletions src/background/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe("requestRunInAllFrames", () => {
const meta: MessengerMeta = messengerMetaFactory();

const promise = requestRunInAllFrames.call(meta, {
blockId: registryIdFactory(),
blockArgs: unsafeAssumeValidArg({}),
brickId: registryIdFactory(),
brickArgs: unsafeAssumeValidArg({}),
options: optionsFactory(),
});
await expect(promise).resolves.toStrictEqual([]);
Expand All @@ -85,8 +85,8 @@ describe("requestRunInAllFrames", () => {
const meta: MessengerMeta = messengerMetaFactory();

const promise = requestRunInAllFrames.call(meta, {
blockId: registryIdFactory(),
blockArgs: unsafeAssumeValidArg({}),
brickId: registryIdFactory(),
brickArgs: unsafeAssumeValidArg({}),
options: optionsFactory(),
});

Expand All @@ -105,8 +105,8 @@ describe("requestRunInAllFrames", () => {
const meta: MessengerMeta = messengerMetaFactory();

const promise = requestRunInAllFrames.call(meta, {
blockId: registryIdFactory(),
blockArgs: unsafeAssumeValidArg({}),
brickId: registryIdFactory(),
brickArgs: unsafeAssumeValidArg({}),
options: optionsFactory(),
});

Expand Down
4 changes: 2 additions & 2 deletions src/bricks/brickFilterHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ export function makeIsBrickAllowedForPipeline(
}
}

return ({ type, block }: TypedBrickPair) =>
type !== excludedType || ALWAYS_SHOW.has(block.id);
return ({ type, brick }: TypedBrickPair) =>
type !== excludedType || ALWAYS_SHOW.has(brick.id);
}
6 changes: 4 additions & 2 deletions src/bricks/readers/readerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { type ReaderConfig } from "@/bricks/types";
import brickRegistry from "@/bricks/registry";
import { getPlatform } from "@/platform/platformContext";
import ArrayCompositeReader from "@/bricks/readers/ArrayCompositeReader";
import { isPlainObject, mapValues } from "lodash";
import CompositeReader from "@/bricks/readers/CompositeReader";
Expand All @@ -29,7 +29,9 @@ export async function mergeReaders(
readerConfig: ReaderConfig,
): Promise<Reader> {
if (typeof readerConfig === "string") {
return brickRegistry.lookup(readerConfig) as Promise<Reader>;
return getPlatform().registry.bricks.lookup(
readerConfig,
) as Promise<Reader>;
}

if (Array.isArray(readerConfig)) {
Expand Down
2 changes: 1 addition & 1 deletion src/bricks/registerBuiltinBricks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function registerBuiltinBricks(): void {
}

brickRegistry.register([
...getAllTransformers(brickRegistry),
...getAllTransformers(),
...getAllEffects(),
...getAllRenderers(),
...getAllReaders(),
Expand Down
4 changes: 2 additions & 2 deletions src/bricks/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("bricksMap", () => {
const typedBrick = typedBricks.get(brick.id);

expect(typedBrick!.type).toBe("reader");
expect(typedBrick!.block).toBe(brick);
expect(typedBrick!.brick).toBe(brick);
});

test("returns bricks of multiple registrations", async () => {
Expand All @@ -66,7 +66,7 @@ describe("bricksMap", () => {

for (const brick of bricks) {
expect(enrichedBlocks.get(brick.id)!.type).toBe("reader");
expect(enrichedBlocks.get(brick.id)!.block).toBe(brick);
expect(enrichedBlocks.get(brick.id)!.brick).toBe(brick);
}
});

Expand Down
7 changes: 3 additions & 4 deletions src/bricks/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import { partial } from "lodash";
import { allSettled } from "@/utils/promiseUtils";

/**
* A brick along with inferred/calculated information
* A brick along with its inferred/calculated information
*/
export type TypedBrickPair = {
block: Brick;
brick: Brick;
type: BrickType | null;
};

Expand Down Expand Up @@ -68,7 +68,7 @@ class BrickRegistry extends MemoryRegistry<RegistryId, Brick> {
// this module? getType references the brickRegistry in order to calculate the type for composite bricks
// that are defined as a pipeline of other blocks.
typeCache.set(item.id, {
block: item,
brick: item,
type: await getType(item),
});
}),
Expand Down Expand Up @@ -102,7 +102,6 @@ class BrickRegistry extends MemoryRegistry<RegistryId, Brick> {

/**
* The singleton brick registry instance
* @see initRuntime
*/
const registry = new BrickRegistry();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import GetBrickInterfaceTransformer from "@/bricks/transformers/GetBrickInterfac
import { pick } from "lodash";
import { BusinessError } from "@/errors/businessErrors";

const brick = new GetBrickInterfaceTransformer(brickRegistry);
const brick = new GetBrickInterfaceTransformer();

beforeEach(() => {
brickRegistry.clear();
Expand Down
16 changes: 8 additions & 8 deletions src/bricks/transformers/GetBrickInterfaceTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
*/

import { TransformerABC } from "@/types/bricks/transformerTypes";
import { type BrickArgs } from "@/types/runtimeTypes";
import { type BrickArgs, type BrickOptions } from "@/types/runtimeTypes";
import { type Schema } from "@/types/schemaTypes";
import { validateRegistryId } from "@/types/helpers";
import type { RegistryId, RegistryProtocol } from "@/types/registryTypes";
import type { Brick } from "@/types/brickTypes";
import type { RegistryId } from "@/types/registryTypes";
import { BusinessError, PropError } from "@/errors/businessErrors";
import { propertiesToSchema } from "@/utils/schemaUtils";

Expand All @@ -34,7 +33,7 @@ import { propertiesToSchema } from "@/utils/schemaUtils";
class GetBrickInterfaceTransformer extends TransformerABC {
static BRICK_ID = validateRegistryId("@pixiebrix/reflect/brick-get");

constructor(private readonly registry: RegistryProtocol<RegistryId, Brick>) {
constructor() {
super(
GetBrickInterfaceTransformer.BRICK_ID,
"[Experimental] Get Brick Interface",
Expand Down Expand Up @@ -90,9 +89,10 @@ class GetBrickInterfaceTransformer extends TransformerABC {
["id", "name", "inputSchema", "outputSchema"],
);

async transform({
registryId,
}: BrickArgs<{ registryId: string }>): Promise<unknown> {
async transform(
{ registryId }: BrickArgs<{ registryId: string }>,
{ platform }: BrickOptions,
): Promise<unknown> {
try {
validateRegistryId(registryId);
} catch {
Expand All @@ -107,7 +107,7 @@ class GetBrickInterfaceTransformer extends TransformerABC {
let brick;

try {
brick = await this.registry.lookup(registryId as RegistryId);
brick = await platform.registry.bricks.lookup(registryId as RegistryId);
} catch {
throw new BusinessError(
"Could not find brick with registry id: " + registryId,
Expand Down
2 changes: 1 addition & 1 deletion src/bricks/transformers/RunBrickByIdTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { brickOptionsFactory } from "@/testUtils/factories/runtimeFactories";
import { InputValidationError } from "@/bricks/errors";
import { BusinessError } from "@/errors/businessErrors";

const brick = new RunBrickByIdTransformer(brickRegistry);
const brick = new RunBrickByIdTransformer();

beforeEach(() => {
brickRegistry.clear();
Expand Down
9 changes: 5 additions & 4 deletions src/bricks/transformers/RunBrickByIdTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import {
} from "@/types/runtimeTypes";
import { type Schema } from "@/types/schemaTypes";
import { validateRegistryId } from "@/types/helpers";
import type { RegistryId, RegistryProtocol } from "@/types/registryTypes";
import type { Brick } from "@/types/brickTypes";
import type { RegistryId } from "@/types/registryTypes";
import { BusinessError, PropError } from "@/errors/businessErrors";
import { throwIfInvalidInput } from "@/runtime/runtimeUtils";
import { propertiesToSchema } from "@/utils/schemaUtils";
Expand All @@ -39,7 +38,7 @@ import { propertiesToSchema } from "@/utils/schemaUtils";
class RunBrickByIdTransformer extends TransformerABC {
static BRICK_ID = validateRegistryId("@pixiebrix/reflect/run");

constructor(private readonly registry: RegistryProtocol<RegistryId, Brick>) {
constructor() {
super(
RunBrickByIdTransformer.BRICK_ID,
"[Experimental] Run Brick by ID",
Expand Down Expand Up @@ -83,7 +82,9 @@ class RunBrickByIdTransformer extends TransformerABC {
let brick;

try {
brick = await this.registry.lookup(registryId as RegistryId);
brick = await options.platform.registry.bricks.lookup(
registryId as RegistryId,
);
} catch {
throw new BusinessError(
"Could not find brick with registry id: " + registryId,
Expand Down
9 changes: 3 additions & 6 deletions src/bricks/transformers/brickFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import {
DefinitionKinds,
type Metadata,
type Registry,
type RegistryId,
type SemVerString,
} from "@/types/registryTypes";
Expand All @@ -56,13 +57,9 @@ import {
inputProperties,
unionSchemaDefinitionTypes,
} from "@/utils/schemaUtils";
import type BaseRegistry from "@/registry/memoryRegistry";
import type { PlatformCapability } from "@/platform/capabilities";
import { runHeadlessPipeline } from "@/contentScript/messenger/api";

// Interface to avoid circular dependency with the implementation
type BrickRegistryProtocol = BaseRegistry<RegistryId, Brick>;

export type BrickDefinition = {
/**
* The runtime version to use when running the Brick.
Expand Down Expand Up @@ -141,7 +138,7 @@ class UserDefinedBrick extends BrickABC {
readonly version?: SemVerString;

constructor(
private readonly registry: BrickRegistryProtocol,
private readonly registry: Registry<RegistryId, Brick>,
public readonly component: BrickDefinition,
) {
const { id, name, description, version } = component.metadata;
Expand Down Expand Up @@ -392,7 +389,7 @@ class UserDefinedBrick extends BrickABC {

// Put registry first for easier partial application
export function fromJS(
registry: BrickRegistryProtocol,
registry: Registry<RegistryId, Brick>,
component: UnknownObject,
): Brick {
if (component.kind == null) {
Expand Down
9 changes: 3 additions & 6 deletions src/bricks/transformers/getAllTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import ConvertDocument from "@/bricks/transformers/convertDocument";
import { SearchText } from "@/bricks/transformers/searchText";
import { WithAsyncModVariable } from "@/bricks/transformers/controlFlow/WithAsyncModVariable";
import { JavaScriptTransformer } from "@/bricks/transformers/javascript";
import type { RegistryId, RegistryProtocol } from "@/types/registryTypes";
import RunBrickByIdTransformer from "@/bricks/transformers/RunBrickByIdTransformer";
import GetBrickInterfaceTransformer from "@/bricks/transformers/GetBrickInterfaceTransformer";
import RunMetadataTransformer from "@/bricks/transformers/RunMetadataTransformer";
Expand All @@ -66,9 +65,7 @@ import LocalPrompt from "@/bricks/transformers/ai/LocalPrompt";
import LocalChatCompletionTransformer from "@/bricks/transformers/ai/LocalChatCompletion";
import LocalSummarization from "@/bricks/transformers/ai/LocalSummarization";

function getAllTransformers(
registry: RegistryProtocol<RegistryId, Brick>,
): Brick[] {
function getAllTransformers(): Brick[] {
return [
new JavaScriptTransformer(),
new JQTransformer(),
Expand Down Expand Up @@ -106,8 +103,8 @@ function getAllTransformers(
new ExtensionDiagnostics(),

// Reflection/Meta Bricks
new RunBrickByIdTransformer(registry),
new GetBrickInterfaceTransformer(registry),
new RunBrickByIdTransformer(),
new GetBrickInterfaceTransformer(),
new RunMetadataTransformer(),

// Control Flow Bricks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const SourceLabel: React.FunctionComponent<SourceLabelProps> = ({
} else {
label =
brickConfig.label ??
allBricks.get(brickConfig.id)?.block?.name ??
allBricks.get(brickConfig.id)?.brick?.name ??
source;
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import registerExternalMessenger from "@/background/messenger/external/registrat
import registerMessenger from "@/contentScript/messenger/registration";
import registerBuiltinBricks from "@/bricks/registerBuiltinBricks";
import registerContribBricks from "@/contrib/registerContribBricks";
import brickRegistry from "@/bricks/registry";
import { initNavigation } from "@/contentScript/lifecycle";
import { initTelemetry } from "@/background/messenger/api";
import { initToaster } from "@/utils/notify";
Expand All @@ -38,7 +37,6 @@ import { onUncaughtError } from "@/errors/errorHelpers";
import initFloatingActions from "@/components/floatingActions/initFloatingActions";
import { initSidebarActivation } from "@/contentScript/sidebarActivation";
import { initPerformanceMonitoring } from "@/contentScript/performanceMonitoring";
import { initRuntime } from "@/runtime/reducePipeline";
import { setPlatform } from "@/platform/platformContext";
import { markDocumentAsFocusableByUser } from "@/utils/focusTracker";
import contentScriptPlatform from "@/contentScript/contentScriptPlatform";
Expand Down Expand Up @@ -76,9 +74,6 @@ export async function init(): Promise<void> {
registerBuiltinBricks();
registerContribBricks();
markDocumentAsFocusableByUser();
// Since 1.8.2, the brick registry was de-coupled from the runtime to avoid circular dependencies
// Since 1.8.10, we inject the platform into the runtime
initRuntime(brickRegistry);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required registry methods are in the platform now

initDeferredLoginController();

initTelemetry();
Expand Down
Loading
Loading