@pellux/goodvibes-tui 0.18.12 → 0.18.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/CHANGELOG.md +172 -0
  2. package/README.md +1 -1
  3. package/docs/foundation-artifacts/operator-contract.json +1 -1
  4. package/package.json +3 -2
  5. package/src/config/index.ts +1 -138
  6. package/src/core/conversation-rendering.ts +3 -3
  7. package/src/core/conversation.ts +176 -423
  8. package/src/core/history.ts +45 -0
  9. package/src/core/orchestrator.ts +3 -735
  10. package/src/core/system-message-router.ts +19 -58
  11. package/src/daemon/cli.ts +82 -6
  12. package/src/input/command-registry.ts +2 -0
  13. package/src/input/commands/control-room-runtime.ts +1 -1
  14. package/src/input/commands/health-runtime.ts +1 -1
  15. package/src/input/commands/local-setup-review.ts +1 -1
  16. package/src/input/commands/platform-access-runtime.ts +1 -1
  17. package/src/input/commands/qrcode-runtime.ts +20 -0
  18. package/src/input/commands/subscription-runtime.ts +1 -1
  19. package/src/input/commands.ts +2 -0
  20. package/src/input/handler-content-actions.ts +2 -2
  21. package/src/input/handler-feed.ts +7 -1
  22. package/src/input/handler-modal-routes.ts +19 -2
  23. package/src/input/handler-modal-token-routes.ts +4 -1
  24. package/src/input/handler-picker-routes.ts +4 -2
  25. package/src/input/handler-ui-state.ts +1 -1
  26. package/src/input/handler.ts +1 -1
  27. package/src/input/model-picker.ts +11 -0
  28. package/src/input/search.ts +1 -1
  29. package/src/input/selection.ts +2 -2
  30. package/src/input/settings-modal.ts +31 -3
  31. package/src/main.ts +1 -1
  32. package/src/panels/agent-inspector-panel.ts +3 -3
  33. package/src/panels/agent-logs-panel.ts +26 -27
  34. package/src/panels/approval-panel.ts +2 -2
  35. package/src/panels/automation-control-panel.ts +3 -3
  36. package/src/panels/base-panel.ts +14 -14
  37. package/src/panels/builtin/operations.ts +1 -1
  38. package/src/panels/builtin/session.ts +67 -1
  39. package/src/panels/builtin/shared.ts +4 -4
  40. package/src/panels/cockpit-panel.ts +2 -2
  41. package/src/panels/communication-panel.ts +3 -3
  42. package/src/panels/context-visualizer-panel.ts +2 -2
  43. package/src/panels/control-plane-panel.ts +3 -3
  44. package/src/panels/cost-tracker-panel.ts +3 -3
  45. package/src/panels/debug-panel.ts +2 -2
  46. package/src/panels/diff-panel.ts +2 -2
  47. package/src/panels/docs-panel.ts +1 -1
  48. package/src/panels/eval-panel.ts +2 -2
  49. package/src/panels/file-explorer-panel.ts +3 -3
  50. package/src/panels/file-preview-panel.ts +3 -3
  51. package/src/panels/forensics-panel.ts +2 -2
  52. package/src/panels/git-panel.ts +1 -1
  53. package/src/panels/hooks-panel.ts +3 -3
  54. package/src/panels/incident-review-panel.ts +1 -1
  55. package/src/panels/intelligence-panel.ts +2 -2
  56. package/src/panels/knowledge-panel.ts +1 -1
  57. package/src/panels/local-auth-panel.ts +2 -2
  58. package/src/panels/marketplace-panel.ts +1 -1
  59. package/src/panels/mcp-panel.ts +3 -3
  60. package/src/panels/memory-panel.ts +1 -1
  61. package/src/panels/ops-control-panel.ts +3 -3
  62. package/src/panels/ops-strategy-panel.ts +2 -2
  63. package/src/panels/orchestration-panel.ts +2 -2
  64. package/src/panels/panel-list-panel.ts +6 -6
  65. package/src/panels/plan-dashboard-panel.ts +1 -1
  66. package/src/panels/plugins-panel.ts +2 -2
  67. package/src/panels/policy-panel.ts +2 -2
  68. package/src/panels/polish.ts +3 -3
  69. package/src/panels/provider-account-snapshot.ts +1 -1
  70. package/src/panels/provider-accounts-panel.ts +25 -29
  71. package/src/panels/provider-health-panel.ts +2 -2
  72. package/src/panels/provider-stats-panel.ts +3 -3
  73. package/src/panels/qr-panel.ts +182 -0
  74. package/src/panels/remote-panel.ts +3 -3
  75. package/src/panels/routes-panel.ts +3 -3
  76. package/src/panels/sandbox-panel.ts +2 -2
  77. package/src/panels/schedule-panel.ts +1 -1
  78. package/src/panels/scrollable-list-panel.ts +407 -0
  79. package/src/panels/security-panel.ts +2 -2
  80. package/src/panels/services-panel.ts +3 -3
  81. package/src/panels/session-browser-panel.ts +2 -2
  82. package/src/panels/settings-sync-panel.ts +2 -2
  83. package/src/panels/skills-panel.ts +6 -6
  84. package/src/panels/subscription-panel.ts +3 -3
  85. package/src/panels/symbol-outline-panel.ts +3 -3
  86. package/src/panels/system-messages-panel.ts +4 -4
  87. package/src/panels/tasks-panel.ts +2 -2
  88. package/src/panels/thinking-panel.ts +3 -3
  89. package/src/panels/token-budget-panel.ts +1 -1
  90. package/src/panels/tool-inspector-panel.ts +3 -3
  91. package/src/panels/types.ts +5 -5
  92. package/src/panels/watchers-panel.ts +3 -3
  93. package/src/panels/welcome-panel.ts +1 -1
  94. package/src/panels/worktree-panel.ts +22 -21
  95. package/src/panels/wrfc-panel.ts +3 -3
  96. package/src/permissions/prompt.ts +3 -22
  97. package/src/plugins/loader.ts +15 -304
  98. package/src/renderer/agent-detail-modal.ts +1 -1
  99. package/src/renderer/autocomplete-overlay.ts +2 -2
  100. package/src/renderer/bookmark-modal.ts +1 -1
  101. package/src/renderer/bottom-bar.ts +2 -2
  102. package/src/renderer/buffer.ts +1 -1
  103. package/src/renderer/code-block.ts +2 -2
  104. package/src/renderer/compositor.ts +2 -2
  105. package/src/renderer/context-inspector.ts +1 -1
  106. package/src/renderer/conversation-layout.ts +2 -2
  107. package/src/renderer/conversation-overlays.ts +1 -1
  108. package/src/renderer/conversation-surface.ts +2 -2
  109. package/src/renderer/diff-view.ts +2 -2
  110. package/src/renderer/diff.ts +1 -1
  111. package/src/renderer/file-picker-overlay.ts +2 -2
  112. package/src/renderer/file-tree.ts +2 -2
  113. package/src/renderer/help-overlay.ts +1 -1
  114. package/src/renderer/history-search-overlay.ts +2 -2
  115. package/src/renderer/live-tail-modal.ts +1 -1
  116. package/src/renderer/markdown.ts +2 -2
  117. package/src/renderer/modal-factory.ts +3 -3
  118. package/src/renderer/model-picker-overlay.ts +2 -2
  119. package/src/renderer/overlay-box.ts +2 -2
  120. package/src/renderer/panel-composite.ts +1 -1
  121. package/src/renderer/panel-picker-overlay.ts +2 -2
  122. package/src/renderer/panel-tab-bar.ts +1 -1
  123. package/src/renderer/panel-workspace-bar.ts +1 -1
  124. package/src/renderer/process-indicator.ts +2 -2
  125. package/src/renderer/process-modal.ts +1 -1
  126. package/src/renderer/profile-picker-modal.ts +2 -2
  127. package/src/renderer/progress.ts +2 -2
  128. package/src/renderer/qr-renderer.ts +117 -0
  129. package/src/renderer/search-overlay.ts +2 -2
  130. package/src/renderer/selection-modal-overlay.ts +2 -2
  131. package/src/renderer/session-picker-modal.ts +2 -2
  132. package/src/renderer/settings-modal-helpers.ts +122 -0
  133. package/src/renderer/settings-modal.ts +149 -113
  134. package/src/renderer/shell-surface.ts +1 -1
  135. package/src/renderer/system-message.ts +1 -1
  136. package/src/renderer/tab-strip.ts +2 -2
  137. package/src/renderer/text-layout.ts +1 -1
  138. package/src/renderer/thinking.ts +1 -1
  139. package/src/renderer/tool-call.ts +2 -2
  140. package/src/renderer/ui-factory.ts +2 -2
  141. package/src/runtime/bootstrap-command-context.ts +5 -6
  142. package/src/runtime/bootstrap-command-parts.ts +32 -18
  143. package/src/runtime/bootstrap-core.ts +3 -2
  144. package/src/runtime/bootstrap-hook-bridge.ts +15 -174
  145. package/src/runtime/bootstrap-shell.ts +4 -4
  146. package/src/runtime/bootstrap.ts +7 -2
  147. package/src/runtime/context.ts +4 -20
  148. package/src/runtime/diagnostics/panels/index.ts +6 -6
  149. package/src/runtime/diagnostics/panels/ops.ts +1 -1
  150. package/src/runtime/diagnostics/panels/panel-resources.ts +118 -0
  151. package/src/runtime/perf/panel-contracts.ts +32 -0
  152. package/src/runtime/perf/panel-health-monitor.ts +18 -0
  153. package/src/runtime/services.ts +5 -5
  154. package/src/runtime/store/domains/domain-read-matrix.ts +0 -2
  155. package/src/runtime/store/selectors/index.ts +11 -6
  156. package/src/runtime/store/state.ts +12 -4
  157. package/src/runtime/ui-events.ts +1 -0
  158. package/src/runtime/ui-read-model-helpers.ts +1 -32
  159. package/src/runtime/ui-read-models-observability-maintenance.ts +1 -81
  160. package/src/runtime/ui-read-models-observability-options.ts +1 -5
  161. package/src/runtime/ui-read-models-observability-remote.ts +1 -73
  162. package/src/runtime/ui-read-models-observability-security.ts +1 -172
  163. package/src/runtime/ui-read-models-observability-system.ts +1 -217
  164. package/src/runtime/ui-read-models-observability.ts +1 -59
  165. package/src/runtime/ui-service-queries.ts +1 -114
  166. package/src/runtime/ui-services.ts +1 -1
  167. package/src/shell/ui-openers.ts +1 -1
  168. package/src/tools/index.ts +1 -186
  169. package/src/types/grid.ts +48 -0
  170. package/src/utils/clipboard.ts +21 -0
  171. package/src/utils/splash-lines.ts +1 -1
  172. package/src/utils/terminal-width.ts +185 -0
  173. package/src/version.ts +1 -1
  174. package/src/config/service-registry.ts +0 -1
  175. package/src/config/subscription-providers.ts +0 -127
  176. package/src/daemon/facade-composition.ts +0 -398
  177. package/src/daemon/facade.ts +0 -638
  178. package/src/daemon/surface-policy.ts +0 -60
  179. package/src/daemon/types.ts +0 -191
  180. package/src/runtime/diagnostics/actions.ts +0 -776
  181. package/src/runtime/diagnostics/index.ts +0 -99
  182. package/src/runtime/diagnostics/panels/agents.ts +0 -252
  183. package/src/runtime/diagnostics/panels/events.ts +0 -188
  184. package/src/runtime/diagnostics/panels/health.ts +0 -242
  185. package/src/runtime/diagnostics/panels/tasks.ts +0 -251
  186. package/src/runtime/diagnostics/panels/tool-calls.ts +0 -267
  187. package/src/runtime/diagnostics/provider.ts +0 -262
  188. package/src/runtime/store/domains/conversation.ts +0 -181
  189. package/src/runtime/store/domains/permissions.ts +0 -143
  190. package/src/runtime/store/helpers/reducers/conversation.ts +0 -228
  191. package/src/runtime/store/helpers/reducers/lifecycle.ts +0 -440
  192. package/src/runtime/store/helpers/reducers/shared.ts +0 -60
  193. package/src/runtime/store/helpers/reducers/sync.ts +0 -555
  194. package/src/runtime/store/helpers/reducers.ts +0 -30
  195. package/src/runtime/ui-read-models-core.ts +0 -95
  196. package/src/runtime/ui-read-models-operations.ts +0 -203
