generatePrompt function
lang-core generatePrompt.
Implementation
String generatePrompt(PromptSpec spec) {
final root = spec.root;
final hasTools = spec.tools.isNotEmpty || spec.toolNames.isNotEmpty;
final toolCalls = spec.toolCalls ?? hasTools;
final hasState = spec.state.keys.any(_isStateKey);
final bindings =
spec.bindings ?? (toolCalls || spec.records.isNotEmpty || hasState);
final supportsExpressions = toolCalls || bindings;
final usesActionExpression = spec.components.values.any(
(c) =>
c.signature.contains('ActionExpression') ||
_restrictedSlot.hasMatch(c.signature),
);
bool allows(String name) => spec.allowedActions?.contains(name) ?? true;
final steps = _visibleSteps(spec, toolCalls: toolCalls, bindings: bindings);
final actions = [
for (final a in spec.actions.entries)
if (allows(a.key)) a,
];
final offered = {...steps.map((s) => s.key), ...actions.map((a) => a.key)};
final scope = _Scope(
spec.components.keys.toSet(),
{
for (final g in spec.groups)
for (final n in g.components)
if (!spec.components.containsKey(n)) n,
},
{
...builtinStepSpecs.map((s) => s.name),
...spec.actions.keys,
}.difference(offered),
);
final run = offered.contains('Run');
final mutationExample = run && offered.contains('Reset');
bool has(String kind) => spec.components.keys.any((n) => n.contains(kind));
final charts = has('Chart'), tables = has('Table'), forms = has('Form');
final parts = <String>[
spec.preamble ?? _preamble,
'',
_syntaxRules(
root,
supportsExpressions: supportsExpressions,
bindings: bindings,
),
'',
_componentSignatures(
spec,
scope,
offered,
bindings: bindings,
usesActionExpression: usesActionExpression,
),
];
final custom = spec.functions.values
.map((f) => functionLine(scope.entry(f)))
.toList();
if (spec.builtinFunctions ?? supportsExpressions) {
parts.addAll([
'',
_builtinFunctionsSection(
spec.builtinFunctionLines ?? _builtinFunctionLines,
custom,
toolCalls,
charts: charts,
tables: tables,
),
]);
} else if (custom.isNotEmpty) {
parts.addAll(['', '## Built-in Functions\n\n${custom.join('\n')}']);
}
if (toolCalls) parts.addAll(['', _query(run)]);
if (run) parts.addAll(['', _mutationSection]);
final customActions = [
for (final a in actions) '- ${functionLine(scope.entry(a.value))}',
];
if (usesActionExpression) {
parts.addAll([
'',
_actionSection([
for (final s in steps) '- ${functionLine(s.value)}',
...customActions,
], offered),
]);
} else if (customActions.isNotEmpty) {
parts.addAll([
'',
'$_actionHeader\n\nPass one step, or Action([...]) with several steps, as a button\'s action.\n\nAvailable steps:\n${customActions.join('\n')}',
]);
}
if (toolCalls && bindings) {
parts.addAll([
'',
_filters(forms: forms, run: run, mutationExample: mutationExample),
]);
}
if (toolCalls) {
parts.addAll([
'',
_workflow(run: run, mutationExample: mutationExample, charts: charts),
]);
}
if (hasTools) {
parts.addAll(['', _toolsSection(spec.toolNames, spec.tools, run)]);
}
if (spec.records.isNotEmpty) {
final bound = describeBoundRecords(spec.records);
parts.addAll([
'',
offered.containsAll(const ['Set', 'Reset'])
? bound
: bound.replaceFirst('never @Set, @Reset or bind', 'never bind'),
]);
}
if (hasState) parts.addAll(['', _stateSection(spec.state)]);
parts.addAll([
'',
_streamingRules(
root,
supportsExpressions: supportsExpressions,
toolCalls: toolCalls,
),
]);
final examples = [...spec.examples, ...spec.toolExamples];
if (examples.isNotEmpty) {
parts.addAll(['', '## Examples', '']);
for (final ex in examples) {
parts.addAll([ex, '']);
}
}
if (spec.editMode) parts.addAll(['', _editModeSection]);
if (spec.inlineMode) parts.addAll(['', _inlineModeSection]);
parts.add(
_importantRules(
root,
toolCalls: toolCalls,
bindings: bindings,
uses: [
if (tables) 'tables for comparisons',
if (charts) 'charts for trends',
if (forms) 'forms for input',
],
),
);
if (spec.additionalRules.isNotEmpty) {
parts.add('');
parts.addAll(spec.additionalRules.map((r) => '- $r'));
}
return parts.join('\n');
}