Skip to content

Commit b254594

Browse files
committed
feat(frontend): accept $variable references in loop blocks
Inside a control block (Loop Start ... Loop End) any primitive property of an operator may hold a loop-variable reference such as "$K", which the backend binds at run time (apache#8635). The property panel renders such fields as a text input with an autocomplete of the variables the enclosing Loop Starts declare, stores a complete typed numeral or true/false as the property's primitive and a reference as the literal string, and flags a name no enclosing block declares. The canvas validation waives the schema type check exactly where a reference sits in a primitive property, so the run button stays enabled, and still rejects undeclared names. Operators outside every block are unchanged. Both follow edits elsewhere in the graph: after a link is added or removed, or a Loop Start's properties change, every operator holding a reference is re-validated and the open form is rebuilt when its scope changed. Block membership (getEnclosingLoopStarts) and loop-variable extraction from the Loop Start's initialization code are pure functions with their own specs. The block walk caps its open-block count, so it ends on a cycle through a LoopStart or LoopEnd. Extraction follows Python's module-level binding: assignments under if/else, try/except, while and for, and for-loop targets count; def and class bodies do not.
1 parent 6defa2c commit b254594

19 files changed

Lines changed: 2282 additions & 10 deletions

‎frontend/src/app/app.module.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import { HuggingFaceImageUploadComponent } from "./workspace/component/hugging-f
106106
import { HuggingFaceComponent } from "./workspace/component/hugging-face/hugging-face.component";
107107
import { HuggingFaceAudioUploadComponent } from "./workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component";
108108
import { DatasetFileSelectorComponent } from "./workspace/component/dataset-file-selector/dataset-file-selector.component";
109+
import { LoopVariableInputComponent } from "./workspace/component/loop-variable-input/loop-variable-input.component";
109110
import { DatasetVersionSelectorComponent } from "./workspace/component/dataset-version-selector/dataset-version-selector.component";
110111
import { DatasetSelectionModalComponent } from "./workspace/component/dataset-selection-modal/dataset-selection-modal.component";
111112
import { ReActStepDetailModalComponent } from "./workspace/component/agent/agent-panel/react-step-detail-modal/react-step-detail-modal.component";
@@ -325,6 +326,7 @@ registerLocaleData(en);
325326
HuggingFaceAudioUploadComponent,
326327
HuggingFaceImageUploadComponent,
327328
DatasetFileSelectorComponent,
329+
LoopVariableInputComponent,
328330
DatasetVersionSelectorComponent,
329331
DatasetSelectionModalComponent,
330332
ReActStepDetailModalComponent,

‎frontend/src/app/common/formly/formly-config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { ResourceValueSelectorComponent } from "../../workspace/component/resour
3535
import { HuggingFaceImageUploadComponent } from "../../workspace/component/hugging-face-image-upload/hugging-face-image-upload.component";
3636
import { HuggingFaceComponent } from "../../workspace/component/hugging-face/hugging-face.component";
3737
import { HuggingFaceAudioUploadComponent } from "../../workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component";
38+
import { LoopVariableInputComponent } from "../../workspace/component/loop-variable-input/loop-variable-input.component";
39+
import { LOOP_VARIABLE_INPUT_TYPE } from "../../workspace/util/loop-variable-field.util";
3840

3941
/**
4042
* Configuration for using Json Schema with Formly.
@@ -85,6 +87,7 @@ export const TEXERA_FORMLY_CONFIG = {
8587
{ name: "multischema", component: MultiSchemaTypeComponent },
8688
{ name: "codearea", component: CodeareaCustomTemplateComponent },
8789
{ name: "inputautocomplete", component: DatasetFileSelectorComponent, wrappers: ["form-field"] },
90+
{ name: LOOP_VARIABLE_INPUT_TYPE, component: LoopVariableInputComponent, wrappers: ["form-field"] },
8891
{ name: "datasetversionselector", component: DatasetVersionSelectorComponent, wrappers: ["form-field"] },
8992
{ name: "huggingface", component: HuggingFaceComponent, wrappers: ["form-field"] },
9093
{ name: "huggingface-audio-upload", component: HuggingFaceAudioUploadComponent, wrappers: ["form-field"] },
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<input
20+
nz-input
21+
[formControl]="formControl"
22+
[formlyAttributes]="field"
23+
[nzAutocomplete]="loopVariables" />
24+
<nz-autocomplete
25+
#loopVariables
26+
[nzBackfill]="false">
27+
<nz-auto-option
28+
*ngFor="let option of referenceOptions"
29+
[nzValue]="option"
30+
[nzLabel]="option">
31+
{{ option }}
32+
</nz-auto-option>
33+
</nz-autocomplete>
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { Component } from "@angular/core";
21+
import { ComponentFixture, TestBed } from "@angular/core/testing";
22+
import { FormControl, FormGroup, ReactiveFormsModule } from "@angular/forms";
23+
import { By } from "@angular/platform-browser";
24+
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
25+
import { FieldTypeConfig, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core";
26+
import { FormlyJsonschema } from "@ngx-formly/core/json-schema";
27+
import { JSONSchema7 } from "json-schema";
28+
import { LoopVariableInputComponent } from "./loop-variable-input.component";
29+
import {
30+
applyLoopVariableField,
31+
LOOP_VARIABLE_INPUT_TYPE,
32+
primitiveSchemaType,
33+
} from "../../util/loop-variable-field.util";
34+
35+
/** A formly form around the fields a test builds, as the property panel hosts them. */
36+
@Component({
37+
template: `<form [formGroup]="form">
38+
<formly-form
39+
[form]="form"
40+
[fields]="fields"
41+
[model]="model"></formly-form>
42+
</form>`,
43+
imports: [ReactiveFormsModule, FormlyModule],
44+
})
45+
class FormHostComponent {
46+
form = new FormGroup({});
47+
model: Record<string, unknown> = {};
48+
fields: FormlyFieldConfig[] = [];
49+
}
50+
51+
describe("LoopVariableInputComponent", () => {
52+
describe("the options it offers", () => {
53+
let component: LoopVariableInputComponent;
54+
55+
beforeEach(async () => {
56+
await TestBed.configureTestingModule({
57+
imports: [LoopVariableInputComponent, NoopAnimationsModule],
58+
}).compileComponents();
59+
component = TestBed.createComponent(LoopVariableInputComponent).componentInstance;
60+
});
61+
62+
function holding(value: unknown, loopVariableOptions?: string[]): void {
63+
component.field = {
64+
formControl: new FormControl(value),
65+
props: loopVariableOptions === undefined ? {} : { loopVariableOptions },
66+
} as FieldTypeConfig;
67+
}
68+
69+
it("offers every variable while the box is empty", () => {
70+
holding("", ["$K", "$prev"]);
71+
expect(component.referenceOptions).toEqual(["$K", "$prev"]);
72+
holding(undefined, ["$K", "$prev"]);
73+
expect(component.referenceOptions).toEqual(["$K", "$prev"]);
74+
});
75+
76+
it("offers the variables matching a typed $ prefix, ignoring case", () => {
77+
holding("$p", ["$K", "$prev"]);
78+
expect(component.referenceOptions).toEqual(["$prev"]);
79+
holding("$P", ["$K", "$prev"]);
80+
expect(component.referenceOptions).toEqual(["$prev"]);
81+
holding("$", ["$K", "$prev"]);
82+
expect(component.referenceOptions).toEqual(["$K", "$prev"]);
83+
});
84+
85+
it("offers nothing while a plain value is typed, or when the block declares no variable", () => {
86+
holding("5", ["$K", "$prev"]);
87+
expect(component.referenceOptions).toEqual([]);
88+
holding(5, ["$K"]);
89+
expect(component.referenceOptions).toEqual([]);
90+
holding("", undefined);
91+
expect(component.referenceOptions).toEqual([]);
92+
});
93+
});
94+
95+
// Rendered through formly, as the property panel renders it, so that the parser formly runs on every
96+
// keystroke and the value it writes back into the box are the real ones.
97+
describe("rendered in a form", () => {
98+
const schema: JSONSchema7 = {
99+
type: "object",
100+
properties: {
101+
limit: { type: "integer", title: "limit" },
102+
fraction: { type: "number", title: "fraction" },
103+
prefix: { type: "string", title: "prefix" },
104+
},
105+
};
106+
107+
let fixture: ComponentFixture<FormHostComponent>;
108+
let host: FormHostComponent;
109+
110+
beforeEach(async () => {
111+
await TestBed.configureTestingModule({
112+
imports: [
113+
FormHostComponent,
114+
NoopAnimationsModule,
115+
FormlyModule.forRoot({ types: [{ name: LOOP_VARIABLE_INPUT_TYPE, component: LoopVariableInputComponent }] }),
116+
],
117+
}).compileComponents();
118+
fixture = TestBed.createComponent(FormHostComponent);
119+
host = fixture.componentInstance;
120+
// the property panel's mapping: every primitive property of an operator inside a block
121+
const root = TestBed.inject(FormlyJsonschema).toFieldConfig(schema, {
122+
map: (field: FormlyFieldConfig, source: JSONSchema7) => {
123+
const schemaType = primitiveSchemaType(source.type);
124+
if (schemaType !== undefined) {
125+
applyLoopVariableField(field, schemaType, ["K"]);
126+
field.props = { ...field.props, placeholder: `the ${field.key}`, attributes: { style: "color: red" } };
127+
}
128+
return field;
129+
},
130+
});
131+
root.type = undefined; // render the properties as a plain group, without the object type
132+
host.fields = [root];
133+
fixture.detectChanges();
134+
await fixture.whenStable();
135+
});
136+
137+
function input(key: string): HTMLInputElement {
138+
return fixture.debugElement.query(By.css(`input[placeholder="the ${key}"]`)).nativeElement;
139+
}
140+
141+
/** Types the text one key at a time, letting the box redraw between keys as it does for a user. */
142+
async function typeInto(key: string, text: string): Promise<void> {
143+
const box = input(key);
144+
for (const char of text) {
145+
box.value += char;
146+
box.dispatchEvent(new Event("input"));
147+
fixture.detectChanges();
148+
await fixture.whenStable();
149+
await Promise.resolve(); // the autocomplete writes a new value into the box in a microtask
150+
}
151+
}
152+
153+
it("renders one text box per field, carrying the field's attributes", () => {
154+
const box = input("limit");
155+
expect(box.getAttribute("nz-input")).not.toBeNull();
156+
expect(box.getAttribute("style")).toContain("color: red");
157+
expect(box.id).not.toBe("");
158+
});
159+
160+
it("stores a decimal typed one key at a time and keeps the box as typed", async () => {
161+
await typeInto("fraction", "1.05");
162+
expect(host.model["fraction"]).toBe(1.05);
163+
expect(input("fraction").value).toBe("1.05");
164+
});
165+
166+
it("keeps a leading minus and a leading zero while they are typed", async () => {
167+
await typeInto("fraction", "-0.5");
168+
expect(host.model["fraction"]).toBe(-0.5);
169+
expect(input("fraction").value).toBe("-0.5");
170+
});
171+
172+
it("keeps a numeral still being typed as text, flagged until it is complete", async () => {
173+
await typeInto("fraction", "7.");
174+
expect(input("fraction").value).toBe("7.");
175+
expect(host.model["fraction"]).toBe("7.");
176+
expect(host.form.get("fraction")?.hasError("type")).toBe(true);
177+
178+
await typeInto("fraction", "5");
179+
expect(host.model["fraction"]).toBe(7.5);
180+
expect(host.form.get("fraction")?.valid).toBe(true);
181+
});
182+
183+
it("stores an integer and a reference as typed", async () => {
184+
await typeInto("limit", "12");
185+
expect(host.model["limit"]).toBe(12);
186+
expect(input("limit").value).toBe("12");
187+
188+
input("limit").value = "";
189+
await typeInto("limit", "$K");
190+
expect(host.model["limit"]).toBe("$K");
191+
expect(input("limit").value).toBe("$K");
192+
});
193+
194+
it("gives a string field its empty-string default", () => {
195+
expect(host.model["prefix"]).toBe("");
196+
});
197+
});
198+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { Component } from "@angular/core";
21+
import { FieldType, FieldTypeConfig, FormlyAttributes } from "@ngx-formly/core";
22+
import { NgFor } from "@angular/common";
23+
import { ReactiveFormsModule } from "@angular/forms";
24+
import { NzInputDirective } from "ng-zorro-antd/input";
25+
import {
26+
NzAutocompleteComponent,
27+
NzAutocompleteOptionComponent,
28+
NzAutocompleteTriggerDirective,
29+
} from "ng-zorro-antd/auto-complete";
30+
31+
/**
32+
* The control of a primitive property (string, integer, number, boolean) of an operator inside a control
33+
* block. A text input, so that a loop-variable reference such as "$K" can be typed where the plain
34+
* control (a number box, a check box) could not take it, with an autocomplete offering "$" + name for
35+
* every variable the enclosing Loop Starts declare (`props.loopVariableOptions`). The field's parser,
36+
* set by applyLoopVariableField, stores numeric or true/false text as the property's primitive and a
37+
* reference as the literal string, which the backend binds to the loop variable at run time.
38+
*/
39+
@Component({
40+
templateUrl: "loop-variable-input.component.html",
41+
imports: [
42+
NgFor,
43+
ReactiveFormsModule,
44+
FormlyAttributes,
45+
NzInputDirective,
46+
NzAutocompleteTriggerDirective,
47+
NzAutocompleteComponent,
48+
NzAutocompleteOptionComponent,
49+
],
50+
})
51+
export class LoopVariableInputComponent extends FieldType<FieldTypeConfig> {
52+
/**
53+
* The "$name" options to offer: all of them while the box is empty, those matching the typed prefix
54+
* once it starts with "$", and none while a plain value is being typed.
55+
*/
56+
get referenceOptions(): string[] {
57+
const all: string[] = this.props["loopVariableOptions"] ?? [];
58+
const text = String(this.formControl.value ?? "");
59+
if (text === "") {
60+
return all;
61+
}
62+
if (!text.startsWith("$")) {
63+
return [];
64+
}
65+
const prefix = text.toLowerCase();
66+
return all.filter(option => option.toLowerCase().startsWith(prefix));
67+
}
68+
}

0 commit comments

Comments
 (0)