|
| 1 | +namespace OpenClaw.Chat; |
| 2 | + |
| 3 | +public enum ChatThreadStatus |
| 4 | +{ |
| 5 | + Created, |
| 6 | + Running, |
| 7 | + Suspended |
| 8 | +} |
| 9 | + |
| 10 | +public enum ChatActivity |
| 11 | +{ |
| 12 | + Idle, |
| 13 | + Working, |
| 14 | + AwaitingInput, |
| 15 | + AwaitingPermission, |
| 16 | + Error |
| 17 | +} |
| 18 | + |
| 19 | +public enum ChatTimelineItemKind |
| 20 | +{ |
| 21 | + User, |
| 22 | + Assistant, |
| 23 | + ToolCall, |
| 24 | + Reasoning, |
| 25 | + Status, |
| 26 | + Raw |
| 27 | +} |
| 28 | + |
| 29 | +public enum ChatToolCallStatus |
| 30 | +{ |
| 31 | + InProgress, |
| 32 | + Success, |
| 33 | + Error |
| 34 | +} |
| 35 | + |
| 36 | +public enum ChatTone |
| 37 | +{ |
| 38 | + Info, |
| 39 | + Success, |
| 40 | + Warning, |
| 41 | + Error, |
| 42 | + Dim |
| 43 | +} |
| 44 | + |
| 45 | +public record ChatThread |
| 46 | +{ |
| 47 | + public required string Id { get; init; } |
| 48 | + public required string Title { get; init; } |
| 49 | + public ChatThreadStatus Status { get; init; } |
| 50 | + public ChatActivity Activity { get; init; } |
| 51 | + public string? Cwd { get; init; } |
| 52 | + public string? Workspace { get; init; } |
| 53 | + public string? Repository { get; init; } |
| 54 | + public string? Branch { get; init; } |
| 55 | + public string? HostName { get; init; } |
| 56 | + public string? Compute { get; init; } |
| 57 | + public string? ProfileName { get; init; } |
| 58 | + public string? Model { get; init; } |
| 59 | + public int? HistoryCursor { get; init; } |
| 60 | + public DateTimeOffset? CreatedAt { get; init; } |
| 61 | + public DateTimeOffset? UpdatedAt { get; init; } |
| 62 | + |
| 63 | + public string DisplayTitle => Title; |
| 64 | +} |
| 65 | + |
| 66 | +public record ChatTimelineItem( |
| 67 | + string Id, |
| 68 | + ChatTimelineItemKind Kind, |
| 69 | + string Text, |
| 70 | + bool IsStreaming = false, |
| 71 | + string? ToolName = null, |
| 72 | + ChatToolCallStatus? ToolResult = null, |
| 73 | + string? ToolOutput = null, |
| 74 | + string? IntentSummary = null, |
| 75 | + JsonObject? ToolArgs = null, |
| 76 | + ChatTone? Tone = null); |
| 77 | + |
| 78 | +public record ChatPermissionRequest(string RequestId, string PermissionKind, string ToolName, string Detail); |
| 79 | + |
| 80 | +public record ChatTimelineState( |
| 81 | + System.Collections.Immutable.ImmutableList<ChatTimelineItem> Entries, |
| 82 | + bool TurnActive, |
| 83 | + int NextId, |
| 84 | + string? ActiveAssistantId, |
| 85 | + string? ActiveReasoningId, |
| 86 | + string? ActiveToolCallId, |
| 87 | + string? CurrentIntent, |
| 88 | + System.Collections.Immutable.ImmutableHashSet<string> LocalNonces, |
| 89 | + bool HistoryLoaded = false, |
| 90 | + ChatPermissionRequest? PendingPermission = null) |
| 91 | +{ |
| 92 | + public static ChatTimelineState Initial() => new( |
| 93 | + System.Collections.Immutable.ImmutableList<ChatTimelineItem>.Empty, |
| 94 | + false, 1, null, null, null, null, |
| 95 | + System.Collections.Immutable.ImmutableHashSet<string>.Empty); |
| 96 | +} |
| 97 | + |
| 98 | +public record ChatHistoryPage(ChatEvent[] Events, int NextSince, int PrevBefore, bool HasMore); |
| 99 | + |
| 100 | +public abstract record ChatEvent; |
| 101 | +public record ChatUserMessageEvent(string Text, string? Nonce = null) : ChatEvent; |
| 102 | +public record ChatThinkingEvent(string Text) : ChatEvent; |
| 103 | +public record ChatReasoningEvent(string Text) : ChatEvent; |
| 104 | +public record ChatReasoningDeltaEvent(string Text) : ChatEvent; |
| 105 | +public record ChatMessageEvent(string Text, string? ReasoningText = null, bool ReconcilePrevious = false) : ChatEvent; |
| 106 | +public record ChatMessageDeltaEvent(string Text) : ChatEvent; |
| 107 | +public record ChatTurnEndEvent() : ChatEvent; |
| 108 | +public record ChatIntentEvent(string Intent) : ChatEvent; |
| 109 | +public record ChatToolStartEvent(string Text, string ToolName, JsonObject? ToolArgs = null) : ChatEvent; |
| 110 | +public record ChatToolOutputEvent(string Text) : ChatEvent; |
| 111 | +public record ChatToolErrorEvent(string Text) : ChatEvent; |
| 112 | +public record ChatContextChangedEvent(string? Cwd, string? GitBranch) : ChatEvent; |
| 113 | +public record ChatStatusEvent(string Text, ChatTone Tone) : ChatEvent; |
| 114 | +public record ChatErrorEvent(string Text) : ChatEvent; |
| 115 | +public record ChatRestoredEvent(string Text) : ChatEvent; |
| 116 | +public record ChatPermissionRequestEvent(string RequestId, string PermissionKind, string ToolName, string Detail) : ChatEvent; |
| 117 | +public record ChatModelChangedEvent(string Model) : ChatEvent; |
| 118 | +public record ChatRawEvent(string EventType, string? Text = null) : ChatEvent; |
| 119 | + |
| 120 | +public record ChatDataSnapshot( |
| 121 | + ChatThread[] Threads, |
| 122 | + IReadOnlyDictionary<string, ChatTimelineState> Timelines, |
| 123 | + string? DefaultThreadId, |
| 124 | + string? ConnectionStatus, |
| 125 | + string[] AvailableModels); |
| 126 | + |
| 127 | +public sealed class ChatDataChangedEventArgs(ChatDataSnapshot snapshot) : EventArgs |
| 128 | +{ |
| 129 | + public ChatDataSnapshot Snapshot { get; } = snapshot; |
| 130 | +} |
| 131 | + |
| 132 | +public enum ChatProviderNotificationKind |
| 133 | +{ |
| 134 | + TurnComplete, |
| 135 | + PermissionRequested, |
| 136 | + Error |
| 137 | +} |
| 138 | + |
| 139 | +public record ChatProviderNotification( |
| 140 | + ChatProviderNotificationKind Kind, |
| 141 | + string ThreadId, |
| 142 | + string Title, |
| 143 | + string? Message = null, |
| 144 | + string? ToolName = null); |
| 145 | + |
| 146 | +public sealed class ChatProviderNotificationEventArgs(ChatProviderNotification notification) : EventArgs |
| 147 | +{ |
| 148 | + public ChatProviderNotification Notification { get; } = notification; |
| 149 | +} |
| 150 | + |
| 151 | +public interface IChatDataProvider : IAsyncDisposable |
| 152 | +{ |
| 153 | + string DisplayName { get; } |
| 154 | + |
| 155 | + event EventHandler<ChatDataChangedEventArgs>? Changed; |
| 156 | + event EventHandler<ChatProviderNotificationEventArgs>? NotificationRequested; |
| 157 | + |
| 158 | + Task<ChatDataSnapshot> LoadAsync(CancellationToken cancellationToken = default); |
| 159 | + Task<ChatThread> CreateThreadAsync(string? initialMessage = null, CancellationToken cancellationToken = default); |
| 160 | + Task SendMessageAsync(string threadId, string message, CancellationToken cancellationToken = default); |
| 161 | + Task StopResponseAsync(string threadId, CancellationToken cancellationToken = default); |
| 162 | + Task SetThreadSuspendedAsync(string threadId, bool suspended, CancellationToken cancellationToken = default); |
| 163 | + Task DeleteThreadAsync(string threadId, CancellationToken cancellationToken = default); |
| 164 | + Task SetModelAsync(string threadId, string model, CancellationToken cancellationToken = default); |
| 165 | + Task SetPermissionModeAsync(string threadId, bool allowAll, CancellationToken cancellationToken = default); |
| 166 | + Task RespondToPermissionAsync(string threadId, string requestId, bool allow, CancellationToken cancellationToken = default); |
| 167 | +} |
0 commit comments