@@ -1,181 +0,0 @@
1
- /**
2
- * Conversation domain state — tracks the active turn lifecycle,
3
- * message buffer, streaming deltas, and tool dispatch state.
4
- */
5
-
6
- import type { TurnStopReason } from '@pellux/goodvibes-sdk/platform/runtime/events/turn';
7
-
8
- /** States for the turn lifecycle machine. */
9
- export type TurnState =
10
- | 'idle'
11
- | 'preflight'
12
- | 'streaming'
13
- | 'tool_dispatch'
14
- | 'post_hooks'
15
- | 'completed'
16
- | 'failed'
17
- | 'cancelled';
18
-
19
- /** States for the tool execution machine. */
20
- export type ToolExecutionState =
21
- | 'received'
22
- | 'validated'
23
- | 'prehooked'
24
- | 'permissioned'
25
- | 'executing'
26
- | 'mapped'
27
- | 'posthooked'
28
- | 'succeeded'
29
- | 'failed'
30
- | 'cancelled';
31
-
32
- /** A lightweight record of an in-progress or recently completed tool call. */
33
- export interface ActiveToolCall {
34
- /** Tool call ID from the provider. */
35
- callId: string;
36
- /** Tool name. */
37
- toolName: string;
38
- /** Serialized arguments (JSON string). */
39
- args: string;
40
- /** Current execution state. */
41
- state: ToolExecutionState;
42
- /** Epoch ms when this call entered the current state. */
43
- stateEnteredAt: number;
44
- /** Phase timestamps keyed by ToolExecutionState. */
45
- phaseTimestamps: Partial<Record<ToolExecutionState, number>>;
46
- /** Error message if state === 'failed'. */
47
- error?: string;
48
- }
49
-
50
- /** Token usage accumulated for the current or most recent turn. */
51
- export interface TurnUsage {
52
- inputTokens: number;
53
- outputTokens: number;
54
- cacheReadTokens: number;
55
- cacheWriteTokens: number;
56
- reasoningTokens: number;
57
- }
58
-
59
- /** Streaming progress for the current turn. */
60
- export interface StreamProgress {
61
- /** Accumulated text content so far. */
62
- accumulated: string;
63
- /** Latest reasoning delta (if any). */
64
- reasoningAccumulated: string;
65
- /** Compact preview of the most recent partial tool call streamed so far. */
66
- partialToolPreview?: string;
67
- /** Number of delta events received. */
68
- deltaCount: number;
69
- /** Epoch ms of the first delta. */
70
- firstDeltaAt?: number;
71
- /** Epoch ms of the most recent delta. */
72
- lastDeltaAt?: number;
73
- }
74
-
75
- export interface TurnReconciliationRecord {
76
- count: number;
77
- callIds: string[];
78
- toolNames: string[];
79
- reason: string;
80
- timestamp: number;
81
- isMalformed: boolean;
82
- }
83
-
84
- /**
85
- * ConversationDomainState — turn lifecycle and streaming state.
86
- */
87
- export interface ConversationDomainState {
88
- // ── Domain metadata ────────────────────────────────────────────────────────
89
- /** Monotonic revision counter; increments on every mutation. */
90
- revision: number;
91
- /** Timestamp of last mutation (Date.now()). */
92
- lastUpdatedAt: number;
93
- /** Subsystem that triggered the last mutation. */
94
- source: string;
95
-
96
- // ── Turn lifecycle ─────────────────────────────────────────────────────────
97
- /** Current state of the turn lifecycle machine. */
98
- turnState: TurnState;
99
- /** Unique ID of the current turn (undefined when idle). */
100
- currentTurnId?: string;
101
- /** Epoch ms when the current turn started (undefined when idle). */
102
- turnStartedAt?: number;
103
- /** Epoch ms when the current turn completed/failed (undefined while active). */
104
- turnEndedAt?: number;
105
- /** Error from the most recent failed turn. */
106
- lastTurnError?: string;
107
- /** Explicit terminal reason for the most recently finished turn. */
108
- lastTurnStopReason?: TurnStopReason;
109
- /** Final assistant response for the most recently completed turn. */
110
- lastTurnResponse?: string;
111
- /** Most recent preflight failure message, if any. */
112
- lastPreflightFailure?: string;
113
- /** Total number of turns completed in this session. */
114
- totalTurns: number;
115
-
116
- // ── Streaming ──────────────────────────────────────────────────────────────
117
- /** Live streaming progress (populated during 'streaming' state). */
118
- stream: StreamProgress;
119
-
120
- // ── Tool dispatch ──────────────────────────────────────────────────────────
121
- /** Map of callId → ActiveToolCall for all in-flight tool calls. */
122
- activeToolCalls: Map<string, ActiveToolCall>;
123
- /** Count of tool calls dispatched in the current turn. */
124
- toolCallsThisTurn: number;
125
- /** Most recent reconciliation record for unresolved or malformed tool calls. */
126
- lastToolReconciliation?: TurnReconciliationRecord;
127
-
128
- // ── Token accounting ───────────────────────────────────────────────────────
129
- /** Usage for the current or most recent turn. */
130
- currentTurnUsage: TurnUsage;
131
- /** Cumulative usage across all turns in this session. */
132
- sessionUsage: TurnUsage;
133
-
134
- // ── Context window ─────────────────────────────────────────────────────────
135
- /** Estimated token count of the current context window. */
136
- estimatedContextTokens: number;
137
- /** Whether a context warning threshold has been crossed. */
138
- contextWarningActive: boolean;
139
- /** Message count in the current session conversation. */
140
- messageCount: number;
141
- }
142
-
143
- function makeEmptyUsage(): TurnUsage {
144
- return { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, reasoningTokens: 0 };
145
- }
146
-
147
- /**
148
- * Returns the default initial state for the conversation domain.
149
- */
150
- export function createInitialConversationState(): ConversationDomainState {
151
- return {
152
- revision: 0,
153
- lastUpdatedAt: 0,
154
- source: 'init',
155
- turnState: 'idle',
156
- currentTurnId: undefined,
157
- turnStartedAt: undefined,
158
- turnEndedAt: undefined,
159
- lastTurnError: undefined,
160
- lastTurnStopReason: undefined,
161
- lastTurnResponse: undefined,
162
- lastPreflightFailure: undefined,
163
- totalTurns: 0,
164
- stream: {
165
- accumulated: '',
166
- reasoningAccumulated: '',
167
- partialToolPreview: undefined,
168
- deltaCount: 0,
169
- firstDeltaAt: undefined,
170
- lastDeltaAt: undefined,
171
- },
172
- activeToolCalls: new Map(),
173
- toolCallsThisTurn: 0,
174
- lastToolReconciliation: undefined,
175
- currentTurnUsage: makeEmptyUsage(),
176
- sessionUsage: makeEmptyUsage(),
177
- estimatedContextTokens: 0,
178
- contextWarningActive: false,
179
- messageCount: 0,
180
- };
181
- }
@@ -1,143 +0,0 @@
1
- /**
2
- * Permissions domain state — tracks the permission mode, session approvals,
3
- * and the most recent permission decision with full audit trail.
4
- */
5
-
6
- import type { PermissionCategory } from '@pellux/goodvibes-sdk/platform/permissions/manager';
7
- import type { PermissionRiskLevel } from '@pellux/goodvibes-sdk/platform/permissions/types';
8
-
9
- /** Permission evaluation modes (maps to PermissionsToolConfig). */
10
- export type PermissionMode =
11
- | 'default'
12
- | 'plan'
13
- | 'allow-all'
14
- | 'custom'
15
- | 'background-restricted';
16
-
17
- /** States for the permission decision machine. */
18
- export type PermissionDecisionMachineState =
19
- | 'collect_rules'
20
- | 'normalize_input'
21
- | 'evaluate_policy'
22
- | 'evaluate_runtime_mode'
23
- | 'evaluate_session_override'
24
- | 'final_safety_checks'
25
- | 'decision_emitted';
26
-
27
- /** The outcome of a permission decision. */
28
- export type PermissionDecisionOutcome = 'approved' | 'denied' | 'deferred';
29
-
30
- /** Source layer that determined the permission outcome. */
31
- export type PermissionSourceLayer =
32
- | 'config_policy'
33
- | 'managed_policy'
34
- | 'runtime_mode'
35
- | 'session_override'
36
- | 'safety_check'
37
- | 'user_prompt';
38
-
39
- /** Reason codes for a permission decision. */
40
- export type PermissionDecisionReason =
41
- | 'config_allow'
42
- | 'config_deny'
43
- | 'managed_policy_allow'
44
- | 'managed_policy_deny'
45
- | 'mode_allow_all'
46
- | 'mode_denied'
47
- | 'mode_plan_deny'
48
- | 'mode_background_restricted'
49
- | 'session_cached_approval'
50
- | 'session_cached_denial'
51
- | 'safety_guardrail'
52
- | 'user_approved'
53
- | 'user_denied';
54
-
55
- /** Full record of the most recent permission decision. */
56
- export interface PermissionDecision {
57
- /** Tool call ID this decision is for. */
58
- callId: string;
59
- /** Tool name. */
60
- toolName: string;
61
- /** Permission category. */
62
- category: PermissionCategory;
63
- /** State machine state when the decision was emitted. */
64
- machineState: PermissionDecisionMachineState;
65
- /** Final outcome. */
66
- outcome: PermissionDecisionOutcome;
67
- /** Primary reason code. */
68
- reason: PermissionDecisionReason;
69
- /** Source layer that yielded the decision. */
70
- sourceLayer: PermissionSourceLayer;
71
- /** Whether the decision was persisted to session approvals. */
72
- persisted: boolean;
73
- /** Semantic classification surfaced with the decision. */
74
- classification?: string;
75
- /** Risk level surfaced with the decision. */
76
- riskLevel?: PermissionRiskLevel;
77
- /** Human-readable summary for operators and the prompt UI. */
78
- summary?: string;
79
- /** Epoch ms when the decision was emitted. */
80
- decidedAt: number;
81
- }
82
-
83
- /**
84
- * PermissionDomainState — permission configuration and decision state.
85
- */
86
- export interface PermissionDomainState {
87
- // ── Domain metadata ────────────────────────────────────────────────────────
88
- /** Monotonic revision counter; increments on every mutation. */
89
- revision: number;
90
- /** Timestamp of last mutation (Date.now()). */
91
- lastUpdatedAt: number;
92
- /** Subsystem that triggered the last mutation. */
93
- source: string;
94
-
95
- // ── Mode ───────────────────────────────────────────────────────────────────
96
- /** Current global permission mode. */
97
- mode: PermissionMode;
98
- /** Whether the user is currently being prompted for a permission decision. */
99
- awaitingDecision: boolean;
100
- /** Current state of the decision state machine (while a decision is in flight). */
101
- decisionMachineState?: PermissionDecisionMachineState;
102
-
103
- // ── Session approvals ──────────────────────────────────────────────────────
104
- /**
105
- * Per-session tool approval cache.
106
- * Key format: "<toolName>:<argsHash>" → boolean (approved/denied).
107
- */
108
- sessionApprovals: Map<string, boolean>;
109
- /** Number of approvals granted this session. */
110
- approvalCount: number;
111
- /** Number of denials issued this session. */
112
- denialCount: number;
113
-
114
- // ── Last decision ──────────────────────────────────────────────────────────
115
- /** The most recent permission decision record. */
116
- lastDecision?: PermissionDecision;
117
-
118
- // ── Statistics ─────────────────────────────────────────────────────────────
119
- /** Total permission checks performed this session. */
120
- totalChecks: number;
121
- /** Total checks that were served from session cache. */
122
- cachedChecks: number;
123
- }
124
-
125
- /**
126
- * Returns the default initial state for the permissions domain.
127
- */
128
- export function createInitialPermissionsState(): PermissionDomainState {
129
- return {
130
- revision: 0,
131
- lastUpdatedAt: 0,
132
- source: 'init',
133
- mode: 'default',
134
- awaitingDecision: false,
135
- decisionMachineState: undefined,
136
- sessionApprovals: new Map(),
137
- approvalCount: 0,
138
- denialCount: 0,
139
- lastDecision: undefined,
140
- totalChecks: 0,
141
- cachedChecks: 0,
142
- };
143
- }
@@ -1,228 +0,0 @@
1
- import type { TurnEvent } from '@pellux/goodvibes-sdk/platform/runtime/events/turn';
2
- import type { ToolEvent } from '@pellux/goodvibes-sdk/platform/runtime/events/tools';
3
- import type { ConversationDomainState, ActiveToolCall, ToolExecutionState } from '@pellux/goodvibes-sdk/platform/runtime/store/domains/conversation';
4
- import { canStartNewTurn, formatPartialToolPreview, isCurrentTurnEvent, isTerminalTurnState, now, resetStreamState, updateDomainMetadata } from '@pellux/goodvibes-sdk/platform/runtime/store/helpers/reducers/shared';
5
-
6
- function formatToolArgs(event: ToolEvent | TurnEvent): string {
7
- if ('args' in event) {
8
- return JSON.stringify(event.args);
9
- }
10
- return '{}';
11
- }
12
-
13
- export function updateConversationState(
14
- domain: ConversationDomainState,
15
- event: TurnEvent | ToolEvent,
16
- ): ConversationDomainState {
17
- const source = event.type;
18
- if ('callId' in event) {
19
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) {
20
- return domain;
21
- }
22
- const activeToolCalls = new Map(domain.activeToolCalls);
23
- const existing = activeToolCalls.get(event.callId);
24
- const stateByEvent: Partial<Record<ToolEvent['type'], ToolExecutionState>> = {
25
- TOOL_RECEIVED: 'received',
26
- TOOL_VALIDATED: 'validated',
27
- TOOL_PREHOOKED: 'prehooked',
28
- TOOL_PERMISSIONED: 'permissioned',
29
- TOOL_EXECUTING: 'executing',
30
- TOOL_MAPPED: 'mapped',
31
- TOOL_POSTHOOKED: 'posthooked',
32
- TOOL_SUCCEEDED: 'succeeded',
33
- TOOL_FAILED: 'failed',
34
- TOOL_CANCELLED: 'cancelled',
35
- BUDGET_EXCEEDED_MS: 'failed',
36
- BUDGET_EXCEEDED_TOKENS: 'failed',
37
- BUDGET_EXCEEDED_COST: 'failed',
38
- };
39
- const nextState = stateByEvent[event.type];
40
- const timestamp = now();
41
- const nextRecord: ActiveToolCall = {
42
- callId: event.callId,
43
- toolName: event.tool,
44
- args: existing?.args ?? formatToolArgs(event),
45
- state: nextState ?? existing?.state ?? 'received',
46
- stateEnteredAt: 'startedAt' in event ? event.startedAt : timestamp,
47
- phaseTimestamps: {
48
- ...(existing?.phaseTimestamps ?? {}),
49
- ...(nextState ? { [nextState]: timestamp } : {}),
50
- },
51
- error:
52
- 'error' in event
53
- ? event.error
54
- : event.type === 'BUDGET_EXCEEDED_MS'
55
- ? `${event.phase} exceeded ${event.limitMs}ms budget`
56
- : event.type === 'BUDGET_EXCEEDED_TOKENS'
57
- ? `${event.phase} exceeded ${event.limitTokens} token budget`
58
- : event.type === 'BUDGET_EXCEEDED_COST'
59
- ? `${event.phase} exceeded $${event.limitCostUsd} cost budget`
60
- : existing?.error,
61
- };
62
- activeToolCalls.set(event.callId, nextRecord);
63
- return {
64
- ...updateDomainMetadata(domain, source),
65
- activeToolCalls,
66
- currentTurnId: domain.currentTurnId ?? event.turnId,
67
- toolCallsThisTurn: event.type === 'TOOL_RECEIVED' ? domain.toolCallsThisTurn + 1 : domain.toolCallsThisTurn,
68
- };
69
- }
70
-
71
- switch (event.type) {
72
- case 'TURN_SUBMITTED':
73
- if (!canStartNewTurn(domain)) return domain;
74
- return {
75
- ...updateDomainMetadata(domain, source),
76
- turnState: 'preflight',
77
- currentTurnId: event.turnId,
78
- turnStartedAt: now(),
79
- turnEndedAt: undefined,
80
- lastTurnError: undefined,
81
- lastTurnStopReason: undefined,
82
- lastTurnResponse: undefined,
83
- lastPreflightFailure: undefined,
84
- stream: resetStreamState(),
85
- activeToolCalls: new Map(),
86
- toolCallsThisTurn: 0,
87
- lastToolReconciliation: undefined,
88
- };
89
- case 'PREFLIGHT_OK':
90
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'preflight') return domain;
91
- return {
92
- ...updateDomainMetadata(domain, source),
93
- currentTurnId: event.turnId,
94
- turnState: 'preflight',
95
- };
96
- case 'PREFLIGHT_FAIL':
97
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'preflight') return domain;
98
- return {
99
- ...updateDomainMetadata(domain, source),
100
- currentTurnId: event.turnId,
101
- turnState: 'failed',
102
- turnEndedAt: now(),
103
- lastTurnError: event.reason,
104
- lastTurnStopReason: event.stopReason,
105
- lastPreflightFailure: event.reason,
106
- stream: resetStreamState(),
107
- };
108
- case 'STREAM_START':
109
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'preflight') return domain;
110
- return {
111
- ...updateDomainMetadata(domain, source),
112
- currentTurnId: event.turnId,
113
- turnState: 'streaming',
114
- stream: resetStreamState(),
115
- };
116
- case 'STREAM_DELTA':
117
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'streaming') return domain;
118
- return {
119
- ...updateDomainMetadata(domain, source),
120
- currentTurnId: event.turnId,
121
- turnState: 'streaming',
122
- stream: {
123
- accumulated: event.accumulated,
124
- reasoningAccumulated: `${domain.stream.reasoningAccumulated}${event.reasoning ?? ''}`,
125
- partialToolPreview: formatPartialToolPreview(event.toolCalls),
126
- deltaCount: domain.stream.deltaCount + 1,
127
- firstDeltaAt: domain.stream.firstDeltaAt ?? now(),
128
- lastDeltaAt: now(),
129
- },
130
- };
131
- case 'STREAM_END':
132
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'streaming') return domain;
133
- return {
134
- ...updateDomainMetadata(domain, source),
135
- currentTurnId: event.turnId,
136
- stream: {
137
- ...domain.stream,
138
- partialToolPreview: undefined,
139
- },
140
- };
141
- case 'LLM_RESPONSE_RECEIVED':
142
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) return domain;
143
- return {
144
- ...updateDomainMetadata(domain, source),
145
- currentTurnId: event.turnId,
146
- };
147
- case 'TOOL_BATCH_READY':
148
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'tool_dispatch') return domain;
149
- return {
150
- ...updateDomainMetadata(domain, source),
151
- currentTurnId: event.turnId,
152
- turnState: 'post_hooks',
153
- };
154
- case 'TOOLS_DONE':
155
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'tool_dispatch') return domain;
156
- return {
157
- ...updateDomainMetadata(domain, source),
158
- currentTurnId: event.turnId,
159
- turnState: 'post_hooks',
160
- };
161
- case 'POST_HOOKS_DONE':
162
- if (!isCurrentTurnEvent(domain, event.turnId) || domain.turnState !== 'post_hooks') return domain;
163
- return {
164
- ...updateDomainMetadata(domain, source),
165
- currentTurnId: event.turnId,
166
- turnState: 'post_hooks',
167
- };
168
- case 'TOOL_RECONCILED':
169
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) return domain;
170
- return {
171
- ...updateDomainMetadata(domain, source),
172
- currentTurnId: event.turnId,
173
- lastToolReconciliation: {
174
- count: event.count,
175
- callIds: [...event.callIds],
176
- toolNames: [...event.toolNames],
177
- reason: event.reason,
178
- timestamp: event.timestamp,
179
- isMalformed: event.isMalformed ?? false,
180
- },
181
- };
182
- case 'TURN_COMPLETED':
183
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) return domain;
184
- return {
185
- ...updateDomainMetadata(domain, source),
186
- currentTurnId: event.turnId,
187
- turnState: 'completed',
188
- turnEndedAt: now(),
189
- totalTurns: domain.totalTurns + 1,
190
- lastTurnResponse: event.response,
191
- lastTurnStopReason: event.stopReason,
192
- stream: {
193
- ...domain.stream,
194
- partialToolPreview: undefined,
195
- },
196
- };
197
- case 'TURN_ERROR':
198
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) return domain;
199
- return {
200
- ...updateDomainMetadata(domain, source),
201
- currentTurnId: event.turnId,
202
- turnState: 'failed',
203
- turnEndedAt: now(),
204
- lastTurnError: event.error,
205
- lastTurnStopReason: event.stopReason,
206
- stream: {
207
- ...domain.stream,
208
- partialToolPreview: undefined,
209
- },
210
- };
211
- case 'TURN_CANCEL':
212
- if (!isCurrentTurnEvent(domain, event.turnId) || isTerminalTurnState(domain.turnState)) return domain;
213
- return {
214
- ...updateDomainMetadata(domain, source),
215
- currentTurnId: event.turnId,
216
- turnState: 'cancelled',
217
- turnEndedAt: now(),
218
- lastTurnError: event.reason,
219
- lastTurnStopReason: event.stopReason,
220
- stream: {
221
- ...domain.stream,
222
- partialToolPreview: undefined,
223
- },
224
- };
225
- default:
226
- return updateDomainMetadata(domain, source);
227
- }
228
- }