187 lines
6.8 KiB
Dart
187 lines
6.8 KiB
Dart
/// Permission approval cards: the model behind one `chat_permission` frame.
|
|
///
|
|
/// The web's `approvalCard`/`permissionRiskText`/`decisionLabel` are the target.
|
|
/// A card is derived from the server frame plus the operator's local answer, so
|
|
/// a server resolution always outranks a stale local decision.
|
|
library;
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
|
|
const chatDecisionOnce = 'once';
|
|
const chatDecisionAlways = 'always';
|
|
const chatDecisionReject = 'reject';
|
|
const chatDecisions = [
|
|
chatDecisionOnce,
|
|
chatDecisionAlways,
|
|
chatDecisionReject,
|
|
];
|
|
|
|
const chatApprovalBlankAction = 'tool call';
|
|
const chatApprovalHeadlineResolved = 'Already resolved';
|
|
const chatApprovalStatusPending = 'Waiting for your response';
|
|
const chatApprovalStatusAnswered = 'Waiting for the agent to continue…';
|
|
const chatApprovalStatusResolved =
|
|
'Another client already answered this request.';
|
|
const chatRiskReviewLabel = 'RISK REVIEW';
|
|
const chatApprovalOnceLabel = 'Allow once';
|
|
const chatApprovalAlwaysLabel = 'Always allow';
|
|
const chatApprovalRejectLabel = 'Reject';
|
|
const chatApprovalMaxResourceLines = 6;
|
|
|
|
/// How one approval request stands. `stale` is a server-resolved request whose
|
|
/// recorded decision is blank: the card can say only that it is already over.
|
|
enum ChatPermissionState { pending, answered, resolved, stale }
|
|
|
|
String chatDecisionLabel(String decision) => switch (decision) {
|
|
chatDecisionOnce => 'Allowed once',
|
|
chatDecisionAlways => 'Always allowed',
|
|
chatDecisionReject => 'Rejected',
|
|
_ => 'Resolved',
|
|
};
|
|
|
|
/// The amber risk sentence. The server's own summary wins; otherwise name the
|
|
/// action so the panel is never blank.
|
|
String chatPermissionRiskText(JsonMap permission) {
|
|
final message = _text(permission['message']);
|
|
if (message.isNotEmpty) return message;
|
|
final action = _text(permission['action']);
|
|
final what = action.isEmpty ? 'a tool call' : action;
|
|
return 'The agent is asking to run $what; review the request before allowing.';
|
|
}
|
|
|
|
/// The request's non-blank resources, in the server's own order.
|
|
///
|
|
/// A resource's recorded text is kept verbatim: the web filters blanks but never
|
|
/// trims, so an indented command or YAML block keeps its indentation in both the
|
|
/// six-line view and the expanded request.
|
|
List<String> chatPermissionResources(JsonMap permission) {
|
|
final resources = permission['resources'];
|
|
if (resources is! List) return const [];
|
|
return [
|
|
for (final resource in resources)
|
|
if (_text(resource).isNotEmpty) '${resource ?? ''}',
|
|
];
|
|
}
|
|
|
|
/// The resource block capped at six lines, an embedded newline counting as a
|
|
/// line. The full list stays available on the card for expansion.
|
|
String chatPermissionResourceText(JsonMap permission) {
|
|
final lines = [
|
|
for (final resource in chatPermissionResources(permission))
|
|
...resource.split('\n'),
|
|
];
|
|
if (lines.length <= chatApprovalMaxResourceLines) return lines.join('\n');
|
|
final kept = lines.take(chatApprovalMaxResourceLines).toList();
|
|
kept[kept.length - 1] = '${kept.last}…';
|
|
return kept.join('\n');
|
|
}
|
|
|
|
/// One rendered request. [resources] keeps every recorded entry; [resourceText]
|
|
/// is only the six-line view.
|
|
class ChatPermissionCardData {
|
|
const ChatPermissionCardData({
|
|
required this.requestId,
|
|
required this.resources,
|
|
required this.resourceText,
|
|
required this.risk,
|
|
required this.state,
|
|
required this.decision,
|
|
required this.note,
|
|
required this.alwaysLabel,
|
|
required this.headline,
|
|
required this.statusLine,
|
|
});
|
|
|
|
final String requestId;
|
|
final List<String> resources;
|
|
final String resourceText;
|
|
final String risk;
|
|
final ChatPermissionState state;
|
|
final String decision;
|
|
final String note;
|
|
final String alwaysLabel;
|
|
final String headline;
|
|
final String statusLine;
|
|
|
|
bool get actionable => state == ChatPermissionState.pending;
|
|
bool get showRisk =>
|
|
state == ChatPermissionState.pending ||
|
|
(state == ChatPermissionState.answered && decision == chatDecisionReject);
|
|
}
|
|
|
|
/// The card for [permission], or null when there is no request to show.
|
|
ChatPermissionCardData? chatPermissionCard(
|
|
JsonMap? permission, [
|
|
String answer = '',
|
|
]) {
|
|
if (permission == null) return null;
|
|
final requestId = _text(permission['request_id']);
|
|
if (requestId.isEmpty) return null;
|
|
final resolvedByServer =
|
|
permission['resolved'] == true ||
|
|
_text(permission['decision']).isNotEmpty;
|
|
final answerText = answer.trim();
|
|
final state = resolvedByServer
|
|
? (_text(permission['decision']).isEmpty
|
|
? ChatPermissionState.stale
|
|
: ChatPermissionState.resolved)
|
|
: answerText.isNotEmpty
|
|
? ChatPermissionState.answered
|
|
: ChatPermissionState.pending;
|
|
final decision = resolvedByServer
|
|
? _text(permission['decision'])
|
|
: answerText;
|
|
final note = resolvedByServer ? _text(permission['message']) : '';
|
|
final action = _text(permission['action']);
|
|
final alwaysLabel = _text(permission['always_label']);
|
|
final headline = switch (state) {
|
|
ChatPermissionState.pending =>
|
|
'Approve ${action.isEmpty ? chatApprovalBlankAction : action}',
|
|
ChatPermissionState.answered || ChatPermissionState.resolved =>
|
|
decision == chatDecisionAlways && alwaysLabel == 'Allow for session'
|
|
? 'Allowed for session'
|
|
: chatDecisionLabel(decision),
|
|
ChatPermissionState.stale => chatApprovalHeadlineResolved,
|
|
};
|
|
final statusLine = switch (state) {
|
|
ChatPermissionState.pending => chatApprovalStatusPending,
|
|
ChatPermissionState.answered => chatApprovalStatusAnswered,
|
|
ChatPermissionState.resolved || ChatPermissionState.stale =>
|
|
note.isEmpty ? chatApprovalStatusResolved : note,
|
|
};
|
|
return ChatPermissionCardData(
|
|
requestId: requestId,
|
|
resources: chatPermissionResources(permission),
|
|
resourceText: chatPermissionResourceText(permission),
|
|
risk: chatPermissionRiskText(permission),
|
|
state: state,
|
|
decision: decision,
|
|
note: note,
|
|
alwaysLabel: alwaysLabel.isEmpty ? chatApprovalAlwaysLabel : alwaysLabel,
|
|
headline: headline,
|
|
statusLine: statusLine,
|
|
);
|
|
}
|
|
|
|
/// A stable id for one confirmed reply. The id never changes across a retry of
|
|
/// the same request and decision, so the send gate can recognise a double tap.
|
|
String chatPermissionReplyId(String requestId, String decision) =>
|
|
'chat-reply:${requestId.trim()}:${decision.trim()}';
|
|
|
|
/// A one-shot gate for confirmed actions. [start] returns false while the same
|
|
/// stable id is already in flight or already accepted; [release] reopens an id
|
|
/// after a refused send so the operator can retry it.
|
|
class ChatActionGate {
|
|
final Set<String> _started = {};
|
|
|
|
bool start(String id) => _started.add(id);
|
|
|
|
void release(String id) => _started.remove(id);
|
|
|
|
bool isStarted(String id) => _started.contains(id);
|
|
|
|
void clear() => _started.clear();
|
|
}
|
|
|
|
String _text(Object? value) => '${value ?? ''}'.trim();
|