@agimon-ai/po-native 0.0.1-alpha.69 → 0.0.1-alpha.70
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agimon-ai/po-native",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.70",
|
|
4
4
|
"description": "Po for iOS and Android",
|
|
5
5
|
"homepage": "https://agimon.ai",
|
|
6
6
|
"bugs": {
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@agimon-ai/po-core": "0.0.0-alpha.
|
|
33
|
-
"@agimon-ai/po-expo-signing": "0.0.1-alpha.
|
|
34
|
-
"@agimon-ai/po-identity": "0.0.2-alpha.
|
|
35
|
-
"@agimon-ai/po-native-components": "0.0.0-alpha.
|
|
36
|
-
"@agimon-ai/po-native-dashboard": "0.0.0-alpha.
|
|
37
|
-
"@agimon-ai/po-template-advanced": "0.0.0-alpha.
|
|
38
|
-
"@agimon-ai/po-template-elegant": "0.0.0-alpha.
|
|
39
|
-
"@agimon-ai/po-voice": "0.0.0-alpha.
|
|
40
|
-
"@agimon-ai/po-web-components": "0.0.0-alpha.
|
|
41
|
-
"@agimon-ai/po-web-dashboard": "0.0.0-alpha.
|
|
42
|
-
"@agimon-ai/po-web-security": "0.0.0-alpha.
|
|
32
|
+
"@agimon-ai/po-core": "0.0.0-alpha.190",
|
|
33
|
+
"@agimon-ai/po-expo-signing": "0.0.1-alpha.70",
|
|
34
|
+
"@agimon-ai/po-identity": "0.0.2-alpha.137",
|
|
35
|
+
"@agimon-ai/po-native-components": "0.0.0-alpha.190",
|
|
36
|
+
"@agimon-ai/po-native-dashboard": "0.0.0-alpha.62",
|
|
37
|
+
"@agimon-ai/po-template-advanced": "0.0.0-alpha.190",
|
|
38
|
+
"@agimon-ai/po-template-elegant": "0.0.0-alpha.190",
|
|
39
|
+
"@agimon-ai/po-voice": "0.0.0-alpha.190",
|
|
40
|
+
"@agimon-ai/po-web-components": "0.0.0-alpha.190",
|
|
41
|
+
"@agimon-ai/po-web-dashboard": "0.0.0-alpha.190",
|
|
42
|
+
"@agimon-ai/po-web-security": "0.0.0-alpha.190",
|
|
43
43
|
"@better-auth/expo": "1.7.12",
|
|
44
44
|
"@config-plugins/react-native-webrtc": "15.0.2",
|
|
45
45
|
"@earendil-works/chord": "1.5.2",
|
package/src/app/AccountRoute.tsx
CHANGED
|
@@ -66,6 +66,8 @@ type Mode =
|
|
|
66
66
|
| { kind: 'self'; page?: 'methods' }
|
|
67
67
|
| { kind: 'unreachable'; error: string };
|
|
68
68
|
|
|
69
|
+
const DISCOVERY_TIMEOUT_MS = 10_000;
|
|
70
|
+
|
|
69
71
|
/**
|
|
70
72
|
* The Account screen. Hosted Po Cloud and a self-hosted identity home sign in differently, so the selected
|
|
71
73
|
* issuer's bootstrap picks the flow before either one talks to it.
|
|
@@ -77,9 +79,22 @@ export function AccountRoute(props: AccountRouteProps) {
|
|
|
77
79
|
useEffect(() => {
|
|
78
80
|
let active = true;
|
|
79
81
|
setMode({ kind: 'checking' });
|
|
82
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
80
83
|
void (async () => {
|
|
81
|
-
|
|
82
|
-
|
|
84
|
+
try {
|
|
85
|
+
const bootstrap = await Promise.race([
|
|
86
|
+
(async () => hostedAccount.discover(await selectedIdentityIssuer()))(),
|
|
87
|
+
new Promise<never>((_, reject) => {
|
|
88
|
+
timeout = setTimeout(
|
|
89
|
+
() => reject(new Error('Po Cloud is taking too long. Connect a computer instead.')),
|
|
90
|
+
DISCOVERY_TIMEOUT_MS,
|
|
91
|
+
);
|
|
92
|
+
}),
|
|
93
|
+
]);
|
|
94
|
+
if (active) setMode(bootstrap ? { kind: 'hosted', bootstrap } : { kind: 'self' });
|
|
95
|
+
} finally {
|
|
96
|
+
if (timeout) clearTimeout(timeout);
|
|
97
|
+
}
|
|
83
98
|
})().catch((failure: unknown) => {
|
|
84
99
|
if (active)
|
|
85
100
|
setMode({
|
package/src/app/App.tsx
CHANGED
|
@@ -78,7 +78,6 @@ import {
|
|
|
78
78
|
railSessionStatus,
|
|
79
79
|
liveSessionOptions,
|
|
80
80
|
mentionableSessions,
|
|
81
|
-
type MentionableSession,
|
|
82
81
|
resolveSessionReference,
|
|
83
82
|
activityWaitingCount,
|
|
84
83
|
sessionStoreFor,
|
|
@@ -512,14 +511,6 @@ function useOpenSession(): (sessionId: string) => void {
|
|
|
512
511
|
);
|
|
513
512
|
}
|
|
514
513
|
|
|
515
|
-
/** Sessions an `@session:` mention in `sessionId` can name: those on its computer, by that computer's ids. */
|
|
516
|
-
function mentionSessionsOn(sessionId: string): MentionableSession[] {
|
|
517
|
-
const hostId = splitHostId(sessionId)?.hostId;
|
|
518
|
-
return mentionableSessions(sessionsStore.state, sessionId, Date.now()).flatMap((session) => {
|
|
519
|
-
const owner = splitHostId(session.id);
|
|
520
|
-
return owner?.hostId === hostId ? [{ ...session, id: owner?.localId ?? session.id }] : [];
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
514
|
function useOpenWorkspace(hostId?: string): (workspaceId: string) => void {
|
|
524
515
|
const navigation = useNavigation<NativeStackNavigationProp<Routes>>();
|
|
525
516
|
return useCallback(
|
|
@@ -1007,7 +998,12 @@ function SessionRoute({
|
|
|
1007
998
|
loadImage: (path: string) => loadSessionImage(sessionId, path),
|
|
1008
999
|
onFileLink: (label: string, explicit = false) => {
|
|
1009
1000
|
// `@session:<token>` opens that session, found among the ones on this session's computer.
|
|
1010
|
-
const mentioned = explicit
|
|
1001
|
+
const mentioned = explicit
|
|
1002
|
+
? undefined
|
|
1003
|
+
: resolveSessionReference(
|
|
1004
|
+
label,
|
|
1005
|
+
mentionableSessions(sessionsStore.state, workspacesStore.state.byId, sessionId, Date.now()),
|
|
1006
|
+
);
|
|
1011
1007
|
if (mentioned !== undefined) {
|
|
1012
1008
|
const hostId = splitHostId(sessionId)?.hostId;
|
|
1013
1009
|
return () => openSession(hostId === undefined ? mentioned.id : qualifyHostId(hostId, mentioned.id));
|
|
@@ -1055,6 +1051,8 @@ function SessionRoute({
|
|
|
1055
1051
|
},
|
|
1056
1052
|
{
|
|
1057
1053
|
...richRenderers,
|
|
1054
|
+
// Session pages such as the Insert sheet's reach the session's own computer, as its header controls do.
|
|
1055
|
+
...(headerApi ? { dashboardApi: headerApi } : {}),
|
|
1058
1056
|
renderMcpApp: ({ key, toolCallId }) => <McpAppView key={key} toolCallId={toolCallId} sessionId={sessionId} />,
|
|
1059
1057
|
nativeSaveFile,
|
|
1060
1058
|
nativeAudio,
|
|
@@ -1237,7 +1235,8 @@ function SessionRoute({
|
|
|
1237
1235
|
composer={{
|
|
1238
1236
|
live: true,
|
|
1239
1237
|
searchFiles: (query: string) => searchFiles(sessionId, query),
|
|
1240
|
-
mentionSessions: () =>
|
|
1238
|
+
mentionSessions: () =>
|
|
1239
|
+
mentionableSessions(sessionsStore.state, workspacesStore.state.byId, sessionId, Date.now()),
|
|
1241
1240
|
references: (query: string) => fileLinkMentions(sessionId, query),
|
|
1242
1241
|
uploadAttachment: uploadSessionAttachment,
|
|
1243
1242
|
onAbort: () => whenConnected(sessionId, () => abortRun(sessionId)),
|
|
@@ -85,13 +85,14 @@ function GroupCard({ group, slotProps }: { group: ActivityGroup; slotProps: WebP
|
|
|
85
85
|
) : (
|
|
86
86
|
<View style={styles.cardHead}>{heading}</View>
|
|
87
87
|
)}
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
{slotFills(slot, slotProps.sessionId).length > 0 ? (
|
|
89
|
+
// A package's own body takes the card's full width; only the summary line sits under the name.
|
|
90
|
+
<View style={styles.cardBody}>
|
|
90
91
|
<Fills slot={slot} slotProps={slotProps} />
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
)}
|
|
94
|
-
|
|
92
|
+
</View>
|
|
93
|
+
) : (
|
|
94
|
+
<Text style={[styles.summary(group.active), styles.indent]}>{group.summary || 'Idle'}</Text>
|
|
95
|
+
)}
|
|
95
96
|
</View>
|
|
96
97
|
);
|
|
97
98
|
}
|
|
@@ -285,8 +286,9 @@ const styles = StyleSheet.create((theme) => {
|
|
|
285
286
|
borderWidth: 1,
|
|
286
287
|
borderColor: theme.colors['border-soft'],
|
|
287
288
|
},
|
|
288
|
-
|
|
289
|
-
|
|
289
|
+
cardBody: { minWidth: 0, gap: theme.spacing(1.5) },
|
|
290
|
+
// The summary under the name starts where the name does, past the 20 point icon.
|
|
291
|
+
indent: { paddingLeft: 20 + theme.spacing(3) },
|
|
290
292
|
cardHead: { flexDirection: 'row', alignItems: 'center', gap: theme.spacing(3) },
|
|
291
293
|
cardName: { ...text('base', theme.colors.hi), fontWeight: theme.fontWeight.semibold, flex: 1 },
|
|
292
294
|
summary: (active: boolean) => text('sm', active ? theme.colors.yellow : theme.colors.dim),
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
HTML_INSPECT_LIMITS,
|
|
3
|
+
htmlInspectRequest,
|
|
4
|
+
parseHtmlInspectMessage,
|
|
5
|
+
type StoryPreviewInspectHandle,
|
|
6
|
+
type WebPluginSlotProps,
|
|
7
|
+
} from '@agimon-ai/po-core/web';
|
|
2
8
|
import { Button, DownloadIcon, fontOf } from '@agimon-ai/po-native-components';
|
|
3
9
|
import { useIsFocused } from '@react-navigation/native';
|
|
4
10
|
import { Directory, FileMode } from 'expo-file-system';
|
|
@@ -30,9 +36,125 @@ export type RichActivityRenderers = Pick<
|
|
|
30
36
|
>;
|
|
31
37
|
type StoryRequest = Parameters<NonNullable<WebPluginSlotProps['renderStoryPreview']>>[0];
|
|
32
38
|
type MediaRequest = Parameters<NonNullable<WebPluginSlotProps['renderMediaPreview']>>[0];
|
|
33
|
-
// react-native-webview 14 types its ref as unknown, so name the one method the
|
|
39
|
+
// react-native-webview 14 types its ref as unknown, so name the one method the host calls.
|
|
34
40
|
type WebViewHandle = { injectJavaScript: (script: string) => void };
|
|
35
|
-
const
|
|
41
|
+
const HandleWebView = WebView as ComponentType<ComponentProps<typeof WebView> & RefAttributes<WebViewHandle>>;
|
|
42
|
+
const INSPECT_TIMEOUT_MS = 4000;
|
|
43
|
+
// The relay envelope adds the nonce and JSON quoting around a capped payload.
|
|
44
|
+
const INSPECT_ENVELOPE_CHARS = HTML_INSPECT_LIMITS.messageChars * 2 + 512;
|
|
45
|
+
|
|
46
|
+
function inspectNonce(): string {
|
|
47
|
+
return Array.from(crypto.getRandomValues(new Uint8Array(32)), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
48
|
+
}
|
|
49
|
+
function finite(value: unknown): value is number {
|
|
50
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
51
|
+
}
|
|
52
|
+
function stale(): Error {
|
|
53
|
+
return new Error('The story preview changed.');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One generation of a live story preview's inspect bridge. Replies count only with this mount's
|
|
58
|
+
* nonce, within the message cap, for an outstanding request, and never carry page-supplied pixels.
|
|
59
|
+
* `dispose` rejects everything in flight and every later call.
|
|
60
|
+
*/
|
|
61
|
+
export function storyInspectSession(options: {
|
|
62
|
+
nonce: string;
|
|
63
|
+
inject: (script: string) => void;
|
|
64
|
+
capture: () => Promise<string>;
|
|
65
|
+
timeoutMs?: number;
|
|
66
|
+
}) {
|
|
67
|
+
const timeoutMs = options.timeoutMs ?? INSPECT_TIMEOUT_MS;
|
|
68
|
+
const pending = new Map<number, { resolve: (value: unknown) => void; reject: (reason: Error) => void }>();
|
|
69
|
+
let disposed = false;
|
|
70
|
+
let nextId = 0;
|
|
71
|
+
function bounded<T>(start: (id: number) => Promise<T> | void, signal?: AbortSignal): Promise<T> {
|
|
72
|
+
if (disposed) return Promise.reject(stale());
|
|
73
|
+
if (signal?.aborted) return Promise.reject(new Error('Inspect cancelled.'));
|
|
74
|
+
const id = ++nextId;
|
|
75
|
+
return new Promise<T>((resolve, reject) => {
|
|
76
|
+
const settle = () => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
pending.delete(id);
|
|
79
|
+
signal?.removeEventListener('abort', abort);
|
|
80
|
+
};
|
|
81
|
+
const abort = () => {
|
|
82
|
+
settle();
|
|
83
|
+
reject(new Error('Inspect cancelled.'));
|
|
84
|
+
};
|
|
85
|
+
const timer = setTimeout(() => {
|
|
86
|
+
settle();
|
|
87
|
+
reject(new Error('The story preview did not answer in time.'));
|
|
88
|
+
}, timeoutMs);
|
|
89
|
+
pending.set(id, {
|
|
90
|
+
resolve: (value) => {
|
|
91
|
+
settle();
|
|
92
|
+
resolve(value as T);
|
|
93
|
+
},
|
|
94
|
+
reject: (reason) => {
|
|
95
|
+
settle();
|
|
96
|
+
reject(reason);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
100
|
+
let started: Promise<T> | void;
|
|
101
|
+
try {
|
|
102
|
+
started = start(id);
|
|
103
|
+
} catch (reason) {
|
|
104
|
+
pending.get(id)?.reject(new Error(message(reason)));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (started)
|
|
108
|
+
started.then(
|
|
109
|
+
(value) => pending.get(id)?.resolve(value),
|
|
110
|
+
(reason: unknown) => pending.get(id)?.reject(new Error(message(reason))),
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const handle: StoryPreviewInspectHandle = {
|
|
115
|
+
inspect: (rect, signal) => {
|
|
116
|
+
if (!rect || ![rect.x, rect.y, rect.width, rect.height].every(finite))
|
|
117
|
+
return Promise.reject(new Error('Invalid inspect area.'));
|
|
118
|
+
return bounded((id) => {
|
|
119
|
+
const request = htmlInspectRequest(id, rect, { captureImage: false });
|
|
120
|
+
options.inject(`window.__poInspect&&window.__poInspect(${scriptValue(request)});true;`);
|
|
121
|
+
}, signal);
|
|
122
|
+
},
|
|
123
|
+
capture: (signal) =>
|
|
124
|
+
bounded(async () => {
|
|
125
|
+
const data = await options.capture();
|
|
126
|
+
mediaDataUri('image/png', data);
|
|
127
|
+
return { data, mimeType: 'image/png' as const };
|
|
128
|
+
}, signal),
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
handle,
|
|
132
|
+
receive(raw: unknown): void {
|
|
133
|
+
if (disposed || typeof raw !== 'string' || raw.length > INSPECT_ENVELOPE_CHARS) return;
|
|
134
|
+
let envelope: unknown;
|
|
135
|
+
try {
|
|
136
|
+
envelope = JSON.parse(raw);
|
|
137
|
+
} catch {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (typeof envelope !== 'object' || envelope === null) return;
|
|
141
|
+
const { nonce, payload } = envelope as { nonce?: unknown; payload?: unknown };
|
|
142
|
+
if (nonce !== options.nonce || typeof payload !== 'string' || payload.length > HTML_INSPECT_LIMITS.messageChars)
|
|
143
|
+
return;
|
|
144
|
+
const reply = parseHtmlInspectMessage(payload, { allowImage: false });
|
|
145
|
+
if (reply?.type !== 'inspected') return;
|
|
146
|
+
pending.get(reply.id)?.resolve({
|
|
147
|
+
view: reply.view,
|
|
148
|
+
...(reply.element ? { element: reply.element } : {}),
|
|
149
|
+
...(reply.quote ? { quote: reply.quote } : {}),
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
dispose(): void {
|
|
153
|
+
disposed = true;
|
|
154
|
+
for (const entry of pending.values()) entry.reject(stale());
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
36
158
|
|
|
37
159
|
// originWhitelist '*' avoids WebView's automatic Linking/browser fallback. Every navigation is gated here.
|
|
38
160
|
export const isolatedWebViewProps = {
|
|
@@ -62,20 +184,35 @@ export function useVisible(): boolean {
|
|
|
62
184
|
}, []);
|
|
63
185
|
return focused && active;
|
|
64
186
|
}
|
|
187
|
+
function capturePng(surface: { current: ComponentRef<typeof View> | null }): Promise<string> {
|
|
188
|
+
return captureRef(surface, { format: 'png', result: 'base64', quality: 1, handleGLSurfaceViewOnAndroid: true });
|
|
189
|
+
}
|
|
65
190
|
function HtmlPreview({ request, scripts }: { request: StoryRequest; scripts: boolean }) {
|
|
66
191
|
const surface = useRef<ComponentRef<typeof View>>(null);
|
|
192
|
+
const web = useRef<WebViewHandle>(null);
|
|
67
193
|
const visible = useVisible();
|
|
68
194
|
const [ready, setReady] = useState(false);
|
|
69
195
|
const [working, setWorking] = useState(false);
|
|
70
196
|
const [error, setError] = useState('');
|
|
71
197
|
const generation = useRef(0);
|
|
198
|
+
const inspectable = scripts && !!request.onInspectReady;
|
|
199
|
+
// Per mount: the sandboxed child never sees it, so it cannot forge relay messages.
|
|
200
|
+
const nonce = useMemo(() => (inspectable ? inspectNonce() : undefined), [inspectable]);
|
|
201
|
+
const session = useRef<ReturnType<typeof storyInspectSession> | undefined>(undefined);
|
|
202
|
+
// The host callback may change identity each render; the live handle follows the latest one.
|
|
203
|
+
const onInspectReady = useRef(request.onInspectReady);
|
|
204
|
+
useEffect(() => {
|
|
205
|
+
onInspectReady.current = request.onInspectReady;
|
|
206
|
+
});
|
|
72
207
|
const content = useMemo(() => {
|
|
73
208
|
try {
|
|
74
|
-
return {
|
|
209
|
+
return {
|
|
210
|
+
html: isolatedContent(request.html, scripts, request.title, nonce ? { inspect: { nonce } } : undefined),
|
|
211
|
+
};
|
|
75
212
|
} catch (reason) {
|
|
76
213
|
return { error: message(reason) };
|
|
77
214
|
}
|
|
78
|
-
}, [request.html, scripts, request.title]);
|
|
215
|
+
}, [request.html, scripts, request.title, nonce]);
|
|
79
216
|
useEffect(() => {
|
|
80
217
|
generation.current++;
|
|
81
218
|
setReady(false);
|
|
@@ -85,17 +222,27 @@ function HtmlPreview({ request, scripts }: { request: StoryRequest; scripts: boo
|
|
|
85
222
|
generation.current++;
|
|
86
223
|
};
|
|
87
224
|
}, [content, visible]);
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
if (!nonce || !ready || !visible || !content.html) return;
|
|
227
|
+
const current = storyInspectSession({
|
|
228
|
+
nonce,
|
|
229
|
+
inject: (script) => web.current?.injectJavaScript(script),
|
|
230
|
+
capture: () => capturePng(surface),
|
|
231
|
+
});
|
|
232
|
+
session.current = current;
|
|
233
|
+
onInspectReady.current?.(current.handle);
|
|
234
|
+
return () => {
|
|
235
|
+
current.dispose();
|
|
236
|
+
if (session.current === current) session.current = undefined;
|
|
237
|
+
onInspectReady.current?.(undefined);
|
|
238
|
+
};
|
|
239
|
+
}, [nonce, ready, visible, content]);
|
|
88
240
|
const capture = async () => {
|
|
89
241
|
if (!ready || !visible || working || !request.onCapture) return;
|
|
90
242
|
const current = generation.current;
|
|
91
243
|
setWorking(true);
|
|
92
244
|
try {
|
|
93
|
-
const data = await
|
|
94
|
-
format: 'png',
|
|
95
|
-
result: 'base64',
|
|
96
|
-
quality: 1,
|
|
97
|
-
handleGLSurfaceViewOnAndroid: true,
|
|
98
|
-
});
|
|
245
|
+
const data = await capturePng(surface);
|
|
99
246
|
mediaDataUri('image/png', data);
|
|
100
247
|
if (current === generation.current) request.onCapture({ data, mimeType: 'image/png' });
|
|
101
248
|
} catch (reason) {
|
|
@@ -108,11 +255,19 @@ function HtmlPreview({ request, scripts }: { request: StoryRequest; scripts: boo
|
|
|
108
255
|
<View style={styles.root}>
|
|
109
256
|
<View ref={surface} collapsable={false} style={styles.canvas}>
|
|
110
257
|
{visible && content.html ? (
|
|
111
|
-
<
|
|
258
|
+
<HandleWebView
|
|
259
|
+
ref={web}
|
|
112
260
|
{...isolatedWebViewProps}
|
|
113
261
|
source={{ html: content.html }}
|
|
114
262
|
javaScriptEnabled={scripts}
|
|
115
263
|
style={styles.web}
|
|
264
|
+
{...(nonce
|
|
265
|
+
? {
|
|
266
|
+
injectedJavaScriptForMainFrameOnly: true,
|
|
267
|
+
onMessage: (event: { nativeEvent: { data: string } }) =>
|
|
268
|
+
session.current?.receive(event.nativeEvent.data),
|
|
269
|
+
}
|
|
270
|
+
: {})}
|
|
116
271
|
onLoad={() => setReady(true)}
|
|
117
272
|
onError={(event) => {
|
|
118
273
|
setReady(false);
|
|
@@ -214,7 +369,7 @@ function RunnerTerminal({ request }: { request: TerminalRequest }) {
|
|
|
214
369
|
<View style={styles.root}>
|
|
215
370
|
<View style={styles.terminal}>
|
|
216
371
|
{visible ? (
|
|
217
|
-
<
|
|
372
|
+
<HandleWebView
|
|
218
373
|
ref={web}
|
|
219
374
|
{...isolatedWebViewProps}
|
|
220
375
|
source={{ html }}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { HTML_INSPECT_LIMITS, HTML_INSPECT_PREFIX, withHtmlInspectBridge } from '@agimon-ai/po-core/web';
|
|
2
|
+
|
|
1
3
|
const MAX_HTML_CHARACTERS = 4 * 1024 * 1024;
|
|
2
4
|
export const MAX_MEDIA_BYTES = 5 * 1024 * 1024;
|
|
3
5
|
const RESOURCE_POLICY =
|
|
@@ -7,13 +9,64 @@ function attribute(value: string): string {
|
|
|
7
9
|
return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>');
|
|
8
10
|
}
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
const INSPECT_NONCE = /^[A-Za-z0-9_-]{32,128}$/u;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Runs in the outer bridge document only. It forwards inspect replies from the sandboxed child,
|
|
16
|
+
* tagged with the per-mount nonce the child cannot read, and posts host requests down to it.
|
|
17
|
+
* Only replies to a request still in flight cross the native bridge, so scroll `view` updates and
|
|
18
|
+
* page spam never reach the native JS thread.
|
|
19
|
+
*/
|
|
20
|
+
function inspectRelay(nonce: string): string {
|
|
21
|
+
return `<script>(() => {
|
|
22
|
+
const nonce = ${JSON.stringify(nonce)};
|
|
23
|
+
const limit = ${HTML_INSPECT_LIMITS.messageChars};
|
|
24
|
+
const inspected = ${JSON.stringify(`${HTML_INSPECT_PREFIX}inspected`)};
|
|
25
|
+
const pending = new Set();
|
|
26
|
+
const frame = () => document.querySelector('iframe');
|
|
27
|
+
addEventListener('message', (event) => {
|
|
28
|
+
const child = frame();
|
|
29
|
+
if (!child || event.source !== child.contentWindow || !window.ReactNativeWebView) return;
|
|
30
|
+
let payload;
|
|
31
|
+
try { payload = typeof event.data === 'string' ? event.data : JSON.stringify(event.data); } catch { return; }
|
|
32
|
+
if (typeof payload !== 'string' || payload.length > limit) return;
|
|
33
|
+
let id;
|
|
34
|
+
try { const data = JSON.parse(payload); if (data && data.type === inspected) id = data.id; } catch { return; }
|
|
35
|
+
if (!pending.delete(id)) return;
|
|
36
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({ nonce, payload }));
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(window, '__poInspect', {
|
|
39
|
+
value: (message) => {
|
|
40
|
+
const child = frame();
|
|
41
|
+
if (!child || !child.contentWindow) return;
|
|
42
|
+
if (message && typeof message.id === 'number') {
|
|
43
|
+
if (pending.size >= 8) pending.clear();
|
|
44
|
+
pending.add(message.id);
|
|
45
|
+
}
|
|
46
|
+
child.contentWindow.postMessage(message, '*');
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
})();</script>`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The untrusted document is an opaque sandboxed child, never the native bridge document.
|
|
54
|
+
* `inspect` adds the page inspect bridge inside the child and a nonce-tagged relay outside it.
|
|
55
|
+
*/
|
|
56
|
+
export function isolatedContent(
|
|
57
|
+
html: string,
|
|
58
|
+
scripts: boolean,
|
|
59
|
+
title: string,
|
|
60
|
+
options?: { inspect?: { nonce: string } },
|
|
61
|
+
): string {
|
|
12
62
|
if (html.length > MAX_HTML_CHARACTERS) throw new Error('Preview HTML exceeds the local content limit.');
|
|
63
|
+
const inspect = scripts ? options?.inspect : undefined;
|
|
64
|
+
if (inspect && !INSPECT_NONCE.test(inspect.nonce)) throw new Error('Invalid preview inspect nonce.');
|
|
13
65
|
const policy = `${RESOURCE_POLICY}; script-src ${scripts ? "'unsafe-inline'" : "'none'"}`;
|
|
14
66
|
// A fresh head precedes every caller byte, including scripts before the caller's head.
|
|
15
|
-
const
|
|
16
|
-
|
|
67
|
+
const head = `<head><meta http-equiv="Content-Security-Policy" content="${policy}"></head>`;
|
|
68
|
+
const content = inspect ? withHtmlInspectBridge(`${head}${html}`) : `${head}${html}`;
|
|
69
|
+
return `<!doctype html><html><head><meta http-equiv="Content-Security-Policy" content="${policy.replace("frame-src 'none'", 'frame-src about:')}"><meta name="viewport" content="width=device-width, initial-scale=1"><style>html,body,iframe{margin:0;width:100%;height:100%;border:0}</style></head><body><iframe title="${attribute(title)}" sandbox="${scripts ? 'allow-scripts' : ''}" srcdoc="${attribute(content)}"></iframe>${inspect ? inspectRelay(inspect.nonce) : ''}</body></html>`;
|
|
17
70
|
}
|
|
18
71
|
|
|
19
72
|
/** The byte count a padded base64 string decodes to. */
|