@mobileai/react-native 0.9.16 → 0.9.18
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.
- package/README.md +2 -2
- package/package.json +5 -8
- package/lib/module/__cli_tmp__.js.map +0 -1
- package/lib/module/components/AIAgent.js.map +0 -1
- package/lib/module/components/AIZone.js.map +0 -1
- package/lib/module/components/AgentChatBar.js.map +0 -1
- package/lib/module/components/AgentErrorBoundary.js.map +0 -1
- package/lib/module/components/AgentOverlay.js.map +0 -1
- package/lib/module/components/DiscoveryTooltip.js.map +0 -1
- package/lib/module/components/HighlightOverlay.js.map +0 -1
- package/lib/module/components/Icons.js.map +0 -1
- package/lib/module/components/ProactiveHint.js.map +0 -1
- package/lib/module/components/cards/InfoCard.js.map +0 -1
- package/lib/module/components/cards/ReviewSummary.js.map +0 -1
- package/lib/module/config/endpoints.js.map +0 -1
- package/lib/module/core/ActionRegistry.js.map +0 -1
- package/lib/module/core/AgentRuntime.js.map +0 -1
- package/lib/module/core/FiberTreeWalker.js.map +0 -1
- package/lib/module/core/IdleDetector.js.map +0 -1
- package/lib/module/core/MCPBridge.js.map +0 -1
- package/lib/module/core/ScreenDehydrator.js.map +0 -1
- package/lib/module/core/ZoneRegistry.js.map +0 -1
- package/lib/module/core/systemPrompt.js.map +0 -1
- package/lib/module/core/types.js.map +0 -1
- package/lib/module/hooks/useAction.js.map +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/plugin/withAppIntents.js.map +0 -1
- package/lib/module/providers/GeminiProvider.js.map +0 -1
- package/lib/module/providers/OpenAIProvider.js.map +0 -1
- package/lib/module/providers/ProviderFactory.js.map +0 -1
- package/lib/module/services/AudioInputService.js.map +0 -1
- package/lib/module/services/AudioOutputService.js.map +0 -1
- package/lib/module/services/KnowledgeBaseService.js.map +0 -1
- package/lib/module/services/VoiceService.js.map +0 -1
- package/lib/module/services/flags/FlagService.js.map +0 -1
- package/lib/module/services/telemetry/MobileAI.js.map +0 -1
- package/lib/module/services/telemetry/PiiScrubber.js.map +0 -1
- package/lib/module/services/telemetry/TelemetryService.js.map +0 -1
- package/lib/module/services/telemetry/TouchAutoCapture.js.map +0 -1
- package/lib/module/services/telemetry/device.js.map +0 -1
- package/lib/module/services/telemetry/deviceMetadata.js.map +0 -1
- package/lib/module/services/telemetry/index.js.map +0 -1
- package/lib/module/services/telemetry/types.js.map +0 -1
- package/lib/module/support/CSATSurvey.js.map +0 -1
- package/lib/module/support/EscalationEventSource.js.map +0 -1
- package/lib/module/support/EscalationSocket.js.map +0 -1
- package/lib/module/support/SupportChatModal.js.map +0 -1
- package/lib/module/support/SupportGreeting.js.map +0 -1
- package/lib/module/support/TicketStore.js.map +0 -1
- package/lib/module/support/escalateTool.js.map +0 -1
- package/lib/module/support/index.js.map +0 -1
- package/lib/module/support/supportPrompt.js.map +0 -1
- package/lib/module/support/types.js.map +0 -1
- package/lib/module/tools/datePickerTool.js.map +0 -1
- package/lib/module/tools/guideTool.js.map +0 -1
- package/lib/module/tools/index.js.map +0 -1
- package/lib/module/tools/keyboardTool.js.map +0 -1
- package/lib/module/tools/longPressTool.js.map +0 -1
- package/lib/module/tools/pickerTool.js.map +0 -1
- package/lib/module/tools/restoreTool.js.map +0 -1
- package/lib/module/tools/scrollTool.js.map +0 -1
- package/lib/module/tools/simplifyTool.js.map +0 -1
- package/lib/module/tools/sliderTool.js.map +0 -1
- package/lib/module/tools/tapTool.js.map +0 -1
- package/lib/module/tools/typeTool.js.map +0 -1
- package/lib/module/tools/types.js.map +0 -1
- package/lib/module/types/jsx.d.js.map +0 -1
- package/lib/module/utils/audioUtils.js.map +0 -1
- package/lib/module/utils/logger.js.map +0 -1
- package/lib/typescript/babel.config.d.ts.map +0 -1
- package/lib/typescript/bin/generate-map.d.cts.map +0 -1
- package/lib/typescript/eslint.config.d.mts.map +0 -1
- package/lib/typescript/generate-map.d.ts.map +0 -1
- package/lib/typescript/src/__cli_tmp__.d.ts.map +0 -1
- package/lib/typescript/src/components/AIAgent.d.ts.map +0 -1
- package/lib/typescript/src/components/AIZone.d.ts.map +0 -1
- package/lib/typescript/src/components/AgentChatBar.d.ts.map +0 -1
- package/lib/typescript/src/components/AgentErrorBoundary.d.ts.map +0 -1
- package/lib/typescript/src/components/AgentOverlay.d.ts.map +0 -1
- package/lib/typescript/src/components/DiscoveryTooltip.d.ts.map +0 -1
- package/lib/typescript/src/components/HighlightOverlay.d.ts.map +0 -1
- package/lib/typescript/src/components/Icons.d.ts.map +0 -1
- package/lib/typescript/src/components/ProactiveHint.d.ts.map +0 -1
- package/lib/typescript/src/components/cards/InfoCard.d.ts.map +0 -1
- package/lib/typescript/src/components/cards/ReviewSummary.d.ts.map +0 -1
- package/lib/typescript/src/config/endpoints.d.ts.map +0 -1
- package/lib/typescript/src/core/ActionRegistry.d.ts.map +0 -1
- package/lib/typescript/src/core/AgentRuntime.d.ts.map +0 -1
- package/lib/typescript/src/core/FiberTreeWalker.d.ts.map +0 -1
- package/lib/typescript/src/core/IdleDetector.d.ts.map +0 -1
- package/lib/typescript/src/core/MCPBridge.d.ts.map +0 -1
- package/lib/typescript/src/core/ScreenDehydrator.d.ts.map +0 -1
- package/lib/typescript/src/core/ZoneRegistry.d.ts.map +0 -1
- package/lib/typescript/src/core/systemPrompt.d.ts.map +0 -1
- package/lib/typescript/src/core/types.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useAction.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/lib/typescript/src/plugin/withAppIntents.d.ts.map +0 -1
- package/lib/typescript/src/providers/GeminiProvider.d.ts.map +0 -1
- package/lib/typescript/src/providers/OpenAIProvider.d.ts.map +0 -1
- package/lib/typescript/src/providers/ProviderFactory.d.ts.map +0 -1
- package/lib/typescript/src/services/AudioInputService.d.ts.map +0 -1
- package/lib/typescript/src/services/AudioOutputService.d.ts.map +0 -1
- package/lib/typescript/src/services/KnowledgeBaseService.d.ts.map +0 -1
- package/lib/typescript/src/services/VoiceService.d.ts.map +0 -1
- package/lib/typescript/src/services/flags/FlagService.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/MobileAI.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/PiiScrubber.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/TelemetryService.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/TouchAutoCapture.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/device.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/deviceMetadata.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/index.d.ts.map +0 -1
- package/lib/typescript/src/services/telemetry/types.d.ts.map +0 -1
- package/lib/typescript/src/support/CSATSurvey.d.ts.map +0 -1
- package/lib/typescript/src/support/EscalationEventSource.d.ts.map +0 -1
- package/lib/typescript/src/support/EscalationSocket.d.ts.map +0 -1
- package/lib/typescript/src/support/SupportChatModal.d.ts.map +0 -1
- package/lib/typescript/src/support/SupportGreeting.d.ts.map +0 -1
- package/lib/typescript/src/support/TicketStore.d.ts.map +0 -1
- package/lib/typescript/src/support/escalateTool.d.ts.map +0 -1
- package/lib/typescript/src/support/index.d.ts.map +0 -1
- package/lib/typescript/src/support/supportPrompt.d.ts.map +0 -1
- package/lib/typescript/src/support/types.d.ts.map +0 -1
- package/lib/typescript/src/tools/datePickerTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/guideTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/index.d.ts.map +0 -1
- package/lib/typescript/src/tools/keyboardTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/longPressTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/pickerTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/restoreTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/scrollTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/simplifyTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/sliderTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/tapTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/typeTool.d.ts.map +0 -1
- package/lib/typescript/src/tools/types.d.ts.map +0 -1
- package/lib/typescript/src/utils/audioUtils.d.ts.map +0 -1
- package/lib/typescript/src/utils/logger.d.ts.map +0 -1
- package/src/__cli_tmp__.tsx +0 -9
- package/src/cli/analyzers/chain-analyzer.ts +0 -183
- package/src/cli/extractors/ai-extractor.ts +0 -6
- package/src/cli/extractors/ast-extractor.ts +0 -551
- package/src/cli/generate-intents.ts +0 -140
- package/src/cli/generate-map.ts +0 -121
- package/src/cli/generate-swift.ts +0 -116
- package/src/cli/scanners/expo-scanner.ts +0 -203
- package/src/cli/scanners/rn-scanner.ts +0 -445
- package/src/components/AIAgent.tsx +0 -1716
- package/src/components/AIZone.tsx +0 -147
- package/src/components/AgentChatBar.tsx +0 -1143
- package/src/components/AgentErrorBoundary.tsx +0 -78
- package/src/components/AgentOverlay.tsx +0 -73
- package/src/components/DiscoveryTooltip.tsx +0 -148
- package/src/components/HighlightOverlay.tsx +0 -136
- package/src/components/Icons.tsx +0 -253
- package/src/components/ProactiveHint.tsx +0 -145
- package/src/components/cards/InfoCard.tsx +0 -58
- package/src/components/cards/ReviewSummary.tsx +0 -76
- package/src/config/endpoints.ts +0 -22
- package/src/core/ActionRegistry.ts +0 -105
- package/src/core/AgentRuntime.ts +0 -1471
- package/src/core/FiberTreeWalker.ts +0 -930
- package/src/core/IdleDetector.ts +0 -72
- package/src/core/MCPBridge.ts +0 -163
- package/src/core/ScreenDehydrator.ts +0 -53
- package/src/core/ZoneRegistry.ts +0 -44
- package/src/core/systemPrompt.ts +0 -431
- package/src/core/types.ts +0 -521
- package/src/hooks/useAction.ts +0 -182
- package/src/index.ts +0 -83
- package/src/plugin/withAppIntents.ts +0 -98
- package/src/providers/GeminiProvider.ts +0 -357
- package/src/providers/OpenAIProvider.ts +0 -379
- package/src/providers/ProviderFactory.ts +0 -36
- package/src/services/AudioInputService.ts +0 -226
- package/src/services/AudioOutputService.ts +0 -236
- package/src/services/KnowledgeBaseService.ts +0 -156
- package/src/services/VoiceService.ts +0 -451
- package/src/services/flags/FlagService.ts +0 -137
- package/src/services/telemetry/MobileAI.ts +0 -66
- package/src/services/telemetry/PiiScrubber.ts +0 -17
- package/src/services/telemetry/TelemetryService.ts +0 -323
- package/src/services/telemetry/TouchAutoCapture.ts +0 -165
- package/src/services/telemetry/device.ts +0 -93
- package/src/services/telemetry/deviceMetadata.ts +0 -13
- package/src/services/telemetry/index.ts +0 -13
- package/src/services/telemetry/types.ts +0 -75
- package/src/support/CSATSurvey.tsx +0 -304
- package/src/support/EscalationEventSource.ts +0 -190
- package/src/support/EscalationSocket.ts +0 -152
- package/src/support/SupportChatModal.tsx +0 -563
- package/src/support/SupportGreeting.tsx +0 -161
- package/src/support/TicketStore.ts +0 -100
- package/src/support/escalateTool.ts +0 -174
- package/src/support/index.ts +0 -29
- package/src/support/supportPrompt.ts +0 -55
- package/src/support/types.ts +0 -155
- package/src/tools/datePickerTool.ts +0 -60
- package/src/tools/guideTool.ts +0 -76
- package/src/tools/index.ts +0 -20
- package/src/tools/keyboardTool.ts +0 -30
- package/src/tools/longPressTool.ts +0 -61
- package/src/tools/pickerTool.ts +0 -115
- package/src/tools/restoreTool.ts +0 -33
- package/src/tools/scrollTool.ts +0 -156
- package/src/tools/simplifyTool.ts +0 -33
- package/src/tools/sliderTool.ts +0 -65
- package/src/tools/tapTool.ts +0 -93
- package/src/tools/typeTool.ts +0 -113
- package/src/tools/types.ts +0 -58
- package/src/types/jsx.d.ts +0 -20
- package/src/utils/audioUtils.ts +0 -54
- package/src/utils/logger.ts +0 -38
package/src/core/types.ts
DELETED
|
@@ -1,521 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core types for the React Native AI SDK.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// ─── Agent Modes ──────────────────────────────────────────────
|
|
6
|
-
|
|
7
|
-
export type AgentMode = 'text' | 'voice' | 'human';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Controls how the agent handles irreversible actions.
|
|
11
|
-
* 'copilot' (default): AI pauses before final commit actions (place order, delete, submit).
|
|
12
|
-
* The prompt instructs the AI to ask_user before the final irreversible step.
|
|
13
|
-
* Elements with aiConfirm={true} also trigger a code-level confirmation gate.
|
|
14
|
-
* 'autopilot': Full autonomy — all actions execute without confirmation.
|
|
15
|
-
*/
|
|
16
|
-
export type InteractionMode = 'copilot' | 'autopilot';
|
|
17
|
-
|
|
18
|
-
// ─── Provider Names ──────────────────────────────────────────
|
|
19
|
-
|
|
20
|
-
export type AIProviderName = 'gemini' | 'openai';
|
|
21
|
-
|
|
22
|
-
// ─── Interactive Element (discovered from Fiber tree) ─────────
|
|
23
|
-
|
|
24
|
-
export type ElementType = 'pressable' | 'text-input' | 'switch' | 'scrollable'
|
|
25
|
-
| 'slider' | 'picker' | 'date-picker';
|
|
26
|
-
|
|
27
|
-
export interface InteractiveElement {
|
|
28
|
-
/** Unique index assigned during tree walk */
|
|
29
|
-
index: number;
|
|
30
|
-
/** Element type */
|
|
31
|
-
type: ElementType;
|
|
32
|
-
/** Human-readable label (extracted from Text children or accessibilityLabel) */
|
|
33
|
-
label: string;
|
|
34
|
-
/** Declarative AI priority explicitly set by the developer */
|
|
35
|
-
aiPriority?: 'high' | 'low';
|
|
36
|
-
/** The nearest enclosing AIZone ID (if any) */
|
|
37
|
-
zoneId?: string;
|
|
38
|
-
/** Reference to the Fiber node for execution */
|
|
39
|
-
fiberNode: any;
|
|
40
|
-
/**
|
|
41
|
-
* Props snapshot from the fiber node.
|
|
42
|
-
* Record<string, any> because RN components have diverse props:
|
|
43
|
-
* - Pressable: onPress, onLongPress
|
|
44
|
-
* - Slider: onValueChange, onSlidingComplete, minimumValue, maximumValue
|
|
45
|
-
* - Picker: onValueChange, items, selectedValue
|
|
46
|
-
* - DatePicker: onChange, onDateChange, mode
|
|
47
|
-
* - Switch: onValueChange, value
|
|
48
|
-
*/
|
|
49
|
-
props: Record<string, any>;
|
|
50
|
-
/**
|
|
51
|
-
* If true, AI interaction with this element requires user confirmation (copilot safety net).
|
|
52
|
-
* Set automatically by the FiberTreeWalker when the element has aiConfirm={true} prop.
|
|
53
|
-
*/
|
|
54
|
-
requiresConfirmation?: boolean;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// ─── Dehydrated Screen State ──────────────────────────────────
|
|
58
|
-
|
|
59
|
-
export interface DehydratedScreen {
|
|
60
|
-
/** Current screen name (from navigation state) */
|
|
61
|
-
screenName: string;
|
|
62
|
-
/** All available screen names (from routeNames) */
|
|
63
|
-
availableScreens: string[];
|
|
64
|
-
/** Indexed interactive elements as text */
|
|
65
|
-
elementsText: string;
|
|
66
|
-
/** Raw elements array */
|
|
67
|
-
elements: InteractiveElement[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// ─── Screen Map (generated by CLI) ───────────────────────────
|
|
71
|
-
|
|
72
|
-
export interface ScreenMapEntry {
|
|
73
|
-
title?: string;
|
|
74
|
-
description: string;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface ScreenMap {
|
|
78
|
-
generatedAt: string;
|
|
79
|
-
framework: 'expo-router' | 'react-navigation';
|
|
80
|
-
screens: Record<string, ScreenMapEntry>;
|
|
81
|
-
chains?: string[][];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// ─── Agent Types ──────────────────────────────────────────────
|
|
85
|
-
|
|
86
|
-
export interface AgentStep {
|
|
87
|
-
stepIndex: number;
|
|
88
|
-
reflection: AgentReasoning;
|
|
89
|
-
action: {
|
|
90
|
-
name: string;
|
|
91
|
-
input: Record<string, any>;
|
|
92
|
-
output: string;
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface AgentConfig {
|
|
97
|
-
/**
|
|
98
|
-
* Which LLM provider to use for text mode.
|
|
99
|
-
* Default: 'gemini'
|
|
100
|
-
*/
|
|
101
|
-
provider?: AIProviderName;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* API key (for prototyping only).
|
|
105
|
-
* Do not use in production. Use proxyUrl instead.
|
|
106
|
-
*/
|
|
107
|
-
apiKey?: string;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The URL of your secure backend proxy (for production).
|
|
111
|
-
* Routes all Gemini API requests through your server.
|
|
112
|
-
*/
|
|
113
|
-
proxyUrl?: string;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Optional headers to send to your proxyUrl (e.g., auth tokens).
|
|
117
|
-
*/
|
|
118
|
-
proxyHeaders?: Record<string, string>;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Optional specific URL for Voice Mode (WebSockets).
|
|
122
|
-
* Useful if you use Serverless for text, but need a dedicated server for voice.
|
|
123
|
-
*/
|
|
124
|
-
voiceProxyUrl?: string;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Optional specific headers for voiceProxyUrl.
|
|
128
|
-
*/
|
|
129
|
-
voiceProxyHeaders?: Record<string, string>;
|
|
130
|
-
|
|
131
|
-
model?: string;
|
|
132
|
-
|
|
133
|
-
/** Maximum steps per task */
|
|
134
|
-
maxSteps?: number;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Controls how the agent handles irreversible actions.
|
|
138
|
-
* 'copilot' (default): AI pauses before final commit actions.
|
|
139
|
-
* 'autopilot': Full autonomy, no pauses.
|
|
140
|
-
*/
|
|
141
|
-
interactionMode?: InteractionMode;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* MCP server mode — controls whether external agents can discover and invoke actions.
|
|
145
|
-
* 'auto' (default): enabled in __DEV__, disabled in production
|
|
146
|
-
* 'enabled': always on (opt-in for production)
|
|
147
|
-
* 'disabled': always off
|
|
148
|
-
*/
|
|
149
|
-
mcpServerMode?: 'auto' | 'enabled' | 'disabled';
|
|
150
|
-
|
|
151
|
-
// ─── Element Gating ──
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* React refs of elements the AI must NOT interact with.
|
|
155
|
-
* Refs of elements the AI must NOT interact with.
|
|
156
|
-
* The Fiber walker skips any node whose ref matches one in this list.
|
|
157
|
-
*/
|
|
158
|
-
interactiveBlacklist?: React.RefObject<any>[];
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* If set, the AI can ONLY interact with these elements.
|
|
162
|
-
* If set, AI can ONLY interact with these elements.
|
|
163
|
-
*/
|
|
164
|
-
interactiveWhitelist?: React.RefObject<any>[];
|
|
165
|
-
|
|
166
|
-
// ─── Lifecycle Hooks ───────
|
|
167
|
-
|
|
168
|
-
/** Called before each agent step. */
|
|
169
|
-
onBeforeStep?: (stepCount: number) => Promise<void> | void;
|
|
170
|
-
|
|
171
|
-
/** Called after each agent step with full history. */
|
|
172
|
-
onAfterStep?: (history: AgentStep[]) => Promise<void> | void;
|
|
173
|
-
|
|
174
|
-
/** Called before task execution starts. */
|
|
175
|
-
onBeforeTask?: () => Promise<void> | void;
|
|
176
|
-
|
|
177
|
-
/** Called after task completes (success or failure). */
|
|
178
|
-
onAfterTask?: (result: ExecutionResult) => Promise<void> | void;
|
|
179
|
-
|
|
180
|
-
// ─── Content Masking ──────────
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Transform dehydrated screen text before sending to LLM.
|
|
184
|
-
* Use to mask sensitive data (credit cards, PII, etc).
|
|
185
|
-
* Transform screen content before the LLM sees it (for data masking).
|
|
186
|
-
*/
|
|
187
|
-
transformScreenContent?: (content: string) => Promise<string> | string;
|
|
188
|
-
|
|
189
|
-
// ─── Custom Tools ─────────────────────
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Override or remove built-in tools.
|
|
193
|
-
* Set tool to `null` to remove it (e.g., `{ navigate: null }`).
|
|
194
|
-
* Override or remove built-in tools (null = remove).
|
|
195
|
-
*/
|
|
196
|
-
customTools?: Record<string, ToolDefinition | null>;
|
|
197
|
-
|
|
198
|
-
// ─── Instructions ────────────────────
|
|
199
|
-
|
|
200
|
-
/** Instructions to guide the agent's behavior. */
|
|
201
|
-
instructions?: {
|
|
202
|
-
/** Global system-level instructions, applied to all tasks. */
|
|
203
|
-
system?: string;
|
|
204
|
-
/**
|
|
205
|
-
* Dynamic per-screen instructions callback.
|
|
206
|
-
* Called before each step to get instructions for the current screen.
|
|
207
|
-
* Per-screen instructions callback.
|
|
208
|
-
*/
|
|
209
|
-
getScreenInstructions?: (screenName: string) => string | undefined | null;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Enable or disable UI control tools (tap, type, navigate, ask_user, capture_screenshot).
|
|
214
|
-
* When false, the AI operates as a knowledge-only assistant — it can read the screen
|
|
215
|
-
* and answer questions via query_knowledge, but cannot interact with UI elements.
|
|
216
|
-
* Default: true
|
|
217
|
-
*/
|
|
218
|
-
enableUIControl?: boolean;
|
|
219
|
-
|
|
220
|
-
/** Delay between steps in ms. */
|
|
221
|
-
stepDelay?: number;
|
|
222
|
-
|
|
223
|
-
// ─── Screen Map ───────────────────────────────────────────────────────────
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Pre-generated screen map from `npx react-native-ai-agent generate-map`.
|
|
227
|
-
* Gives the AI knowledge of all screens, their content, and navigation chains.
|
|
228
|
-
* Without this, the AI only sees the currently rendered screen.
|
|
229
|
-
*/
|
|
230
|
-
screenMap?: ScreenMap;
|
|
231
|
-
|
|
232
|
-
// ─── Status Updates ──────────────────────────────────────────────────────
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Called with a human-readable status string at each step.
|
|
236
|
-
* Use this to show dynamic loading text (e.g., "Tapping 'Add'...").
|
|
237
|
-
*/
|
|
238
|
-
onStatusUpdate?: (status: string) => void;
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Called after each step with token usage data.
|
|
242
|
-
* Use to track cost, enforce budgets, or display usage to the user.
|
|
243
|
-
*/
|
|
244
|
-
onTokenUsage?: (usage: TokenUsage) => void;
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Callback for when agent needs user input (ask_user tool).
|
|
248
|
-
* The agent loop blocks until the user responds.
|
|
249
|
-
* If not set, ask_user tool will break the loop (legacy behavior).
|
|
250
|
-
* @example onAskUser: (q) => new Promise(resolve => showPrompt(q, resolve))
|
|
251
|
-
*/
|
|
252
|
-
onAskUser?: (question: string) => Promise<string>;
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Called immediately before and after each agent tool execution.
|
|
256
|
-
* Used by AIAgent to toggle isAgentActing on TelemetryService so that
|
|
257
|
-
* AI-driven taps are not double-counted as user interactions.
|
|
258
|
-
* @param active - true = agent is acting, false = agent finished acting
|
|
259
|
-
*/
|
|
260
|
-
onToolExecute?: (active: boolean) => void;
|
|
261
|
-
|
|
262
|
-
// ─── Expo Router Support ─────────────────────────────────────────────────
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Expo Router instance (from useRouter()).
|
|
266
|
-
* When provided, the navigate tool uses router.push('/path') instead of navRef.navigate().
|
|
267
|
-
*/
|
|
268
|
-
router?: {
|
|
269
|
-
push: (href: string) => void;
|
|
270
|
-
replace: (href: string) => void;
|
|
271
|
-
back: () => void;
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Current pathname from Expo Router (from usePathname()).
|
|
276
|
-
* Used to determine the current screen when using Expo Router.
|
|
277
|
-
*/
|
|
278
|
-
pathname?: string;
|
|
279
|
-
|
|
280
|
-
// ─── Knowledge Base ────────────────────────────────────────────────────────
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Domain knowledge the AI can query via the query_knowledge tool.
|
|
284
|
-
* Pass a static array of KnowledgeEntry[] (SDK handles keyword matching),
|
|
285
|
-
* or a KnowledgeRetriever with a custom async retrieve() function.
|
|
286
|
-
*/
|
|
287
|
-
knowledgeBase?: KnowledgeBaseConfig;
|
|
288
|
-
|
|
289
|
-
/** Max tokens for knowledge retrieval results (~4 chars per token). Default: 2000 */
|
|
290
|
-
knowledgeMaxTokens?: number;
|
|
291
|
-
|
|
292
|
-
// ─── Budget Guards ──────────────────────────────────────────────────────
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Maximum total tokens (prompt + completion) allowed per task.
|
|
296
|
-
* The agent loop auto-stops when this budget is exceeded, returning partial results.
|
|
297
|
-
*/
|
|
298
|
-
maxTokenBudget?: number;
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Maximum estimated cost (USD) allowed per task.
|
|
302
|
-
* The agent loop auto-stops when this budget is exceeded, returning partial results.
|
|
303
|
-
* Cost is estimated based on the provider's pricing (see provider source for rates).
|
|
304
|
-
*/
|
|
305
|
-
maxCostUSD?: number;
|
|
306
|
-
|
|
307
|
-
// ─── MCP Bridge Integration ──────────────────────────────────────────────
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Optional URL of the companion Node.js MCP server bridge (e.g. ws://localhost:3101).
|
|
311
|
-
* If set, the SDK will connect to this server and listen for execution requests
|
|
312
|
-
* from external AI agents (like OpenClaw, Claude Desktop, etc).
|
|
313
|
-
*/
|
|
314
|
-
mcpServerUrl?: string;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export interface ExecutionResult {
|
|
318
|
-
success: boolean;
|
|
319
|
-
message: string;
|
|
320
|
-
steps: AgentStep[];
|
|
321
|
-
/** Accumulated token usage for the entire task */
|
|
322
|
-
tokenUsage?: TokenUsage;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// ─── Tool Types ───────────────────────────────────────────────
|
|
326
|
-
|
|
327
|
-
export interface ToolDefinition {
|
|
328
|
-
name: string;
|
|
329
|
-
description: string;
|
|
330
|
-
parameters: Record<string, ToolParam>;
|
|
331
|
-
execute: (args: Record<string, any>) => Promise<string>;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
export interface ToolParam {
|
|
335
|
-
type: 'string' | 'number' | 'boolean';
|
|
336
|
-
description: string;
|
|
337
|
-
required?: boolean;
|
|
338
|
-
enum?: string[];
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// ─── Action (optional useAction hook) ─────────────────────────
|
|
342
|
-
|
|
343
|
-
export interface ActionParameterDef {
|
|
344
|
-
/** The primitive type of the parameter. Maps to MCP schemas and native iOS/Android types. */
|
|
345
|
-
type: 'string' | 'number' | 'boolean';
|
|
346
|
-
/** A clear description of what the parameter is for (read by the AI). */
|
|
347
|
-
description: string;
|
|
348
|
-
/** Whether the AI must provide this parameter. Defaults to true. */
|
|
349
|
-
required?: boolean;
|
|
350
|
-
/** If provided, the AI is restricted to these specific string values. */
|
|
351
|
-
enum?: string[];
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
export interface ActionDefinition {
|
|
355
|
-
name: string;
|
|
356
|
-
description: string;
|
|
357
|
-
parameters: Record<string, string | ActionParameterDef>;
|
|
358
|
-
handler: (args: Record<string, any>) => any;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// ─── Knowledge Base ───────────────────────────────────────────
|
|
362
|
-
|
|
363
|
-
/** A single knowledge entry the AI can retrieve. */
|
|
364
|
-
export interface KnowledgeEntry {
|
|
365
|
-
/** Unique identifier */
|
|
366
|
-
id: string;
|
|
367
|
-
/** Human-readable title (also used for keyword matching) */
|
|
368
|
-
title: string;
|
|
369
|
-
/** The knowledge text content */
|
|
370
|
-
content: string;
|
|
371
|
-
/** Optional tags for keyword matching (e.g., ['refund', 'policy']) */
|
|
372
|
-
tags?: string[];
|
|
373
|
-
/** Optional: only surface this entry on these screens */
|
|
374
|
-
screens?: string[];
|
|
375
|
-
/** Priority 0-10 — higher = preferred when multiple match (default: 5) */
|
|
376
|
-
priority?: number;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/** Async retriever function — consumer brings their own retrieval logic. */
|
|
380
|
-
export interface KnowledgeRetriever {
|
|
381
|
-
retrieve: (query: string, screenName: string) => Promise<KnowledgeEntry[]>;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Knowledge base configuration — accepts either:
|
|
386
|
-
* - A static array of KnowledgeEntry[] (SDK handles keyword matching)
|
|
387
|
-
* - A KnowledgeRetriever object with a custom retrieve() function
|
|
388
|
-
*/
|
|
389
|
-
export type KnowledgeBaseConfig = KnowledgeEntry[] | KnowledgeRetriever;
|
|
390
|
-
|
|
391
|
-
// ─── Chat Messages ───────────────────────────────────────────
|
|
392
|
-
|
|
393
|
-
/** A single message in the conversation history. */
|
|
394
|
-
export interface AIMessage {
|
|
395
|
-
/** Unique message ID */
|
|
396
|
-
id: string;
|
|
397
|
-
/** Who sent this message */
|
|
398
|
-
role: 'user' | 'assistant';
|
|
399
|
-
/** Text content */
|
|
400
|
-
content: string;
|
|
401
|
-
/** Unix timestamp (ms) */
|
|
402
|
-
timestamp: number;
|
|
403
|
-
/** Attached execution result (assistant messages only) */
|
|
404
|
-
result?: ExecutionResult;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// ─── Chat Bar Theme ──────────────────────────────────────────
|
|
408
|
-
|
|
409
|
-
/** Color customization for the floating chat bar / popup. */
|
|
410
|
-
export interface ChatBarTheme {
|
|
411
|
-
/** Primary accent color — FAB background, send button, active tab indicator.
|
|
412
|
-
* Default: '#1a1a2e' */
|
|
413
|
-
primaryColor?: string;
|
|
414
|
-
/** Expanded popup background color. Default: 'rgba(26, 26, 46, 0.95)' */
|
|
415
|
-
backgroundColor?: string;
|
|
416
|
-
/** Text and icon color. Default: '#ffffff' */
|
|
417
|
-
textColor?: string;
|
|
418
|
-
/** User input field background. Default: 'rgba(255, 255, 255, 0.1)' */
|
|
419
|
-
inputBackgroundColor?: string;
|
|
420
|
-
/** Success result bubble background. Default: 'rgba(40, 167, 69, 0.2)' */
|
|
421
|
-
successColor?: string;
|
|
422
|
-
/** Error result bubble background. Default: 'rgba(220, 53, 69, 0.2)' */
|
|
423
|
-
errorColor?: string;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
// ─── Provider Interface ──────────────────────────────────────
|
|
427
|
-
|
|
428
|
-
/** Structured reasoning returned per step via the agent_step tool. */
|
|
429
|
-
export interface AgentReasoning {
|
|
430
|
-
/** Assessment of whether the previous action succeeded or failed. */
|
|
431
|
-
previousGoalEval: string;
|
|
432
|
-
/** What to remember for future steps (progress, items found, etc). */
|
|
433
|
-
memory: string;
|
|
434
|
-
/** The immediate next goal and why. */
|
|
435
|
-
plan: string;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// ─── Token Usage ──────────────────────────────────────────────
|
|
439
|
-
|
|
440
|
-
/** Token usage metrics for cost tracking. */
|
|
441
|
-
export interface TokenUsage {
|
|
442
|
-
/** Tokens in the input prompt */
|
|
443
|
-
promptTokens: number;
|
|
444
|
-
/** Tokens generated by the model */
|
|
445
|
-
completionTokens: number;
|
|
446
|
-
/** Total tokens (prompt + completion) */
|
|
447
|
-
totalTokens: number;
|
|
448
|
-
/** Estimated cost in USD (based on model pricing) */
|
|
449
|
-
estimatedCostUSD: number;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/** Result from the AI provider's generateContent call. */
|
|
453
|
-
export interface ProviderResult {
|
|
454
|
-
/** Extracted action tool call (action_name + params). */
|
|
455
|
-
toolCalls: Array<{ name: string; args: Record<string, any> }>;
|
|
456
|
-
/** Structured reasoning from MacroTool (evaluation, memory, next_goal). */
|
|
457
|
-
reasoning: AgentReasoning;
|
|
458
|
-
/** Raw text response (if any). */
|
|
459
|
-
text?: string;
|
|
460
|
-
/** Token usage for this specific call */
|
|
461
|
-
tokenUsage?: TokenUsage;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
export interface AIProvider {
|
|
465
|
-
generateContent(
|
|
466
|
-
systemPrompt: string,
|
|
467
|
-
userMessage: string,
|
|
468
|
-
tools: ToolDefinition[],
|
|
469
|
-
history: AgentStep[],
|
|
470
|
-
/** Optional base64-encoded JPEG screenshot for vision */
|
|
471
|
-
screenshot?: string,
|
|
472
|
-
): Promise<ProviderResult>;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// ─── AI-Native UI (Pillar B) ───────────────────────────────────────────────────
|
|
476
|
-
|
|
477
|
-
/**
|
|
478
|
-
* Configuration for an AIZone declarative boundary.
|
|
479
|
-
*/
|
|
480
|
-
export interface AIZoneConfig {
|
|
481
|
-
/** Unique identifier for this zone on the current screen */
|
|
482
|
-
id: string;
|
|
483
|
-
/** Whether the AI is allowed to use guide_user() to highlight elements here */
|
|
484
|
-
allowHighlight?: boolean;
|
|
485
|
-
/** Whether the AI is allowed to inject tooltip hints here */
|
|
486
|
-
allowInjectHint?: boolean;
|
|
487
|
-
/** Whether the AI is allowed to hide children marked with aiPriority="low" */
|
|
488
|
-
allowSimplify?: boolean;
|
|
489
|
-
/** Whether the AI is allowed to inject custom cards here */
|
|
490
|
-
allowInjectCard?: boolean;
|
|
491
|
-
/**
|
|
492
|
-
* Whitelist of React component templates the AI can instantiate.
|
|
493
|
-
* Required if allowInjectCard is true.
|
|
494
|
-
* IMPORTANT: The AI receives the displayName of these components
|
|
495
|
-
* and can only produce props for them. It cannot generate JSX.
|
|
496
|
-
*/
|
|
497
|
-
templates?: React.ComponentType<any>[];
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Internal representation of a registered zone.
|
|
502
|
-
*/
|
|
503
|
-
export interface RegisteredZone extends AIZoneConfig {
|
|
504
|
-
/** React ref to the zone's container View */
|
|
505
|
-
ref: React.RefObject<any>;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
export interface ProactiveHelpConfig {
|
|
509
|
-
/** Enable proactive help (default: true) */
|
|
510
|
-
enabled?: boolean;
|
|
511
|
-
/** Time in minutes before a subtle pulse (default: 2) */
|
|
512
|
-
pulseAfterMinutes?: number;
|
|
513
|
-
/** Time in minutes before showing a help badge (default: 4) */
|
|
514
|
-
badgeAfterMinutes?: number;
|
|
515
|
-
/** Default text for the badge (default: "Need help with this screen?") */
|
|
516
|
-
badgeText?: string;
|
|
517
|
-
/** If true, dismissing the badge disables proactive help for the rest of the session (default: true) */
|
|
518
|
-
dismissForSession?: boolean;
|
|
519
|
-
/** Dynamic context suggestion generator based on current screen */
|
|
520
|
-
generateSuggestion?: (screenName: string) => string;
|
|
521
|
-
}
|
package/src/hooks/useAction.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useAction — Register non-UI actions for the AI agent.
|
|
3
|
-
* useAI — Bridge hook to read AIAgent's state (send, isLoading, status).
|
|
4
|
-
*
|
|
5
|
-
* Both hooks consume AgentContext, which is provided by <AIAgent>.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import React, { useEffect, useContext, createContext, useCallback, useRef } from 'react';
|
|
9
|
-
import type { AgentRuntime } from '../core/AgentRuntime';
|
|
10
|
-
import type { ExecutionResult, AIMessage } from '../core/types';
|
|
11
|
-
|
|
12
|
-
// ─── Context ──────────────────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
export interface AgentContextValue {
|
|
15
|
-
runtime: AgentRuntime | null;
|
|
16
|
-
/** Send a text message to the agent (same as typing in the chat bar). */
|
|
17
|
-
send: (message: string, options?: { onResult?: (result: ExecutionResult) => void }) => void;
|
|
18
|
-
/** Whether the agent is currently executing (thinking / tapping / navigating). */
|
|
19
|
-
isLoading: boolean;
|
|
20
|
-
/** Live status text — e.g. "Analyzing screen...", "Tapping element 3..." */
|
|
21
|
-
status: string;
|
|
22
|
-
/** The result of the last completed execution. */
|
|
23
|
-
lastResult: ExecutionResult | null;
|
|
24
|
-
/** The full conversation history for custom chat UIs. */
|
|
25
|
-
messages: AIMessage[];
|
|
26
|
-
/** Clear conversation history. */
|
|
27
|
-
clearMessages: () => void;
|
|
28
|
-
/** Cancel currently running task. */
|
|
29
|
-
cancel: () => void;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const DEFAULT_CONTEXT: AgentContextValue = {
|
|
33
|
-
runtime: null,
|
|
34
|
-
send: () => {},
|
|
35
|
-
isLoading: false,
|
|
36
|
-
status: '',
|
|
37
|
-
lastResult: null,
|
|
38
|
-
messages: [],
|
|
39
|
-
clearMessages: () => {},
|
|
40
|
-
cancel: () => {},
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const AgentContext = createContext<AgentContextValue>(DEFAULT_CONTEXT);
|
|
44
|
-
|
|
45
|
-
import { actionRegistry } from '../core/ActionRegistry';
|
|
46
|
-
import type { ActionParameterDef } from '../core/types';
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Register a non-UI action that the AI agent can call by name.
|
|
50
|
-
*
|
|
51
|
-
* The handler is always kept fresh via an internal ref — no stale closure bugs,
|
|
52
|
-
* even when it captures mutable state like cart contents or form values.
|
|
53
|
-
*
|
|
54
|
-
* The optional `deps` array controls when the action is *re-registered* (i.e. when
|
|
55
|
-
* `name`, `description`, or `parameters` need to change at runtime). You rarely
|
|
56
|
-
* need this — the handler is always up-to-date regardless.
|
|
57
|
-
*
|
|
58
|
-
* @example Basic (handler always fresh — no deps needed)
|
|
59
|
-
* ```tsx
|
|
60
|
-
* const { cart } = useCart();
|
|
61
|
-
* useAction('checkout', 'Place the order', {}, async () => {
|
|
62
|
-
* if (cart.length === 0) return { success: false, message: 'Cart is empty' };
|
|
63
|
-
* // cart is always current — no stale closure
|
|
64
|
-
* });
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @example Dynamic description (re-register when item count changes)
|
|
68
|
-
* ```tsx
|
|
69
|
-
* useAction(
|
|
70
|
-
* 'checkout',
|
|
71
|
-
* `Place the order (${cart.length} items in cart)`,
|
|
72
|
-
* {},
|
|
73
|
-
* handler,
|
|
74
|
-
* [cart.length], // re-register so the AI sees the updated description
|
|
75
|
-
* );
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
export function useAction(
|
|
79
|
-
name: string,
|
|
80
|
-
description: string,
|
|
81
|
-
parameters: Record<string, string | ActionParameterDef>,
|
|
82
|
-
handler: (args: Record<string, any>) => any,
|
|
83
|
-
deps?: React.DependencyList,
|
|
84
|
-
): void {
|
|
85
|
-
// Keep a ref to the latest handler so the registered action always calls
|
|
86
|
-
// the current closure — even without re-registering the action.
|
|
87
|
-
// This is the canonical React pattern for "always-fresh callbacks"
|
|
88
|
-
// (used by react-use, ahooks, TanStack Query internally).
|
|
89
|
-
const handlerRef = useRef(handler);
|
|
90
|
-
useEffect(() => {
|
|
91
|
-
handlerRef.current = handler;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// Registration effect — only re-runs when name/description/parameters change,
|
|
95
|
-
// OR when the consumer explicitly passes deps (e.g. for a dynamic description).
|
|
96
|
-
useEffect(() => {
|
|
97
|
-
actionRegistry.register({
|
|
98
|
-
name,
|
|
99
|
-
description,
|
|
100
|
-
parameters,
|
|
101
|
-
// Delegate to the ref — always calls the latest handler.
|
|
102
|
-
handler: (args) => handlerRef.current(args),
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
return () => {
|
|
106
|
-
actionRegistry.unregister(name);
|
|
107
|
-
};
|
|
108
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
109
|
-
}, deps ? [name, description, ...deps] : [name, description]);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// ─── useAI ────────────────────────────────────────────────────
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Bridge hook — reads the parent <AIAgent>'s state.
|
|
117
|
-
*
|
|
118
|
-
* Must be used inside an <AIAgent> tree.
|
|
119
|
-
*
|
|
120
|
-
* ```tsx
|
|
121
|
-
* <AIAgent showChatBar={false} apiKey="..." navRef={navRef}>
|
|
122
|
-
* <MyApp />
|
|
123
|
-
* </AIAgent>
|
|
124
|
-
*
|
|
125
|
-
* // Inside any screen:
|
|
126
|
-
* const { send, isLoading, status, lastResult } = useAI({ enableUIControl: false }); // knowledge-only for this screen
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
export function useAI(options?: {
|
|
130
|
-
/**
|
|
131
|
-
* Dynamically override the global `enableUIControl` setting.
|
|
132
|
-
* Useful to force "knowledge-only" mode for a specific screen without changing root layout props.
|
|
133
|
-
*/
|
|
134
|
-
enableUIControl?: boolean;
|
|
135
|
-
/**
|
|
136
|
-
* Override the global `onResult` callback for tasks triggered from this hook.
|
|
137
|
-
* Useful for navigating the user back to this specific screen after the AI finishes.
|
|
138
|
-
*/
|
|
139
|
-
onResult?: (result: ExecutionResult) => void;
|
|
140
|
-
}) {
|
|
141
|
-
const ctx = useContext(AgentContext);
|
|
142
|
-
|
|
143
|
-
useEffect(() => {
|
|
144
|
-
if (options?.enableUIControl !== undefined && ctx.runtime) {
|
|
145
|
-
ctx.runtime.setUIControlOverride(options.enableUIControl);
|
|
146
|
-
|
|
147
|
-
// Cleanup: revert to global config when unmounted
|
|
148
|
-
return () => {
|
|
149
|
-
ctx.runtime?.setUIControlOverride(undefined);
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
return undefined;
|
|
153
|
-
}, [options?.enableUIControl, ctx.runtime]);
|
|
154
|
-
|
|
155
|
-
// Track the latest onResult callback in a ref to keep `send`'s identity perfectly stable.
|
|
156
|
-
// This prevents infinite render loops if `send` is used as a dependency in child useEffects.
|
|
157
|
-
const onResultRef = useRef(options?.onResult);
|
|
158
|
-
useEffect(() => {
|
|
159
|
-
onResultRef.current = options?.onResult;
|
|
160
|
-
}, [options?.onResult]);
|
|
161
|
-
|
|
162
|
-
const send = useCallback((message: string) => {
|
|
163
|
-
ctx.send(message, { onResult: onResultRef.current });
|
|
164
|
-
}, [ctx]);
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
/** Send a message to the AI agent. */
|
|
168
|
-
send,
|
|
169
|
-
/** Whether the agent is currently executing. */
|
|
170
|
-
isLoading: ctx.isLoading,
|
|
171
|
-
/** Live status text (e.g. "Navigating to profile..."). */
|
|
172
|
-
status: ctx.status,
|
|
173
|
-
/** Result of the last completed execution. */
|
|
174
|
-
lastResult: ctx.lastResult,
|
|
175
|
-
/** The full conversation history. */
|
|
176
|
-
messages: ctx.messages,
|
|
177
|
-
/** Clear the conversation history. */
|
|
178
|
-
clearMessages: ctx.clearMessages,
|
|
179
|
-
/** Cancel the currently running task. The current step will complete before stopping. */
|
|
180
|
-
cancel: ctx.cancel,
|
|
181
|
-
};
|
|
182
|
-
}
|