@agentskit/chat-react-native 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AgentsKit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # @agentskit/chat-react-native
2
+
3
+ React Native application shell for an AgentsKit Chat definition. It delegates chat state,
4
+ streaming, cancellation, and native headless components to `@agentskit/react-native`.
5
+
6
+ `ChoiceListNative` renders a validated shared frame with native accessibility roles and labels.
7
+
8
+ Use `theme` for semantic tokens and `slots` for native React Native composition. `toChatNativeStyles` exposes the capability-aware style mapping. See [theming and composition](../../docs/theming-and-composition.md).
package/dist/index.cjs ADDED
@@ -0,0 +1,365 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.tsx
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AgentChatNative: () => AgentChatNative,
24
+ ChoiceListNative: () => ChoiceListNative,
25
+ StandardComponentNative: () => StandardComponentNative,
26
+ toChatNativeStyles: () => toChatNativeStyles
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_chat2 = require("@agentskit/chat");
30
+ var import_react_native2 = require("@agentskit/react-native");
31
+ var import_react2 = require("react");
32
+ var import_react_native3 = require("react-native");
33
+
34
+ // src/StandardComponent.tsx
35
+ var import_chat = require("@agentskit/chat");
36
+ var import_react = require("react");
37
+ var import_react_native = require("react-native");
38
+ var import_jsx_runtime = require("react/jsx-runtime");
39
+ var Button = ({ label, onPress, disabled = false }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Pressable, { accessibilityRole: "button", accessibilityLabel: label, disabled, onPress, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: label }) });
40
+ var StandardFormNative = (props) => {
41
+ const item = import_chat.FormPropsSchema.parse(props.frame.props);
42
+ const [values, setValues] = (0, import_react.useState)({});
43
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "none", accessibilityLabel: item.title ?? "Form", testID: "ak-form", children: [
44
+ item.title ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { accessibilityRole: "header", children: item.title }) : null,
45
+ item.fields.map((field) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { children: [
46
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: field.label }),
47
+ field.type === "checkbox" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: `${field.label}: ${values[field.id] ? "checked" : "unchecked"}`, disabled: props.disabled, onPress: () => setValues((current) => ({ ...current, [field.id]: !current[field.id] })) }) : field.type === "select" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { accessibilityRole: "radiogroup", accessibilityLabel: field.label, children: field.options?.map((option) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Pressable, { accessibilityRole: "radio", accessibilityState: { checked: values[field.id] === option.id }, disabled: props.disabled, onPress: () => setValues((current) => ({ ...current, [field.id]: option.id })), children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.Text, { children: [
48
+ values[field.id] === option.id ? "\u25CF" : "\u25CB",
49
+ " ",
50
+ option.label
51
+ ] }) }, option.id)) }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.TextInput, { accessibilityLabel: field.label, editable: !props.disabled, placeholder: field.placeholder, inputMode: field.type === "email" ? "email" : field.type === "number" ? "numeric" : "text", value: String(values[field.id] ?? ""), onChangeText: (value) => setValues((current) => ({ ...current, [field.id]: value })) })
52
+ ] }, field.id)),
53
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.submitLabel, disabled: props.disabled || !(0, import_chat.validateStandardComponentInteraction)("form", item, "submit", values), onPress: () => props.onInteract((0, import_chat.createComponentInteraction)(props.frame, props.manifest, "submit", values)) })
54
+ ] });
55
+ };
56
+ var StandardComponentNative = (props) => {
57
+ if (!(0, import_chat.resolveComponentFrame)(props.frame, props.manifest).ok || props.frame.componentKey === "choice-list") return null;
58
+ const emit = (event, value) => props.onInteract((0, import_chat.createComponentInteraction)(props.frame, props.manifest, event, value));
59
+ const key = props.frame.componentKey;
60
+ if (key === "button-group") {
61
+ const item = import_chat.ButtonGroupPropsSchema.parse(props.frame.props);
62
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "none", accessibilityLabel: item.label, testID: "ak-button-group", children: [
63
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.label }),
64
+ item.buttons.map((button) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: button.label, disabled: props.disabled || button.disabled, onPress: () => emit("select", button.id) }, button.id))
65
+ ] });
66
+ }
67
+ if (key === "form") return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(StandardFormNative, { ...props });
68
+ if (key === "confirmation") {
69
+ const item = import_chat.ConfirmationPropsSchema.parse(props.frame.props);
70
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityLabel: item.title, testID: "ak-confirmation", children: [
71
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.title }),
72
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.message }),
73
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.confirmLabel, disabled: props.disabled, onPress: () => emit("confirm") }),
74
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.cancelLabel, disabled: props.disabled, onPress: () => emit("cancel") })
75
+ ] });
76
+ }
77
+ if (key === "progress") {
78
+ const item = import_chat.ProgressPropsSchema.parse(props.frame.props);
79
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "progressbar", accessibilityLabel: item.label, accessibilityValue: { min: 0, max: 100, now: item.value }, testID: "ak-progress", children: [
80
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.Text, { children: [
81
+ item.label,
82
+ ": ",
83
+ item.value,
84
+ "%"
85
+ ] }),
86
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.status })
87
+ ] });
88
+ }
89
+ if (key === "source-list") {
90
+ const item = import_chat.SourceListPropsSchema.parse(props.frame.props);
91
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "list", accessibilityLabel: item.label, testID: "ak-source-list", children: [
92
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.label }),
93
+ item.sources.map((source) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { children: [
94
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: source.title, disabled: !source.url || props.disabled, onPress: () => emit("open", source.id) }),
95
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: source.snippet })
96
+ ] }, source.id))
97
+ ] });
98
+ }
99
+ if (key === "link-card") {
100
+ const item = import_chat.LinkCardPropsSchema.parse(props.frame.props);
101
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.Pressable, { accessibilityRole: "link", accessibilityLabel: item.label ?? item.title, disabled: props.disabled, onPress: () => emit("open", item.href), testID: "ak-link-card", children: [
102
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.title }),
103
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.description })
104
+ ] });
105
+ }
106
+ if (key === "error-notice") {
107
+ const item = import_chat.ErrorNoticePropsSchema.parse(props.frame.props);
108
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "alert", testID: "ak-error-notice", children: [
109
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.title }),
110
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.message }),
111
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.code }),
112
+ item.retryLabel ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.retryLabel, disabled: props.disabled, onPress: () => emit("retry") }) : null
113
+ ] });
114
+ }
115
+ if (key === "tool-call") {
116
+ const item = import_chat.ToolCallPropsSchema.parse(props.frame.props);
117
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "summary", accessibilityLabel: `${item.name}: ${item.status}`, testID: "ak-tool-call", children: [
118
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.name }),
119
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.status }),
120
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.arguments ? JSON.stringify(item.arguments) : "" }),
121
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.result === void 0 ? "" : JSON.stringify(item.result) })
122
+ ] });
123
+ }
124
+ if (key === "approval-request") {
125
+ const item = import_chat.ApprovalRequestPropsSchema.parse(props.frame.props);
126
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityLabel: item.title, testID: "ak-approval-request", children: [
127
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.title }),
128
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.description }),
129
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.approveLabel, disabled: props.disabled, onPress: () => emit("approve") }),
130
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: item.denyLabel, disabled: props.disabled, onPress: () => emit("deny") })
131
+ ] });
132
+ }
133
+ if (key === "table") {
134
+ const item = import_chat.TablePropsSchema.parse(props.frame.props);
135
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityRole: "summary", accessibilityLabel: item.caption, testID: "ak-table", children: [
136
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.caption }),
137
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.columns.map((column) => column.label).join(" | ") }),
138
+ item.rows.map((row, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.columns.map((column) => String(row[column.key] ?? "")).join(" | ") }, index))
139
+ ] });
140
+ }
141
+ if (key === "file-attachment") {
142
+ const item = import_chat.FileAttachmentPropsSchema.parse(props.frame.props);
143
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { accessibilityLabel: `${item.name}, ${item.mimeType}`, testID: "ak-file-attachment", children: [
144
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.name }),
145
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.mimeType }),
146
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { children: item.sizeBytes === void 0 ? "" : `${item.sizeBytes} bytes` }),
147
+ item.url ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Button, { label: "Open", disabled: props.disabled, onPress: () => emit("open", item.url) }) : null
148
+ ] });
149
+ }
150
+ return null;
151
+ };
152
+
153
+ // src/index.tsx
154
+ var import_jsx_runtime2 = require("react/jsx-runtime");
155
+ var toChatNativeStyles = (input) => {
156
+ const theme = (0, import_chat2.resolveChatTheme)(input);
157
+ const font = theme.fontFamily === "system" ? {} : { fontFamily: theme.fontFamily };
158
+ return {
159
+ root: { flex: 1, backgroundColor: theme.colors.background, padding: theme.spacing.large },
160
+ container: { backgroundColor: theme.colors.background },
161
+ userMessage: { alignSelf: "flex-end", backgroundColor: theme.colors.accent, borderRadius: theme.radius.large, padding: theme.spacing.medium },
162
+ userMessageText: { color: theme.colors.onAccent, ...font },
163
+ assistantMessage: { alignSelf: "flex-start", backgroundColor: theme.colors.surface, borderRadius: theme.radius.large, padding: theme.spacing.medium },
164
+ assistantMessageText: { color: theme.colors.text, ...font },
165
+ choiceList: { gap: theme.spacing.small, padding: theme.spacing.medium },
166
+ choice: { borderColor: theme.colors.border, borderRadius: theme.radius.medium, borderWidth: 1, padding: theme.spacing.medium },
167
+ choiceText: { color: theme.colors.text, ...font },
168
+ mutedText: { color: theme.colors.muted, ...font },
169
+ input: { backgroundColor: theme.colors.surface, borderColor: theme.colors.border, borderTopWidth: 1, padding: theme.spacing.medium },
170
+ inputText: { color: theme.colors.text, ...font },
171
+ dangerText: { color: theme.colors.danger, ...font }
172
+ };
173
+ };
174
+ var ChoiceListNative = ({ frame, manifest, onSelect, disabled = false, styles = toChatNativeStyles() }) => {
175
+ const resolved = (0, import_chat2.resolveChoiceListFrame)(frame, manifest);
176
+ if (!resolved.ok) return null;
177
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native3.View, { testID: "ak-choice-list", style: styles.choiceList, children: [
178
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: resolved.props.prompt }),
179
+ resolved.props.choices.map((choice) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
180
+ import_react_native3.Pressable,
181
+ {
182
+ accessibilityRole: "button",
183
+ disabled,
184
+ accessibilityLabel: choice.description === void 0 ? choice.label : `${choice.label}. ${choice.description}`,
185
+ onPress: () => onSelect((0, import_chat2.selectChoice)(resolved.frame, choice.id)),
186
+ testID: `ak-choice-${choice.id}`,
187
+ style: styles.choice,
188
+ children: [
189
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: choice.label }),
190
+ choice.description === void 0 ? null : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.mutedText, children: choice.description })
191
+ ]
192
+ },
193
+ choice.id
194
+ ))
195
+ ] });
196
+ };
197
+ var AgentChatNativeSession = ({ definition, placeholder, onComponentSelect = () => void 0, onComponentInteract = () => void 0, actionConfirmationTtlMs, session: preparedSession, theme, slots = {} }) => {
198
+ const styles = toChatNativeStyles(theme);
199
+ const ContainerSlot = slots.Container ?? import_react_native2.ChatContainer;
200
+ const MessageSlot = slots.Message ?? import_react_native2.Message;
201
+ const InputSlot = slots.Input ?? import_react_native2.InputBar;
202
+ const ThinkingSlot = slots.Thinking ?? import_react_native2.ThinkingIndicator;
203
+ const ConfirmationSlot = slots.Confirmation ?? import_react_native2.ToolConfirmation;
204
+ const ChoiceListSlot = slots.ChoiceList ?? ChoiceListNative;
205
+ const StandardComponentSlot = slots.StandardComponent ?? StandardComponentNative;
206
+ const [session] = (0, import_react2.useState)(() => (0, import_chat2.resolveChatSession)(definition, preparedSession));
207
+ const sessionId = session.sessionId;
208
+ const [actionError, setActionError] = (0, import_react2.useState)();
209
+ const [editDraft, setEditDraft] = (0, import_react2.useState)();
210
+ const [resolvedInstances, setResolvedInstances] = (0, import_react2.useState)(() => /* @__PURE__ */ new Set());
211
+ const resolvedInstancesRef = (0, import_react2.useRef)(/* @__PURE__ */ new Set());
212
+ const config = (0, import_react2.useMemo)(() => session.updateChat(definition.chat), [definition.chat, session]);
213
+ const chat = (0, import_react_native2.useChat)(config);
214
+ const chatRef = (0, import_react2.useRef)(chat);
215
+ chatRef.current = chat;
216
+ const [confirmation] = (0, import_react2.useState)(() => session.createConfirmation({ ...actionConfirmationTtlMs === void 0 ? {} : { ttlMs: actionConfirmationTtlMs }, chat: {
217
+ proposeToolCall: (proposal) => chatRef.current.proposeToolCall(proposal),
218
+ approve: (id) => chatRef.current.approve(id),
219
+ deny: (id, reason) => chatRef.current.deny(id, reason)
220
+ } }));
221
+ const selectComponent = (event, frame) => {
222
+ if (resolvedInstancesRef.current.has(event.instanceId)) return;
223
+ resolvedInstancesRef.current.add(event.instanceId);
224
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
225
+ try {
226
+ onComponentSelect(event);
227
+ } catch (error) {
228
+ setActionError(error instanceof Error ? error : new Error("Component selection callback failed."));
229
+ }
230
+ const action = (0, import_chat2.resolveChoiceAction)(frame, event.choiceId);
231
+ if (action) void confirmation.propose(action).catch((error) => {
232
+ resolvedInstancesRef.current.delete(event.instanceId);
233
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
234
+ setActionError(error instanceof Error ? error : new Error("Action proposal failed."));
235
+ });
236
+ else {
237
+ let submission;
238
+ try {
239
+ submission = definition.choiceSubmission?.(frame, event.choiceId, { sessionId });
240
+ } catch (error) {
241
+ resolvedInstancesRef.current.delete(event.instanceId);
242
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
243
+ setActionError(error instanceof Error ? error : new Error("Choice submission authorization failed."));
244
+ return;
245
+ }
246
+ if (submission && "unavailable" in submission) {
247
+ resolvedInstancesRef.current.delete(event.instanceId);
248
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
249
+ setActionError(new Error("This deterministic choice expired. Ask the question again."));
250
+ return;
251
+ }
252
+ if (submission) void chatRef.current.send(submission.value).then(
253
+ () => {
254
+ try {
255
+ submission.commit();
256
+ } catch (error) {
257
+ setActionError(error instanceof Error ? error : new Error("Choice submission settlement failed."));
258
+ }
259
+ },
260
+ (error) => {
261
+ try {
262
+ submission.release();
263
+ } catch {
264
+ } finally {
265
+ resolvedInstancesRef.current.delete(event.instanceId);
266
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
267
+ }
268
+ setActionError(error instanceof Error ? error : new Error("Choice submission failed."));
269
+ }
270
+ );
271
+ }
272
+ };
273
+ const interactComponent = (event) => {
274
+ if (resolvedInstancesRef.current.has(event.instanceId)) return;
275
+ resolvedInstancesRef.current.add(event.instanceId);
276
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
277
+ try {
278
+ onComponentInteract(event);
279
+ } catch (error) {
280
+ resolvedInstancesRef.current.delete(event.instanceId);
281
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
282
+ setActionError(error instanceof Error ? error : new Error("Component interaction callback failed."));
283
+ }
284
+ };
285
+ const approve = (toolCallId) => {
286
+ const record = confirmation.getByToolCall(toolCallId);
287
+ void (record ? confirmation.approve(record.token, sessionId) : chat.approve(toolCallId)).catch((error) => setActionError(error instanceof Error ? error : new Error("Action approval failed.")));
288
+ };
289
+ const deny = (toolCallId, reason) => {
290
+ const record = confirmation.getByToolCall(toolCallId);
291
+ void (record ? confirmation.reject(record.token, sessionId, reason) : chat.deny(toolCallId, reason)).catch((error) => setActionError(error instanceof Error ? error : new Error("Action rejection failed.")));
292
+ };
293
+ const targets = (0, import_chat2.getLifecycleTargets)(chat.messages);
294
+ const runLifecycle = (operation) => {
295
+ setActionError(void 0);
296
+ void operation.catch((error) => setActionError(error instanceof Error ? error : new Error("Lifecycle operation failed.")));
297
+ };
298
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native3.View, { testID: "ak-app-chat", accessibilityLabel: `${definition.id} chat`, style: styles.root, children: [
299
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.View, { accessibilityLiveRegion: "polite", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(ContainerSlot, { style: styles.container, children: [
300
+ chat.messages.flatMap((message) => (0, import_chat2.presentChatMessage)(message).map((presentation, index) => {
301
+ const key = `${message.id}:${index}`;
302
+ if (presentation.kind === "component") {
303
+ const manifest = definition.components;
304
+ const resolved = manifest === void 0 ? void 0 : (0, import_chat2.resolveComponentFrame)(presentation.frame, manifest);
305
+ if (resolved?.ok && slots.StandardComponent === void 0 && !import_chat2.STANDARD_COMPONENT_KEYS.includes(presentation.frame.componentKey)) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { children: (0, import_chat2.formatSemanticFallback)(presentation.frame.fallback) }, key);
306
+ if (resolved?.ok) return presentation.frame.componentKey === "choice-list" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ChoiceListSlot, { frame: presentation.frame, manifest, disabled: resolvedInstances.has(presentation.frame.instanceId), onSelect: (event) => selectComponent(event, presentation.frame), styles }, key) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(StandardComponentSlot, { frame: presentation.frame, manifest, disabled: resolvedInstances.has(presentation.frame.instanceId), onInteract: interactComponent }, key);
307
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.assistantMessageText, children: (0, import_chat2.formatSemanticFallback)(presentation.frame.fallback) }, key);
308
+ }
309
+ if (presentation.kind === "diagnostic") return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { accessibilityRole: "alert", style: styles.dangerText, children: presentation.message }, key);
310
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
311
+ MessageSlot,
312
+ {
313
+ message: presentation.message,
314
+ style: presentation.message.role === "user" ? styles.userMessage : styles.assistantMessage,
315
+ contentStyle: presentation.message.role === "user" ? styles.userMessageText : styles.assistantMessageText
316
+ },
317
+ key
318
+ );
319
+ })),
320
+ chat.messages.flatMap((message) => message.toolCalls ?? []).map((toolCall) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ConfirmationSlot, { toolCall, onApprove: approve, onDeny: deny }, toolCall.id)),
321
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ThinkingSlot, { visible: chat.status === "streaming" })
322
+ ] }) }),
323
+ chat.error || actionError ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { accessibilityRole: "alert", style: styles.dangerText, children: chat.error?.message ?? actionError?.message }) : null,
324
+ chat.status === "streaming" ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
325
+ import_react_native3.Pressable,
326
+ {
327
+ testID: "ak-stop",
328
+ accessibilityRole: "button",
329
+ accessibilityLabel: "Stop response",
330
+ onPress: chat.stop,
331
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: "Stop" })
332
+ }
333
+ ) : null,
334
+ chat.status !== "streaming" && targets.userId ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native3.View, { accessibilityLabel: "Response actions", children: [
335
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Pressable, { accessibilityRole: "button", accessibilityLabel: "Retry response", testID: "ak-retry", onPress: () => runLifecycle(chat.retry()), children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: "Retry" }) }),
336
+ targets.assistantId ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Pressable, { accessibilityRole: "button", accessibilityLabel: "Regenerate response", testID: "ak-regenerate", onPress: () => runLifecycle(chat.regenerate(targets.assistantId)), children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: "Regenerate" }) }) : null,
337
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Pressable, { accessibilityRole: "button", accessibilityLabel: "Edit last message", testID: "ak-edit", onPress: () => setEditDraft({ messageId: targets.userId, content: chat.messages.find((message) => message.id === targets.userId)?.content ?? "" }), children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: "Edit" }) }),
338
+ editDraft === void 0 ? null : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
339
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.TextInput, { accessibilityLabel: "Edit message", testID: "ak-edit-input", value: editDraft.content, style: styles.inputText, onChangeText: (content) => setEditDraft({ ...editDraft, content }) }),
340
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Pressable, { accessibilityRole: "button", accessibilityLabel: "Save edit", testID: "ak-edit-save", disabled: editDraft.content.trim() === "", onPress: () => {
341
+ runLifecycle(chat.edit(editDraft.messageId, editDraft.content));
342
+ setEditDraft(void 0);
343
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native3.Text, { style: styles.choiceText, children: "Save" }) })
344
+ ] })
345
+ ] }) : null,
346
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
347
+ InputSlot,
348
+ {
349
+ chat,
350
+ disabled: chat.status === "streaming",
351
+ style: styles.input,
352
+ inputStyle: styles.inputText,
353
+ ...placeholder === void 0 ? {} : { placeholder }
354
+ }
355
+ )
356
+ ] });
357
+ };
358
+ var AgentChatNative = (props) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(AgentChatNativeSession, { ...props }, `${props.definition.id}:${props.definition.revision ?? 1}:${props.session?.sessionId ?? "new"}`);
359
+ // Annotate the CommonJS export names for ESM import in node:
360
+ 0 && (module.exports = {
361
+ AgentChatNative,
362
+ ChoiceListNative,
363
+ StandardComponentNative,
364
+ toChatNativeStyles
365
+ });
@@ -0,0 +1,63 @@
1
+ import { ComponentManifest, ChatDefinition, ChatSession, ChatThemeInput } from '@agentskit/chat';
2
+ export { ChatDefinition } from '@agentskit/chat';
3
+ import { ComponentRenderFrame, ComponentInteractionEvent, ComponentSelectionEvent } from '@agentskit/chat-protocol';
4
+ import { ChatContainer, Message, InputBar, ThinkingIndicator, ToolConfirmation } from '@agentskit/react-native';
5
+ import { ReactElement, ComponentType, ComponentProps } from 'react';
6
+ import { ViewStyle, TextStyle } from 'react-native';
7
+
8
+ interface StandardComponentNativeProps {
9
+ readonly frame: ComponentRenderFrame;
10
+ readonly manifest: ComponentManifest;
11
+ readonly onInteract: (event: ComponentInteractionEvent) => void;
12
+ readonly disabled?: boolean;
13
+ }
14
+ declare const StandardComponentNative: (props: StandardComponentNativeProps) => ReactElement | null;
15
+
16
+ type NativeViewStyle = ViewStyle & Readonly<Record<string, unknown>>;
17
+ type NativeTextStyle = TextStyle & Readonly<Record<string, unknown>>;
18
+ interface ChatNativeStyles {
19
+ readonly root: NativeViewStyle;
20
+ readonly container: NativeViewStyle;
21
+ readonly userMessage: NativeViewStyle;
22
+ readonly userMessageText: NativeTextStyle;
23
+ readonly assistantMessage: NativeViewStyle;
24
+ readonly assistantMessageText: NativeTextStyle;
25
+ readonly choiceList: NativeViewStyle;
26
+ readonly choice: NativeViewStyle;
27
+ readonly choiceText: NativeTextStyle;
28
+ readonly mutedText: NativeTextStyle;
29
+ readonly input: NativeViewStyle;
30
+ readonly inputText: NativeTextStyle;
31
+ readonly dangerText: NativeTextStyle;
32
+ }
33
+ declare const toChatNativeStyles: (input?: ChatThemeInput) => ChatNativeStyles;
34
+ interface AgentChatNativeSlots {
35
+ readonly Container?: ComponentType<ComponentProps<typeof ChatContainer>>;
36
+ readonly Message?: ComponentType<ComponentProps<typeof Message>>;
37
+ readonly Input?: ComponentType<ComponentProps<typeof InputBar>>;
38
+ readonly Thinking?: ComponentType<ComponentProps<typeof ThinkingIndicator>>;
39
+ readonly Confirmation?: ComponentType<ComponentProps<typeof ToolConfirmation>>;
40
+ readonly ChoiceList?: ComponentType<ChoiceListNativeProps>;
41
+ readonly StandardComponent?: ComponentType<StandardComponentNativeProps>;
42
+ }
43
+ interface AgentChatNativeProps {
44
+ readonly definition: ChatDefinition;
45
+ readonly placeholder?: string;
46
+ readonly onComponentSelect?: (event: ComponentSelectionEvent) => void;
47
+ readonly onComponentInteract?: (event: ComponentInteractionEvent) => void;
48
+ readonly actionConfirmationTtlMs?: number;
49
+ readonly session?: ChatSession;
50
+ readonly theme?: ChatThemeInput;
51
+ readonly slots?: AgentChatNativeSlots;
52
+ }
53
+ interface ChoiceListNativeProps {
54
+ readonly frame: unknown;
55
+ readonly manifest: ComponentManifest;
56
+ readonly onSelect: (event: ComponentSelectionEvent) => void;
57
+ readonly disabled?: boolean;
58
+ readonly styles?: ChatNativeStyles;
59
+ }
60
+ declare const ChoiceListNative: ({ frame, manifest, onSelect, disabled, styles }: ChoiceListNativeProps) => ReactElement | null;
61
+ declare const AgentChatNative: (props: AgentChatNativeProps) => ReactElement;
62
+
63
+ export { AgentChatNative, type AgentChatNativeProps, type AgentChatNativeSlots, type ChatNativeStyles, ChoiceListNative, type ChoiceListNativeProps, StandardComponentNative, type StandardComponentNativeProps, toChatNativeStyles };
@@ -0,0 +1,63 @@
1
+ import { ComponentManifest, ChatDefinition, ChatSession, ChatThemeInput } from '@agentskit/chat';
2
+ export { ChatDefinition } from '@agentskit/chat';
3
+ import { ComponentRenderFrame, ComponentInteractionEvent, ComponentSelectionEvent } from '@agentskit/chat-protocol';
4
+ import { ChatContainer, Message, InputBar, ThinkingIndicator, ToolConfirmation } from '@agentskit/react-native';
5
+ import { ReactElement, ComponentType, ComponentProps } from 'react';
6
+ import { ViewStyle, TextStyle } from 'react-native';
7
+
8
+ interface StandardComponentNativeProps {
9
+ readonly frame: ComponentRenderFrame;
10
+ readonly manifest: ComponentManifest;
11
+ readonly onInteract: (event: ComponentInteractionEvent) => void;
12
+ readonly disabled?: boolean;
13
+ }
14
+ declare const StandardComponentNative: (props: StandardComponentNativeProps) => ReactElement | null;
15
+
16
+ type NativeViewStyle = ViewStyle & Readonly<Record<string, unknown>>;
17
+ type NativeTextStyle = TextStyle & Readonly<Record<string, unknown>>;
18
+ interface ChatNativeStyles {
19
+ readonly root: NativeViewStyle;
20
+ readonly container: NativeViewStyle;
21
+ readonly userMessage: NativeViewStyle;
22
+ readonly userMessageText: NativeTextStyle;
23
+ readonly assistantMessage: NativeViewStyle;
24
+ readonly assistantMessageText: NativeTextStyle;
25
+ readonly choiceList: NativeViewStyle;
26
+ readonly choice: NativeViewStyle;
27
+ readonly choiceText: NativeTextStyle;
28
+ readonly mutedText: NativeTextStyle;
29
+ readonly input: NativeViewStyle;
30
+ readonly inputText: NativeTextStyle;
31
+ readonly dangerText: NativeTextStyle;
32
+ }
33
+ declare const toChatNativeStyles: (input?: ChatThemeInput) => ChatNativeStyles;
34
+ interface AgentChatNativeSlots {
35
+ readonly Container?: ComponentType<ComponentProps<typeof ChatContainer>>;
36
+ readonly Message?: ComponentType<ComponentProps<typeof Message>>;
37
+ readonly Input?: ComponentType<ComponentProps<typeof InputBar>>;
38
+ readonly Thinking?: ComponentType<ComponentProps<typeof ThinkingIndicator>>;
39
+ readonly Confirmation?: ComponentType<ComponentProps<typeof ToolConfirmation>>;
40
+ readonly ChoiceList?: ComponentType<ChoiceListNativeProps>;
41
+ readonly StandardComponent?: ComponentType<StandardComponentNativeProps>;
42
+ }
43
+ interface AgentChatNativeProps {
44
+ readonly definition: ChatDefinition;
45
+ readonly placeholder?: string;
46
+ readonly onComponentSelect?: (event: ComponentSelectionEvent) => void;
47
+ readonly onComponentInteract?: (event: ComponentInteractionEvent) => void;
48
+ readonly actionConfirmationTtlMs?: number;
49
+ readonly session?: ChatSession;
50
+ readonly theme?: ChatThemeInput;
51
+ readonly slots?: AgentChatNativeSlots;
52
+ }
53
+ interface ChoiceListNativeProps {
54
+ readonly frame: unknown;
55
+ readonly manifest: ComponentManifest;
56
+ readonly onSelect: (event: ComponentSelectionEvent) => void;
57
+ readonly disabled?: boolean;
58
+ readonly styles?: ChatNativeStyles;
59
+ }
60
+ declare const ChoiceListNative: ({ frame, manifest, onSelect, disabled, styles }: ChoiceListNativeProps) => ReactElement | null;
61
+ declare const AgentChatNative: (props: AgentChatNativeProps) => ReactElement;
62
+
63
+ export { AgentChatNative, type AgentChatNativeProps, type AgentChatNativeSlots, type ChatNativeStyles, ChoiceListNative, type ChoiceListNativeProps, StandardComponentNative, type StandardComponentNativeProps, toChatNativeStyles };
package/dist/index.js ADDED
@@ -0,0 +1,344 @@
1
+ // src/index.tsx
2
+ import { STANDARD_COMPONENT_KEYS, formatSemanticFallback, getLifecycleTargets, presentChatMessage, resolveChatSession, resolveChatTheme, resolveChoiceAction, resolveChoiceListFrame, resolveComponentFrame as resolveComponentFrame2, selectChoice } from "@agentskit/chat";
3
+ import {
4
+ ChatContainer,
5
+ InputBar,
6
+ Message,
7
+ ThinkingIndicator,
8
+ ToolConfirmation,
9
+ useChat
10
+ } from "@agentskit/react-native";
11
+ import { useMemo, useRef, useState as useState2 } from "react";
12
+ import { Pressable as Pressable2, Text as Text2, TextInput as TextInput2, View as View2 } from "react-native";
13
+
14
+ // src/StandardComponent.tsx
15
+ import { ApprovalRequestPropsSchema, ButtonGroupPropsSchema, ConfirmationPropsSchema, ErrorNoticePropsSchema, FileAttachmentPropsSchema, FormPropsSchema, LinkCardPropsSchema, ProgressPropsSchema, SourceListPropsSchema, TablePropsSchema, ToolCallPropsSchema, createComponentInteraction, resolveComponentFrame, validateStandardComponentInteraction } from "@agentskit/chat";
16
+ import { useState } from "react";
17
+ import { Pressable, Text, TextInput, View } from "react-native";
18
+ import { jsx, jsxs } from "react/jsx-runtime";
19
+ var Button = ({ label, onPress, disabled = false }) => /* @__PURE__ */ jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: label, disabled, onPress, children: /* @__PURE__ */ jsx(Text, { children: label }) });
20
+ var StandardFormNative = (props) => {
21
+ const item = FormPropsSchema.parse(props.frame.props);
22
+ const [values, setValues] = useState({});
23
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "none", accessibilityLabel: item.title ?? "Form", testID: "ak-form", children: [
24
+ item.title ? /* @__PURE__ */ jsx(Text, { accessibilityRole: "header", children: item.title }) : null,
25
+ item.fields.map((field) => /* @__PURE__ */ jsxs(View, { children: [
26
+ /* @__PURE__ */ jsx(Text, { children: field.label }),
27
+ field.type === "checkbox" ? /* @__PURE__ */ jsx(Button, { label: `${field.label}: ${values[field.id] ? "checked" : "unchecked"}`, disabled: props.disabled, onPress: () => setValues((current) => ({ ...current, [field.id]: !current[field.id] })) }) : field.type === "select" ? /* @__PURE__ */ jsx(View, { accessibilityRole: "radiogroup", accessibilityLabel: field.label, children: field.options?.map((option) => /* @__PURE__ */ jsx(Pressable, { accessibilityRole: "radio", accessibilityState: { checked: values[field.id] === option.id }, disabled: props.disabled, onPress: () => setValues((current) => ({ ...current, [field.id]: option.id })), children: /* @__PURE__ */ jsxs(Text, { children: [
28
+ values[field.id] === option.id ? "\u25CF" : "\u25CB",
29
+ " ",
30
+ option.label
31
+ ] }) }, option.id)) }) : /* @__PURE__ */ jsx(TextInput, { accessibilityLabel: field.label, editable: !props.disabled, placeholder: field.placeholder, inputMode: field.type === "email" ? "email" : field.type === "number" ? "numeric" : "text", value: String(values[field.id] ?? ""), onChangeText: (value) => setValues((current) => ({ ...current, [field.id]: value })) })
32
+ ] }, field.id)),
33
+ /* @__PURE__ */ jsx(Button, { label: item.submitLabel, disabled: props.disabled || !validateStandardComponentInteraction("form", item, "submit", values), onPress: () => props.onInteract(createComponentInteraction(props.frame, props.manifest, "submit", values)) })
34
+ ] });
35
+ };
36
+ var StandardComponentNative = (props) => {
37
+ if (!resolveComponentFrame(props.frame, props.manifest).ok || props.frame.componentKey === "choice-list") return null;
38
+ const emit = (event, value) => props.onInteract(createComponentInteraction(props.frame, props.manifest, event, value));
39
+ const key = props.frame.componentKey;
40
+ if (key === "button-group") {
41
+ const item = ButtonGroupPropsSchema.parse(props.frame.props);
42
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "none", accessibilityLabel: item.label, testID: "ak-button-group", children: [
43
+ /* @__PURE__ */ jsx(Text, { children: item.label }),
44
+ item.buttons.map((button) => /* @__PURE__ */ jsx(Button, { label: button.label, disabled: props.disabled || button.disabled, onPress: () => emit("select", button.id) }, button.id))
45
+ ] });
46
+ }
47
+ if (key === "form") return /* @__PURE__ */ jsx(StandardFormNative, { ...props });
48
+ if (key === "confirmation") {
49
+ const item = ConfirmationPropsSchema.parse(props.frame.props);
50
+ return /* @__PURE__ */ jsxs(View, { accessibilityLabel: item.title, testID: "ak-confirmation", children: [
51
+ /* @__PURE__ */ jsx(Text, { children: item.title }),
52
+ /* @__PURE__ */ jsx(Text, { children: item.message }),
53
+ /* @__PURE__ */ jsx(Button, { label: item.confirmLabel, disabled: props.disabled, onPress: () => emit("confirm") }),
54
+ /* @__PURE__ */ jsx(Button, { label: item.cancelLabel, disabled: props.disabled, onPress: () => emit("cancel") })
55
+ ] });
56
+ }
57
+ if (key === "progress") {
58
+ const item = ProgressPropsSchema.parse(props.frame.props);
59
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "progressbar", accessibilityLabel: item.label, accessibilityValue: { min: 0, max: 100, now: item.value }, testID: "ak-progress", children: [
60
+ /* @__PURE__ */ jsxs(Text, { children: [
61
+ item.label,
62
+ ": ",
63
+ item.value,
64
+ "%"
65
+ ] }),
66
+ /* @__PURE__ */ jsx(Text, { children: item.status })
67
+ ] });
68
+ }
69
+ if (key === "source-list") {
70
+ const item = SourceListPropsSchema.parse(props.frame.props);
71
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "list", accessibilityLabel: item.label, testID: "ak-source-list", children: [
72
+ /* @__PURE__ */ jsx(Text, { children: item.label }),
73
+ item.sources.map((source) => /* @__PURE__ */ jsxs(View, { children: [
74
+ /* @__PURE__ */ jsx(Button, { label: source.title, disabled: !source.url || props.disabled, onPress: () => emit("open", source.id) }),
75
+ /* @__PURE__ */ jsx(Text, { children: source.snippet })
76
+ ] }, source.id))
77
+ ] });
78
+ }
79
+ if (key === "link-card") {
80
+ const item = LinkCardPropsSchema.parse(props.frame.props);
81
+ return /* @__PURE__ */ jsxs(Pressable, { accessibilityRole: "link", accessibilityLabel: item.label ?? item.title, disabled: props.disabled, onPress: () => emit("open", item.href), testID: "ak-link-card", children: [
82
+ /* @__PURE__ */ jsx(Text, { children: item.title }),
83
+ /* @__PURE__ */ jsx(Text, { children: item.description })
84
+ ] });
85
+ }
86
+ if (key === "error-notice") {
87
+ const item = ErrorNoticePropsSchema.parse(props.frame.props);
88
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "alert", testID: "ak-error-notice", children: [
89
+ /* @__PURE__ */ jsx(Text, { children: item.title }),
90
+ /* @__PURE__ */ jsx(Text, { children: item.message }),
91
+ /* @__PURE__ */ jsx(Text, { children: item.code }),
92
+ item.retryLabel ? /* @__PURE__ */ jsx(Button, { label: item.retryLabel, disabled: props.disabled, onPress: () => emit("retry") }) : null
93
+ ] });
94
+ }
95
+ if (key === "tool-call") {
96
+ const item = ToolCallPropsSchema.parse(props.frame.props);
97
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "summary", accessibilityLabel: `${item.name}: ${item.status}`, testID: "ak-tool-call", children: [
98
+ /* @__PURE__ */ jsx(Text, { children: item.name }),
99
+ /* @__PURE__ */ jsx(Text, { children: item.status }),
100
+ /* @__PURE__ */ jsx(Text, { children: item.arguments ? JSON.stringify(item.arguments) : "" }),
101
+ /* @__PURE__ */ jsx(Text, { children: item.result === void 0 ? "" : JSON.stringify(item.result) })
102
+ ] });
103
+ }
104
+ if (key === "approval-request") {
105
+ const item = ApprovalRequestPropsSchema.parse(props.frame.props);
106
+ return /* @__PURE__ */ jsxs(View, { accessibilityLabel: item.title, testID: "ak-approval-request", children: [
107
+ /* @__PURE__ */ jsx(Text, { children: item.title }),
108
+ /* @__PURE__ */ jsx(Text, { children: item.description }),
109
+ /* @__PURE__ */ jsx(Button, { label: item.approveLabel, disabled: props.disabled, onPress: () => emit("approve") }),
110
+ /* @__PURE__ */ jsx(Button, { label: item.denyLabel, disabled: props.disabled, onPress: () => emit("deny") })
111
+ ] });
112
+ }
113
+ if (key === "table") {
114
+ const item = TablePropsSchema.parse(props.frame.props);
115
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "summary", accessibilityLabel: item.caption, testID: "ak-table", children: [
116
+ /* @__PURE__ */ jsx(Text, { children: item.caption }),
117
+ /* @__PURE__ */ jsx(Text, { children: item.columns.map((column) => column.label).join(" | ") }),
118
+ item.rows.map((row, index) => /* @__PURE__ */ jsx(Text, { children: item.columns.map((column) => String(row[column.key] ?? "")).join(" | ") }, index))
119
+ ] });
120
+ }
121
+ if (key === "file-attachment") {
122
+ const item = FileAttachmentPropsSchema.parse(props.frame.props);
123
+ return /* @__PURE__ */ jsxs(View, { accessibilityLabel: `${item.name}, ${item.mimeType}`, testID: "ak-file-attachment", children: [
124
+ /* @__PURE__ */ jsx(Text, { children: item.name }),
125
+ /* @__PURE__ */ jsx(Text, { children: item.mimeType }),
126
+ /* @__PURE__ */ jsx(Text, { children: item.sizeBytes === void 0 ? "" : `${item.sizeBytes} bytes` }),
127
+ item.url ? /* @__PURE__ */ jsx(Button, { label: "Open", disabled: props.disabled, onPress: () => emit("open", item.url) }) : null
128
+ ] });
129
+ }
130
+ return null;
131
+ };
132
+
133
+ // src/index.tsx
134
+ import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
135
+ var toChatNativeStyles = (input) => {
136
+ const theme = resolveChatTheme(input);
137
+ const font = theme.fontFamily === "system" ? {} : { fontFamily: theme.fontFamily };
138
+ return {
139
+ root: { flex: 1, backgroundColor: theme.colors.background, padding: theme.spacing.large },
140
+ container: { backgroundColor: theme.colors.background },
141
+ userMessage: { alignSelf: "flex-end", backgroundColor: theme.colors.accent, borderRadius: theme.radius.large, padding: theme.spacing.medium },
142
+ userMessageText: { color: theme.colors.onAccent, ...font },
143
+ assistantMessage: { alignSelf: "flex-start", backgroundColor: theme.colors.surface, borderRadius: theme.radius.large, padding: theme.spacing.medium },
144
+ assistantMessageText: { color: theme.colors.text, ...font },
145
+ choiceList: { gap: theme.spacing.small, padding: theme.spacing.medium },
146
+ choice: { borderColor: theme.colors.border, borderRadius: theme.radius.medium, borderWidth: 1, padding: theme.spacing.medium },
147
+ choiceText: { color: theme.colors.text, ...font },
148
+ mutedText: { color: theme.colors.muted, ...font },
149
+ input: { backgroundColor: theme.colors.surface, borderColor: theme.colors.border, borderTopWidth: 1, padding: theme.spacing.medium },
150
+ inputText: { color: theme.colors.text, ...font },
151
+ dangerText: { color: theme.colors.danger, ...font }
152
+ };
153
+ };
154
+ var ChoiceListNative = ({ frame, manifest, onSelect, disabled = false, styles = toChatNativeStyles() }) => {
155
+ const resolved = resolveChoiceListFrame(frame, manifest);
156
+ if (!resolved.ok) return null;
157
+ return /* @__PURE__ */ jsxs2(View2, { testID: "ak-choice-list", style: styles.choiceList, children: [
158
+ /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: resolved.props.prompt }),
159
+ resolved.props.choices.map((choice) => /* @__PURE__ */ jsxs2(
160
+ Pressable2,
161
+ {
162
+ accessibilityRole: "button",
163
+ disabled,
164
+ accessibilityLabel: choice.description === void 0 ? choice.label : `${choice.label}. ${choice.description}`,
165
+ onPress: () => onSelect(selectChoice(resolved.frame, choice.id)),
166
+ testID: `ak-choice-${choice.id}`,
167
+ style: styles.choice,
168
+ children: [
169
+ /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: choice.label }),
170
+ choice.description === void 0 ? null : /* @__PURE__ */ jsx2(Text2, { style: styles.mutedText, children: choice.description })
171
+ ]
172
+ },
173
+ choice.id
174
+ ))
175
+ ] });
176
+ };
177
+ var AgentChatNativeSession = ({ definition, placeholder, onComponentSelect = () => void 0, onComponentInteract = () => void 0, actionConfirmationTtlMs, session: preparedSession, theme, slots = {} }) => {
178
+ const styles = toChatNativeStyles(theme);
179
+ const ContainerSlot = slots.Container ?? ChatContainer;
180
+ const MessageSlot = slots.Message ?? Message;
181
+ const InputSlot = slots.Input ?? InputBar;
182
+ const ThinkingSlot = slots.Thinking ?? ThinkingIndicator;
183
+ const ConfirmationSlot = slots.Confirmation ?? ToolConfirmation;
184
+ const ChoiceListSlot = slots.ChoiceList ?? ChoiceListNative;
185
+ const StandardComponentSlot = slots.StandardComponent ?? StandardComponentNative;
186
+ const [session] = useState2(() => resolveChatSession(definition, preparedSession));
187
+ const sessionId = session.sessionId;
188
+ const [actionError, setActionError] = useState2();
189
+ const [editDraft, setEditDraft] = useState2();
190
+ const [resolvedInstances, setResolvedInstances] = useState2(() => /* @__PURE__ */ new Set());
191
+ const resolvedInstancesRef = useRef(/* @__PURE__ */ new Set());
192
+ const config = useMemo(() => session.updateChat(definition.chat), [definition.chat, session]);
193
+ const chat = useChat(config);
194
+ const chatRef = useRef(chat);
195
+ chatRef.current = chat;
196
+ const [confirmation] = useState2(() => session.createConfirmation({ ...actionConfirmationTtlMs === void 0 ? {} : { ttlMs: actionConfirmationTtlMs }, chat: {
197
+ proposeToolCall: (proposal) => chatRef.current.proposeToolCall(proposal),
198
+ approve: (id) => chatRef.current.approve(id),
199
+ deny: (id, reason) => chatRef.current.deny(id, reason)
200
+ } }));
201
+ const selectComponent = (event, frame) => {
202
+ if (resolvedInstancesRef.current.has(event.instanceId)) return;
203
+ resolvedInstancesRef.current.add(event.instanceId);
204
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
205
+ try {
206
+ onComponentSelect(event);
207
+ } catch (error) {
208
+ setActionError(error instanceof Error ? error : new Error("Component selection callback failed."));
209
+ }
210
+ const action = resolveChoiceAction(frame, event.choiceId);
211
+ if (action) void confirmation.propose(action).catch((error) => {
212
+ resolvedInstancesRef.current.delete(event.instanceId);
213
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
214
+ setActionError(error instanceof Error ? error : new Error("Action proposal failed."));
215
+ });
216
+ else {
217
+ let submission;
218
+ try {
219
+ submission = definition.choiceSubmission?.(frame, event.choiceId, { sessionId });
220
+ } catch (error) {
221
+ resolvedInstancesRef.current.delete(event.instanceId);
222
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
223
+ setActionError(error instanceof Error ? error : new Error("Choice submission authorization failed."));
224
+ return;
225
+ }
226
+ if (submission && "unavailable" in submission) {
227
+ resolvedInstancesRef.current.delete(event.instanceId);
228
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
229
+ setActionError(new Error("This deterministic choice expired. Ask the question again."));
230
+ return;
231
+ }
232
+ if (submission) void chatRef.current.send(submission.value).then(
233
+ () => {
234
+ try {
235
+ submission.commit();
236
+ } catch (error) {
237
+ setActionError(error instanceof Error ? error : new Error("Choice submission settlement failed."));
238
+ }
239
+ },
240
+ (error) => {
241
+ try {
242
+ submission.release();
243
+ } catch {
244
+ } finally {
245
+ resolvedInstancesRef.current.delete(event.instanceId);
246
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
247
+ }
248
+ setActionError(error instanceof Error ? error : new Error("Choice submission failed."));
249
+ }
250
+ );
251
+ }
252
+ };
253
+ const interactComponent = (event) => {
254
+ if (resolvedInstancesRef.current.has(event.instanceId)) return;
255
+ resolvedInstancesRef.current.add(event.instanceId);
256
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
257
+ try {
258
+ onComponentInteract(event);
259
+ } catch (error) {
260
+ resolvedInstancesRef.current.delete(event.instanceId);
261
+ setResolvedInstances(new Set(resolvedInstancesRef.current));
262
+ setActionError(error instanceof Error ? error : new Error("Component interaction callback failed."));
263
+ }
264
+ };
265
+ const approve = (toolCallId) => {
266
+ const record = confirmation.getByToolCall(toolCallId);
267
+ void (record ? confirmation.approve(record.token, sessionId) : chat.approve(toolCallId)).catch((error) => setActionError(error instanceof Error ? error : new Error("Action approval failed.")));
268
+ };
269
+ const deny = (toolCallId, reason) => {
270
+ const record = confirmation.getByToolCall(toolCallId);
271
+ void (record ? confirmation.reject(record.token, sessionId, reason) : chat.deny(toolCallId, reason)).catch((error) => setActionError(error instanceof Error ? error : new Error("Action rejection failed.")));
272
+ };
273
+ const targets = getLifecycleTargets(chat.messages);
274
+ const runLifecycle = (operation) => {
275
+ setActionError(void 0);
276
+ void operation.catch((error) => setActionError(error instanceof Error ? error : new Error("Lifecycle operation failed.")));
277
+ };
278
+ return /* @__PURE__ */ jsxs2(View2, { testID: "ak-app-chat", accessibilityLabel: `${definition.id} chat`, style: styles.root, children: [
279
+ /* @__PURE__ */ jsx2(View2, { accessibilityLiveRegion: "polite", children: /* @__PURE__ */ jsxs2(ContainerSlot, { style: styles.container, children: [
280
+ chat.messages.flatMap((message) => presentChatMessage(message).map((presentation, index) => {
281
+ const key = `${message.id}:${index}`;
282
+ if (presentation.kind === "component") {
283
+ const manifest = definition.components;
284
+ const resolved = manifest === void 0 ? void 0 : resolveComponentFrame2(presentation.frame, manifest);
285
+ if (resolved?.ok && slots.StandardComponent === void 0 && !STANDARD_COMPONENT_KEYS.includes(presentation.frame.componentKey)) return /* @__PURE__ */ jsx2(Text2, { children: formatSemanticFallback(presentation.frame.fallback) }, key);
286
+ if (resolved?.ok) return presentation.frame.componentKey === "choice-list" ? /* @__PURE__ */ jsx2(ChoiceListSlot, { frame: presentation.frame, manifest, disabled: resolvedInstances.has(presentation.frame.instanceId), onSelect: (event) => selectComponent(event, presentation.frame), styles }, key) : /* @__PURE__ */ jsx2(StandardComponentSlot, { frame: presentation.frame, manifest, disabled: resolvedInstances.has(presentation.frame.instanceId), onInteract: interactComponent }, key);
287
+ return /* @__PURE__ */ jsx2(Text2, { style: styles.assistantMessageText, children: formatSemanticFallback(presentation.frame.fallback) }, key);
288
+ }
289
+ if (presentation.kind === "diagnostic") return /* @__PURE__ */ jsx2(Text2, { accessibilityRole: "alert", style: styles.dangerText, children: presentation.message }, key);
290
+ return /* @__PURE__ */ jsx2(
291
+ MessageSlot,
292
+ {
293
+ message: presentation.message,
294
+ style: presentation.message.role === "user" ? styles.userMessage : styles.assistantMessage,
295
+ contentStyle: presentation.message.role === "user" ? styles.userMessageText : styles.assistantMessageText
296
+ },
297
+ key
298
+ );
299
+ })),
300
+ chat.messages.flatMap((message) => message.toolCalls ?? []).map((toolCall) => /* @__PURE__ */ jsx2(ConfirmationSlot, { toolCall, onApprove: approve, onDeny: deny }, toolCall.id)),
301
+ /* @__PURE__ */ jsx2(ThinkingSlot, { visible: chat.status === "streaming" })
302
+ ] }) }),
303
+ chat.error || actionError ? /* @__PURE__ */ jsx2(Text2, { accessibilityRole: "alert", style: styles.dangerText, children: chat.error?.message ?? actionError?.message }) : null,
304
+ chat.status === "streaming" ? /* @__PURE__ */ jsx2(
305
+ Pressable2,
306
+ {
307
+ testID: "ak-stop",
308
+ accessibilityRole: "button",
309
+ accessibilityLabel: "Stop response",
310
+ onPress: chat.stop,
311
+ children: /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: "Stop" })
312
+ }
313
+ ) : null,
314
+ chat.status !== "streaming" && targets.userId ? /* @__PURE__ */ jsxs2(View2, { accessibilityLabel: "Response actions", children: [
315
+ /* @__PURE__ */ jsx2(Pressable2, { accessibilityRole: "button", accessibilityLabel: "Retry response", testID: "ak-retry", onPress: () => runLifecycle(chat.retry()), children: /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: "Retry" }) }),
316
+ targets.assistantId ? /* @__PURE__ */ jsx2(Pressable2, { accessibilityRole: "button", accessibilityLabel: "Regenerate response", testID: "ak-regenerate", onPress: () => runLifecycle(chat.regenerate(targets.assistantId)), children: /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: "Regenerate" }) }) : null,
317
+ /* @__PURE__ */ jsx2(Pressable2, { accessibilityRole: "button", accessibilityLabel: "Edit last message", testID: "ak-edit", onPress: () => setEditDraft({ messageId: targets.userId, content: chat.messages.find((message) => message.id === targets.userId)?.content ?? "" }), children: /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: "Edit" }) }),
318
+ editDraft === void 0 ? null : /* @__PURE__ */ jsxs2(Fragment, { children: [
319
+ /* @__PURE__ */ jsx2(TextInput2, { accessibilityLabel: "Edit message", testID: "ak-edit-input", value: editDraft.content, style: styles.inputText, onChangeText: (content) => setEditDraft({ ...editDraft, content }) }),
320
+ /* @__PURE__ */ jsx2(Pressable2, { accessibilityRole: "button", accessibilityLabel: "Save edit", testID: "ak-edit-save", disabled: editDraft.content.trim() === "", onPress: () => {
321
+ runLifecycle(chat.edit(editDraft.messageId, editDraft.content));
322
+ setEditDraft(void 0);
323
+ }, children: /* @__PURE__ */ jsx2(Text2, { style: styles.choiceText, children: "Save" }) })
324
+ ] })
325
+ ] }) : null,
326
+ /* @__PURE__ */ jsx2(
327
+ InputSlot,
328
+ {
329
+ chat,
330
+ disabled: chat.status === "streaming",
331
+ style: styles.input,
332
+ inputStyle: styles.inputText,
333
+ ...placeholder === void 0 ? {} : { placeholder }
334
+ }
335
+ )
336
+ ] });
337
+ };
338
+ var AgentChatNative = (props) => /* @__PURE__ */ jsx2(AgentChatNativeSession, { ...props }, `${props.definition.id}:${props.definition.revision ?? 1}:${props.session?.sessionId ?? "new"}`);
339
+ export {
340
+ AgentChatNative,
341
+ ChoiceListNative,
342
+ StandardComponentNative,
343
+ toChatNativeStyles
344
+ };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@agentskit/chat-react-native",
3
+ "version": "0.1.0",
4
+ "description": "React Native renderer for AgentsKit Chat.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AgentsKit-io/agentskit-chat.git",
9
+ "directory": "packages/react-native"
10
+ },
11
+ "homepage": "https://github.com/AgentsKit-io/agentskit-chat#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/AgentsKit-io/agentskit-chat/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js",
23
+ "require": "./dist/index.cjs"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "sideEffects": false,
30
+ "dependencies": {
31
+ "@agentskit/chat": "0.1.0",
32
+ "@agentskit/chat-protocol": "0.1.0"
33
+ },
34
+ "peerDependencies": {
35
+ "@agentskit/react-native": "^0.4.4",
36
+ "react": ">=18",
37
+ "react-native": "*"
38
+ },
39
+ "devDependencies": {
40
+ "@agentskit/core": "^1.12.2",
41
+ "@agentskit/react-native": "^0.4.4",
42
+ "@testing-library/react": "^16.3.2",
43
+ "@types/react": "^19.2.17",
44
+ "happy-dom": "^20.10.3",
45
+ "react": "^19.2.7",
46
+ "react-dom": "^19.2.7",
47
+ "react-native": "^0.86.0",
48
+ "tsup": "^8.5.1"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public",
52
+ "provenance": true
53
+ },
54
+ "scripts": {
55
+ "build": "tsup src/index.tsx --format esm,cjs --dts --clean --external react --external react-native --external @agentskit/react-native",
56
+ "lint": "tsc --noEmit",
57
+ "test": "pnpm --filter @agentskit/chat build && vitest run --environment happy-dom"
58
+ }
59
+ }