Skip to content
Merged
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
47 changes: 41 additions & 6 deletions formulus/src/webview/FormulusMessageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ import {FormulusMessageHandlers} from './FormulusMessageHandlers.types';
import {
FormInitData,
FormCompletionResult,
FormInfo,
} from './FormulusInterfaceDefinition';
import {FormService} from '../services/FormService';
import {Observation} from '../database/models/Observation';
Expand Down Expand Up @@ -967,12 +968,46 @@ export function createFormulusMessageHandlers(): FormulusMessageHandlers {
// TODO: implement run local model logic
console.log('Run local model handler called', fieldId, modelId, input);
},
onGetAvailableForms: async () => {
console.log(
'FormulusMessageHandlers: onGetAvailableForms handler invoked.',
);
// TODO: Implement logic to fetch available forms
return Promise.resolve([]); // Example: return empty array
onGetAvailableForms: async (): Promise<FormInfo[]> => {
try {
const formService = await FormService.getInstance();
const formSpecs = formService.getFormSpecs();

return formSpecs.map(spec => {
const schema = spec.schema || {};
const properties = schema.properties || {};

const coreFields: string[] = [];
const auxiliaryFields: string[] = [];

// Extract fields from schema properties
Object.keys(properties).forEach(fieldName => {
const field = properties[fieldName] || {};
const isCore =
field['x-core'] === true || fieldName.startsWith('core_');

if (isCore) {
coreFields.push(fieldName);
} else {
auxiliaryFields.push(fieldName);
}
});

return {
formType: spec.id,
name: spec.name,
version: spec.schemaVersion,
coreFields,
auxiliaryFields,
};
});
} catch (error) {
console.error(
'FormulusMessageHandlers: failed to get available forms',
error,
);
return [];
}
},
onGetObservations: async (
formType: string,
Expand Down
5 changes: 3 additions & 2 deletions formulus/src/webview/FormulusMessageHandlers.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Type definitions for WebView message handlers
// Must match the injected interface in FormulusInterfaceDefinition.ts
import {Observation} from '../database/models/observation';
import {Observation} from '../database/models/Observation';
import {
FormInitData,
FormCompletionResult,
FormInfo,
} from './FormulusInterfaceDefinition';

export interface FormulusMessageHandlers {
Expand Down Expand Up @@ -44,7 +45,7 @@ export interface FormulusMessageHandlers {
input: Record<string, any>,
) => void;
// New handlers to be added
onGetAvailableForms?: () => Promise<any>; // Adjust return type as needed (e.g., Promise<FormListItem[]>)
onGetAvailableForms?: () => Promise<FormInfo[]>;
onGetObservations?: (
formId: string,
isDraft?: boolean,
Expand Down
Loading