Files
pipelines/app/lib/features/pipelines/run_pipeline_intent.dart
T

152 lines
4.4 KiB
Dart

import 'dart:math';
import '../../core/api_gateway.dart';
/// The browser's three notification choices for a started run. `auto` omits
/// the wire field so the pipeline definition applies.
enum RunNotifyMode {
pipelineDefault('Pipeline default'),
notify('Notify me'),
silent('Silent');
const RunNotifyMode(this.label);
final String label;
/// The `notify_mode` value, or null when the definition decides.
String? get wireValue => switch (this) {
RunNotifyMode.pipelineDefault => null,
RunNotifyMode.notify => 'on',
RunNotifyMode.silent => 'off',
};
}
/// Optional report requirement requested for a manually started run.
enum RunOutputFormat {
none('No report requirement', ''),
html('HTML report', 'html'),
markdown('Markdown report', 'markdown');
const RunOutputFormat(this.label, this.wireValue);
final String label;
final String wireValue;
}
/// One confirmed launch intent: the pinned pipeline identity, the operator's
/// fields and the idempotency key that belongs to them.
///
/// A lost reply leaves this intent in place, so checking or retrying reuses
/// [requestKey] and the server can dedupe it. A confirmed refusal clears it;
/// the corrected attempt mints a fresh key.
class RunPipelineIntent {
const RunPipelineIntent({
required this.pipelineId,
required this.version,
required this.prompt,
required this.workspace,
required this.notifyMode,
this.outputFormat = RunOutputFormat.none,
required this.requestKey,
});
final int pipelineId;
final int version;
final String prompt;
final String workspace;
final RunNotifyMode notifyMode;
final RunOutputFormat outputFormat;
final String requestKey;
JsonMap toJson() => {
'pipelineId': pipelineId,
'version': version,
'prompt': prompt,
'workspace': workspace,
'notifyMode': notifyMode.name,
'outputFormat': outputFormat.name,
'requestKey': requestKey,
};
static RunPipelineIntent? fromJson(Object? value) {
if (value is! Map) return null;
final id = value['pipelineId'];
final version = value['version'];
final prompt = value['prompt'];
final workspace = value['workspace'];
final requestKey = value['requestKey'];
final notifyMode = RunNotifyMode.values.where(
(item) => item.name == value['notifyMode'],
);
final outputFormat = RunOutputFormat.values.where(
(item) => item.name == value['outputFormat'],
);
if (id is! int ||
version is! int ||
prompt is! String ||
workspace is! String ||
requestKey is! String ||
notifyMode.length != 1 ||
outputFormat.length != 1) {
return null;
}
return RunPipelineIntent(
pipelineId: id,
version: version,
prompt: prompt,
workspace: workspace,
requestKey: requestKey,
notifyMode: notifyMode.first,
outputFormat: outputFormat.first,
);
}
/// The same key is only ever reused for the same pinned definition.
bool matches(int pipelineId, int version) =>
this.pipelineId == pipelineId && this.version == version;
}
/// The entropy bound for a fresh key, written as a literal because a web
/// bitwise `1 << 32` wraps to 0 and makes `Random.nextInt` reject every call.
const _entropyBound = 0x100000000;
/// A fresh `request_key` for one confirmed launch. The server caps keys at 128
/// characters; this stays well inside that bound and is unique per call.
String newRunPipelineRequestKey(
int pipelineId,
int version, {
DateTime? now,
int? random,
}) {
final stamp = (now ?? DateTime.now()).microsecondsSinceEpoch;
final entropy = random ?? Random().nextInt(_entropyBound);
return 'app-run-$pipelineId-$version-$stamp-${entropy.toRadixString(36)}';
}
/// The id the server's run detail names, or null when it named no usable run.
int? runPipelineRunId(JsonMap run) {
final id = run['id'];
if (id is int && id > 0) return id;
if (id is num && id > 0) return id.toInt();
return null;
}
/// A launch attempt's outcome: either the server's run detail or a refusal
/// with the truth about whether admission is still unknown.
sealed class RunPipelineOutcome {
const RunPipelineOutcome();
}
class RunPipelineAccepted extends RunPipelineOutcome {
const RunPipelineAccepted(this.run);
final JsonMap run;
}
class RunPipelineRefused extends RunPipelineOutcome {
const RunPipelineRefused(this.message, {required this.admissionUnknown});
final String message;
final bool admissionUnknown;
}