34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import {
|
|
BUSINESS_DIRECTIVE_PREFIXES,
|
|
firstNonEmptyLine,
|
|
normalizeDirectiveText,
|
|
resolveBusinessRoute
|
|
} from './business-routes.js';
|
|
|
|
/**
|
|
* Business instruction prefixes that start an independent task.
|
|
*
|
|
* A message sent while another task is waiting for input is normally a
|
|
* continuation. A unique registered directive anywhere in a structured block
|
|
* is a new request and must get a new task/session instead of inheriting the
|
|
* previous parser context. A field signature may select the parser mode for a
|
|
* new task, but never changes continuation semantics by itself.
|
|
*/
|
|
function normalizedFirstLine(message: string): string {
|
|
return normalizeDirectiveText(firstNonEmptyLine(message));
|
|
}
|
|
|
|
export function detectBusinessDirective(message: string): string | null {
|
|
const line = normalizedFirstLine(message);
|
|
if (!line) return null;
|
|
const exactOrFuzzy = resolveBusinessRoute(message);
|
|
if (exactOrFuzzy.routeId && exactOrFuzzy.match !== 'signature') {
|
|
return exactOrFuzzy.matchedDirective || exactOrFuzzy.route?.directive || null;
|
|
}
|
|
return BUSINESS_DIRECTIVE_PREFIXES.find((prefix) => line.startsWith(normalizeDirectiveText(prefix))) || null;
|
|
}
|
|
|
|
export function isNewDirectiveMessage(message: string): boolean {
|
|
return detectBusinessDirective(message) !== null;
|
|
}
|