Skip to content

Commit 8d13ac5

Browse files
alban bertoliniclaude
andcommitted
refactor(workflow-executor): extract findField helper to deduplicate field resolution
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bb2901c commit 8d13ac5

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

packages/workflow-executor/src/executors/read-record-step-executor.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { z } from 'zod';
99

1010
import { NoReadableFieldsError, NoResolvedFieldsError, WorkflowExecutorError } from '../errors';
1111
import BaseStepExecutor from './base-step-executor';
12+
import { findField } from '../types/record';
1213

1314
const READ_RECORD_SYSTEM_PROMPT = `You are an AI agent reading fields from a record to answer a user request.
1415
Select the field(s) that best answer the request. You can read one field or multiple fields at once.
@@ -32,10 +33,7 @@ export default class ReadRecordStepExecutor extends BaseStepExecutor<AiTaskStepD
3233
schema = await this.getCollectionSchema(selectedRecordRef.collectionName);
3334
const selectedDisplayNames = await this.selectFields(schema, step.prompt);
3435
const resolvedFieldNames = selectedDisplayNames
35-
.map(
36-
name =>
37-
schema.fields.find(f => f.fieldName === name || f.displayName === name)?.fieldName,
38-
)
36+
.map(name => findField(schema, name)?.fieldName)
3937
.filter((name): name is string => name !== undefined);
4038

4139
if (resolvedFieldNames.length === 0) {
@@ -135,7 +133,7 @@ export default class ReadRecordStepExecutor extends BaseStepExecutor<AiTaskStepD
135133
fieldNames: string[],
136134
): FieldReadResult[] {
137135
return fieldNames.map(name => {
138-
const field = schema.fields.find(f => f.fieldName === name || f.displayName === name);
136+
const field = findField(schema, name);
139137

140138
if (!field) return { error: `Field not found: ${name}`, fieldName: name, displayName: name };
141139

packages/workflow-executor/src/executors/update-record-step-executor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { z } from 'zod';
99

1010
import { NoWritableFieldsError, WorkflowExecutorError } from '../errors';
1111
import BaseStepExecutor from './base-step-executor';
12+
import { findField } from '../types/record';
1213

1314
const UPDATE_RECORD_SYSTEM_PROMPT = `You are an AI agent updating a field on a record based on a user request.
1415
Select the field to update and provide the new value.
@@ -195,9 +196,7 @@ export default class UpdateRecordStepExecutor extends BaseStepExecutor<AiTaskSte
195196
}
196197

197198
private resolveFieldName(schema: CollectionSchema, displayName: string): string {
198-
const field = schema.fields.find(
199-
f => f.displayName === displayName || f.fieldName === displayName,
200-
);
199+
const field = findField(schema, displayName);
201200

202201
if (!field) {
203202
throw new WorkflowExecutorError(

packages/workflow-executor/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type {
3434
RecordRef,
3535
RecordData,
3636
} from './types/record';
37+
export { findField } from './types/record';
3738

3839
export type {
3940
Step,

packages/workflow-executor/src/types/record.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export interface CollectionSchema {
2121
actions: ActionSchema[];
2222
}
2323

24+
/** Find a field by fieldName or displayName. */
25+
export function findField(schema: CollectionSchema, name: string): FieldSchema | undefined {
26+
return schema.fields.find(f => f.fieldName === name || f.displayName === name);
27+
}
28+
2429
// -- Record types (data — source: AgentPort/RunStore) --
2530

2631
/** Lightweight pointer to a specific record. */

0 commit comments

Comments
 (0)