mcp-decisive
Version:
MCP server for WRAP decision-making framework with structured output
121 lines • 5.68 kB
JavaScript
import { Result, ok, err } from 'neverthrow';
const OptionId = {
generate: () => {
return `option-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
},
fromString: (value) => {
const errors = validateOptionId(value);
return errors.length > 0 ? err(errors) : ok(value);
},
toString: (id) => id
};
const OptionText = {
create: (value) => {
const errors = validateOptionText(value);
return errors.length > 0 ? err(errors) : ok(value.trim());
},
toString: (text) => text
};
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Implementation Section - Business Logic
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Smart constructor for Option
const constructOption = (params) => OptionText.create(params.text)
.andThen(text => {
const option = {
id: OptionId.generate(),
text,
...(params.supplementaryInfo && { supplementaryInfo: params.supplementaryInfo })
};
return ok(option);
})
.mapErr(errors => errors.map(e => OptionError.create('OptionCreationFailed', `Text validation failed: ${e.message}`)));
// Smart constructor for OptionList with business rule enforcement
const constructOptionList = (params) => validateOptionListParams(params)
.andThen(validParams => createOptionsFromParams(validParams.options)
.andThen(options => validateOptionListBusinessRules(options)
.map(() => createOptionList(options))));
// Helper functions
const validateOptionListParams = (params) => {
const errors = [];
if (!params.options || params.options.length === 0) {
errors.push(OptionListError.create('InvalidOptionCount', 'Options array cannot be empty'));
}
return errors.length > 0 ? err(errors) : ok(params);
};
const createOptionsFromParams = (optionParams) => {
const optionResults = optionParams.map((optionParam, index) => constructOption(optionParam)
.mapErr(optionErrors => OptionListError.create('OptionCreationFailed', `Option ${index + 1}: ${optionErrors.map(e => e.message).join(', ')}`)));
return Result.combine(optionResults).mapErr(errors => [errors]);
};
const validateOptionListBusinessRules = (options) => {
const errors = validateOptionCount(options);
return errors.length > 0 ? err(errors) : ok(null);
};
const createOptionList = (options) => {
return {
options: options
};
};
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Business Rules - Domain Policies
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
const validateOptionId = (value) => {
if (!value || value.trim().length === 0) {
return [ValidationError.create('required', 'optionId', 'Option ID is required')];
}
return [];
};
const validateOptionText = (value) => {
if (!value || value.trim().length === 0) {
return [ValidationError.create('required', 'text', '選択肢は必須です')];
}
if (value.length > 30) {
return [ValidationError.create('too_long', 'text', '選択肢は30文字以内で入力してください')];
}
return [];
};
// Business rule: 選択肢は3〜5個である
const validateOptionCount = (options) => {
if (options.length < 3 || options.length > 5) {
return [OptionListError.create('InvalidOptionCount', `Option count must be between 3 and 5, but got ${options.length}`)];
}
return [];
};
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Error Handling Utilities
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
const ValidationError = {
create: (type, field, message) => ({
type, field, message
})
};
const OptionError = {
create: (type, message) => ({ type, message })
};
const OptionListError = {
create: (type, message) => ({ type, message })
};
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Public API - Term Model Interface
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/**
* Option Term Model
*/
export const OptionModel = {
create: constructOption
};
/**
* OptionList Term Model
*/
export const OptionListModel = {
create: constructOptionList
};
/**
* Value Object Constructors
*/
export const Values = {
OptionId,
OptionText
};
//# sourceMappingURL=option.js.map