@agent-native/core 0.64.0 → 0.65.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/dist/embedding/agent.d.ts +32 -0
- package/dist/embedding/agent.d.ts.map +1 -0
- package/dist/embedding/agent.js +110 -0
- package/dist/embedding/agent.js.map +1 -0
- package/dist/embedding/bridge.d.ts +37 -0
- package/dist/embedding/bridge.d.ts.map +1 -0
- package/dist/embedding/bridge.js +148 -0
- package/dist/embedding/bridge.js.map +1 -0
- package/dist/embedding/index.d.ts +5 -0
- package/dist/embedding/index.d.ts.map +1 -0
- package/dist/embedding/index.js +5 -0
- package/dist/embedding/index.js.map +1 -0
- package/dist/embedding/protocol.d.ts +46 -0
- package/dist/embedding/protocol.d.ts.map +1 -0
- package/dist/embedding/protocol.js +122 -0
- package/dist/embedding/protocol.js.map +1 -0
- package/dist/embedding/react.d.ts +39 -0
- package/dist/embedding/react.d.ts.map +1 -0
- package/dist/embedding/react.js +147 -0
- package/dist/embedding/react.js.map +1 -0
- package/package.json +23 -18
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { A2AClient, type Message, type Task } from "../a2a/index.js";
|
|
2
|
+
export interface AgentEndpointOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Optional URL base used when resolving relative app URLs.
|
|
5
|
+
*/
|
|
6
|
+
base?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface SendMessageOptions {
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
contextId?: string;
|
|
11
|
+
metadata?: Record<string, unknown>;
|
|
12
|
+
requestTimeoutMs?: number;
|
|
13
|
+
/**
|
|
14
|
+
* If the target does not support streaming, fall back to async send + poll.
|
|
15
|
+
* Defaults to true.
|
|
16
|
+
*/
|
|
17
|
+
fallbackToPolling?: boolean;
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
userEmail?: string;
|
|
20
|
+
orgDomain?: string;
|
|
21
|
+
orgSecret?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function getMcpUrl(url: string, options?: AgentEndpointOptions): string;
|
|
24
|
+
export declare function getA2AUrl(url: string, options?: AgentEndpointOptions): string;
|
|
25
|
+
export declare function getAgentCardUrl(url: string, options?: AgentEndpointOptions): string;
|
|
26
|
+
/**
|
|
27
|
+
* Send a text prompt to an Agent-Native A2A endpoint and yield text deltas.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sendMessage(url: string, text: string, options?: SendMessageOptions): AsyncGenerator<string>;
|
|
30
|
+
export { A2AClient };
|
|
31
|
+
export type { Message, Task };
|
|
32
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/embedding/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAa,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEhF,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAiBD,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,oBAAyB,GACjC,MAAM,CAWR;AAED,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,oBAAyB,GACjC,MAAM,CAiBR;AAED,wBAAgB,eAAe,CAC7B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,oBAAyB,GACjC,MAAM,CAMR;AAoBD;;GAEG;AACH,wBAAuB,WAAW,CAChC,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,kBAAuB,GAC/B,cAAc,CAAC,MAAM,CAAC,CAsCxB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC;AACrB,YAAY,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { A2AClient, callAgent } from "../a2a/index.js";
|
|
2
|
+
function appUrl(url, options = {}) {
|
|
3
|
+
const base = options.base ??
|
|
4
|
+
(typeof window !== "undefined"
|
|
5
|
+
? window.location.href
|
|
6
|
+
: "http://agent-native.local");
|
|
7
|
+
return new URL(url, base);
|
|
8
|
+
}
|
|
9
|
+
function trimEndpointPath(pathname, suffix) {
|
|
10
|
+
const normalized = pathname.replace(/\/$/, "");
|
|
11
|
+
if (!normalized.endsWith(suffix))
|
|
12
|
+
return null;
|
|
13
|
+
return normalized.slice(0, -suffix.length) || "/";
|
|
14
|
+
}
|
|
15
|
+
export function getMcpUrl(url, options = {}) {
|
|
16
|
+
const parsed = appUrl(url, options);
|
|
17
|
+
const trimmed = trimEndpointPath(parsed.pathname, "/_agent-native/mcp");
|
|
18
|
+
if (trimmed !== null) {
|
|
19
|
+
parsed.pathname = `${trimmed.replace(/\/$/, "")}/_agent-native/mcp`;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
parsed.pathname = `${parsed.pathname.replace(/\/$/, "")}/_agent-native/mcp`;
|
|
23
|
+
}
|
|
24
|
+
parsed.search = "";
|
|
25
|
+
parsed.hash = "";
|
|
26
|
+
return parsed.toString();
|
|
27
|
+
}
|
|
28
|
+
export function getA2AUrl(url, options = {}) {
|
|
29
|
+
const parsed = appUrl(url, options);
|
|
30
|
+
const agentNativeTrimmed = trimEndpointPath(parsed.pathname, "/_agent-native/a2a");
|
|
31
|
+
const legacyTrimmed = trimEndpointPath(parsed.pathname, "/a2a");
|
|
32
|
+
if (agentNativeTrimmed !== null) {
|
|
33
|
+
parsed.pathname = `${agentNativeTrimmed.replace(/\/$/, "")}/_agent-native/a2a`;
|
|
34
|
+
}
|
|
35
|
+
else if (legacyTrimmed !== null) {
|
|
36
|
+
parsed.pathname = `${legacyTrimmed.replace(/\/$/, "")}/_agent-native/a2a`;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
parsed.pathname = `${parsed.pathname.replace(/\/$/, "")}/_agent-native/a2a`;
|
|
40
|
+
}
|
|
41
|
+
parsed.search = "";
|
|
42
|
+
parsed.hash = "";
|
|
43
|
+
return parsed.toString();
|
|
44
|
+
}
|
|
45
|
+
export function getAgentCardUrl(url, options = {}) {
|
|
46
|
+
const parsed = appUrl(url, options);
|
|
47
|
+
parsed.pathname = "/.well-known/agent-card.json";
|
|
48
|
+
parsed.search = "";
|
|
49
|
+
parsed.hash = "";
|
|
50
|
+
return parsed.toString();
|
|
51
|
+
}
|
|
52
|
+
function textMessage(text) {
|
|
53
|
+
return {
|
|
54
|
+
role: "user",
|
|
55
|
+
parts: [{ type: "text", text }],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function extractTaskText(task) {
|
|
59
|
+
return (task.status.message?.parts
|
|
60
|
+
.filter((part) => {
|
|
61
|
+
return part.type === "text" && typeof part.text === "string";
|
|
62
|
+
})
|
|
63
|
+
.map((part) => part.text)
|
|
64
|
+
.join("\n") ?? "");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Send a text prompt to an Agent-Native A2A endpoint and yield text deltas.
|
|
68
|
+
*/
|
|
69
|
+
export async function* sendMessage(url, text, options = {}) {
|
|
70
|
+
const client = new A2AClient(url, options.apiKey, {
|
|
71
|
+
requestTimeoutMs: options.requestTimeoutMs,
|
|
72
|
+
});
|
|
73
|
+
const message = textMessage(text);
|
|
74
|
+
let previous = "";
|
|
75
|
+
let yielded = false;
|
|
76
|
+
try {
|
|
77
|
+
for await (const task of client.stream(message, {
|
|
78
|
+
contextId: options.contextId,
|
|
79
|
+
metadata: options.metadata,
|
|
80
|
+
})) {
|
|
81
|
+
const next = extractTaskText(task);
|
|
82
|
+
const chunk = next.startsWith(previous)
|
|
83
|
+
? next.slice(previous.length)
|
|
84
|
+
: next;
|
|
85
|
+
previous = next;
|
|
86
|
+
if (chunk) {
|
|
87
|
+
yielded = true;
|
|
88
|
+
yield chunk;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (yielded || options.fallbackToPolling === false)
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
const answer = await callAgent(url, text, {
|
|
98
|
+
apiKey: options.apiKey,
|
|
99
|
+
contextId: options.contextId,
|
|
100
|
+
timeoutMs: options.timeoutMs,
|
|
101
|
+
userEmail: options.userEmail,
|
|
102
|
+
orgDomain: options.orgDomain,
|
|
103
|
+
orgSecret: options.orgSecret,
|
|
104
|
+
async: true,
|
|
105
|
+
});
|
|
106
|
+
if (answer)
|
|
107
|
+
yield answer;
|
|
108
|
+
}
|
|
109
|
+
export { A2AClient };
|
|
110
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/embedding/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAA2B,MAAM,iBAAiB,CAAC;AAyBhF,SAAS,MAAM,CAAC,GAAW,EAAE,UAAgC,EAAE;IAC7D,MAAM,IAAI,GACR,OAAO,CAAC,IAAI;QACZ,CAAC,OAAO,MAAM,KAAK,WAAW;YAC5B,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI;YACtB,CAAC,CAAC,2BAA2B,CAAC,CAAC;IACnC,OAAO,I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AC;QACf,IAAI,OAAO,IAAI,OAAO,CAAC,iBAAiB,KAAK,KAAK;YAAE,MAAM,KAAK,CAAC;IAClE,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE;QACxC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,KAAK,EAAE,IAAI;KACZ,CAAC,CAAC;IACH,IAAI,MAAM;QAAE,MAAM,MAAM,CAAC;AAC3B,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC","sourcesContent":["import { A2AClient, callAgent, type Message, type Task } from \"../a2a/index.js\";\n\nexport interface AgentEndpointOptions {\n /**\n * Optional URL base used when resolving relative app URLs.\n */\n base?: string;\n}\n\nexport interface SendMessageOptions {\n apiKey?: string;\n contextId?: string;\n metadata?: Record<string, unknown>;\n requestTimeoutMs?: number;\n /**\n * If the target does not support streaming, fall back to async send + poll.\n * Defaults to true.\n */\n fallbackToPolling?: boolean;\n timeoutMs?: number;\n userEmail?: string;\n orgDomain?: string;\n orgSecret?: string;\n}\n\nfunction appUrl(url: string, options: AgentEndpointOptions = {}): URL {\n const base =\n options.base ??\n (typeof window !== \"undefined\"\n ? window.location.href\n : \"http://agent-native.local\");\n return new URL(url, base);\n}\n\nfunction trimEndpointPath(pathname: string, suffix: string): string | null {\n const normalized = pathname.replace(/\\/$/, \"\");\n if (!normalized.endsWith(suffix)) return null;\n return normalized.slice(0, -suffix.length) || \"/\";\n}\n\nexport function getMcpUrl(\n url: string,\n options: AgentEndpointOptions = {},\n): string {\n const parsed = appUrl(url, options);\n const trimmed = trimEndpointPath(parsed.pathname, \"/_agent-native/mcp\");\n if (trimmed !== null) {\n parsed.pathname = `${trimmed.replace(/\\/$/, \"\")}/_agent-native/mcp`;\n } else {\n parsed.pathname = `${parsed.pathname.replace(/\\/$/, \"\")}/_agent-native/mcp`;\n }\n parsed.search = \"\";\n parsed.hash = \"\";\n return parsed.toString();\n}\n\nexport function getA2AUrl(\n url: string,\n options: AgentEndpointOptions = {},\n): string {\n const parsed = appUrl(url, options);\n const agentNativeTrimmed = trimEndpointPath(\n parsed.pathname,\n \"/_agent-native/a2a\",\n );\n const legacyTrimmed = trimEndpointPath(parsed.pathname, \"/a2a\");\n if (agentNativeTrimmed !== null) {\n parsed.pathname = `${agentNativeTrimmed.replace(/\\/$/, \"\")}/_agent-native/a2a`;\n } else if (legacyTrimmed !== null) {\n parsed.pathname = `${legacyTrimmed.replace(/\\/$/, \"\")}/_agent-native/a2a`;\n } else {\n parsed.pathname = `${parsed.pathname.replace(/\\/$/, \"\")}/_agent-native/a2a`;\n }\n parsed.search = \"\";\n parsed.hash = \"\";\n return parsed.toString();\n}\n\nexport function getAgentCardUrl(\n url: string,\n options: AgentEndpointOptions = {},\n): string {\n const parsed = appUrl(url, options);\n parsed.pathname = \"/.well-known/agent-card.json\";\n parsed.search = \"\";\n parsed.hash = \"\";\n return parsed.toString();\n}\n\nfunction textMessage(text: string): Message {\n return {\n role: \"user\",\n parts: [{ type: \"text\", text }],\n };\n}\n\nfunction extractTaskText(task: Task): string {\n return (\n task.status.message?.parts\n .filter((part): part is { type: \"text\"; text: string } => {\n return part.type === \"text\" && typeof part.text === \"string\";\n })\n .map((part) => part.text)\n .join(\"\\n\") ?? \"\"\n );\n}\n\n/**\n * Send a text prompt to an Agent-Native A2A endpoint and yield text deltas.\n */\nexport async function* sendMessage(\n url: string,\n text: string,\n options: SendMessageOptions = {},\n): AsyncGenerator<string> {\n const client = new A2AClient(url, options.apiKey, {\n requestTimeoutMs: options.requestTimeoutMs,\n });\n const message = textMessage(text);\n let previous = \"\";\n let yielded = false;\n\n try {\n for await (const task of client.stream(message, {\n contextId: options.contextId,\n metadata: options.metadata,\n })) {\n const next = extractTaskText(task);\n const chunk = next.startsWith(previous)\n ? next.slice(previous.length)\n : next;\n previous = next;\n if (chunk) {\n yielded = true;\n yield chunk;\n }\n }\n return;\n } catch (error) {\n if (yielded || options.fallbackToPolling === false) throw error;\n }\n\n const answer = await callAgent(url, text, {\n apiKey: options.apiKey,\n contextId: options.contextId,\n timeoutMs: options.timeoutMs,\n userEmail: options.userEmail,\n orgDomain: options.orgDomain,\n orgSecret: options.orgSecret,\n async: true,\n });\n if (answer) yield answer;\n}\n\nexport { A2AClient };\nexport type { Message, Task };\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface EmbeddedAppMessageEvent<TPayload = unknown> {
|
|
2
|
+
name: string;
|
|
3
|
+
payload: TPayload;
|
|
4
|
+
event: MessageEvent;
|
|
5
|
+
}
|
|
6
|
+
export type EmbeddedAppMessageHandler = (message: EmbeddedAppMessageEvent) => void;
|
|
7
|
+
export type EmbeddedAppRequestHandler = (message: EmbeddedAppMessageEvent) => unknown | Promise<unknown>;
|
|
8
|
+
export interface EmbeddedAppBridgeOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Exact parent origin to post back to. Defaults to document.referrer's
|
|
11
|
+
* origin when available. If no origin can be resolved, sends fail closed.
|
|
12
|
+
*/
|
|
13
|
+
parentOrigin?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Origins allowed to send messages to the embedded app. Defaults to the
|
|
16
|
+
* resolved parent origin.
|
|
17
|
+
*/
|
|
18
|
+
allowedOrigins?: string[];
|
|
19
|
+
onMessage?: EmbeddedAppMessageHandler;
|
|
20
|
+
onRequest?: EmbeddedAppRequestHandler;
|
|
21
|
+
targetWindow?: Window | null;
|
|
22
|
+
currentWindow?: Window;
|
|
23
|
+
}
|
|
24
|
+
export interface EmbeddedAppBridge {
|
|
25
|
+
parentOrigin: string | null;
|
|
26
|
+
postMessage<TPayload = unknown>(name: string, payload?: TPayload): boolean;
|
|
27
|
+
request<TResult = unknown, TPayload = unknown>(name: string, payload?: TPayload, options?: {
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
}): Promise<TResult>;
|
|
30
|
+
ready<TPayload = unknown>(payload?: TPayload): boolean;
|
|
31
|
+
close<TPayload = unknown>(payload?: TPayload): boolean;
|
|
32
|
+
destroy(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function createEmbeddedAppBridge(options?: EmbeddedAppBridgeOptions): EmbeddedAppBridge;
|
|
35
|
+
export declare function sendEmbeddedAppMessage<TPayload = unknown>(name: string, payload?: TPayload, options?: Pick<EmbeddedAppBridgeOptions, "currentWindow" | "parentOrigin" | "targetWindow">): boolean;
|
|
36
|
+
export declare function announceEmbeddedAppReady<TPayload = unknown>(payload?: TPayload, options?: Pick<EmbeddedAppBridgeOptions, "currentWindow" | "parentOrigin" | "targetWindow">): boolean;
|
|
37
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../../src/embedding/bridge.ts"],"names":[],"mappings":"AAWA,MAAM,WAAW,uBAAuB,CAAC,QAAQ,GAAG,OAAO;IACzD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,QAAQ,CAAC;IAClB,KAAK,EAAE,YAAY,CAAC;CACrB;AAED,MAAM,MAAM,yBAAyB,GAAG,CACtC,OAAO,EAAE,uBAAuB,KAC7B,IAAI,CAAC;AAEV,MAAM,MAAM,yBAAyB,GAAG,CACtC,OAAO,EAAE,uBAAuB,KAC7B,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhC,MAAM,WAAW,wBAAwB;IACvC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,SAAS,CAAC,EAAE,yBAAyB,CAAC;IACtC,SAAS,CAAC,EAAE,yBAAyB,CAAC;IACtC,YAAY,CA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, createEmbeddedAppRequestId, embeddedAppOrigin, isAgentNativeEmbedEnvelope, isAllowedEmbeddedAppOrigin, messageErrorPayload, } from "./protocol.js";
|
|
2
|
+
function resolveParentOrigin(win, explicit) {
|
|
3
|
+
if (explicit)
|
|
4
|
+
return explicit;
|
|
5
|
+
const referrerOrigin = embeddedAppOrigin(win.document?.referrer ?? "");
|
|
6
|
+
return referrerOrigin;
|
|
7
|
+
}
|
|
8
|
+
function postToParent(win, parent, parentOrigin, envelope) {
|
|
9
|
+
const target = parent ?? (win.parent === win ? null : win.parent);
|
|
10
|
+
if (!parentOrigin)
|
|
11
|
+
return false;
|
|
12
|
+
if (!target)
|
|
13
|
+
return false;
|
|
14
|
+
target.postMessage(envelope, parentOrigin);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
export function createEmbeddedAppBridge(options = {}) {
|
|
18
|
+
const win = options.currentWindow ?? window;
|
|
19
|
+
const parentOrigin = resolveParentOrigin(win, options.parentOrigin);
|
|
20
|
+
const allowedOrigins = options.allowedOrigins ??
|
|
21
|
+
(parentOrigin && parentOrigin !== "*" ? [parentOrigin] : []);
|
|
22
|
+
const pending = new Map();
|
|
23
|
+
let destroyed = false;
|
|
24
|
+
const postEnvelope = (envelope) => postToParent(win, options.targetWindow ?? null, parentOrigin, envelope);
|
|
25
|
+
const listener = (event) => {
|
|
26
|
+
if (destroyed)
|
|
27
|
+
return;
|
|
28
|
+
if (!isAllowedEmbeddedAppOrigin(event.origin, allowedOrigins))
|
|
29
|
+
return;
|
|
30
|
+
if (!isAgentNativeEmbedEnvelope(event.data))
|
|
31
|
+
return;
|
|
32
|
+
const envelope = event.data;
|
|
33
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE) {
|
|
34
|
+
const requestId = envelope.requestId;
|
|
35
|
+
if (!requestId)
|
|
36
|
+
return;
|
|
37
|
+
const entry = pending.get(requestId);
|
|
38
|
+
if (!entry)
|
|
39
|
+
return;
|
|
40
|
+
pending.delete(requestId);
|
|
41
|
+
if (entry.timer)
|
|
42
|
+
clearTimeout(entry.timer);
|
|
43
|
+
entry.resolve(envelope.payload);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR) {
|
|
47
|
+
const requestId = envelope.requestId;
|
|
48
|
+
if (!requestId)
|
|
49
|
+
return;
|
|
50
|
+
const entry = pending.get(requestId);
|
|
51
|
+
if (!entry)
|
|
52
|
+
return;
|
|
53
|
+
pending.delete(requestId);
|
|
54
|
+
if (entry.timer)
|
|
55
|
+
clearTimeout(entry.timer);
|
|
56
|
+
entry.reject(new Error(envelope.error?.message ?? "Embedded app error"));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!envelope.name)
|
|
60
|
+
return;
|
|
61
|
+
const message = {
|
|
62
|
+
name: envelope.name,
|
|
63
|
+
payload: envelope.payload,
|
|
64
|
+
event,
|
|
65
|
+
};
|
|
66
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST) {
|
|
67
|
+
Promise.resolve(options.onRequest?.(message))
|
|
68
|
+
.then((payload) => {
|
|
69
|
+
postEnvelope(createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE, { requestId: envelope.requestId, payload }));
|
|
70
|
+
})
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
postEnvelope(createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR, {
|
|
73
|
+
requestId: envelope.requestId,
|
|
74
|
+
error: messageErrorPayload(error),
|
|
75
|
+
}));
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE) {
|
|
80
|
+
options.onMessage?.(message);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
win.addEventListener("message", listener);
|
|
84
|
+
return {
|
|
85
|
+
parentOrigin,
|
|
86
|
+
postMessage(name, payload) {
|
|
87
|
+
return postEnvelope(createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE, { name, payload }));
|
|
88
|
+
},
|
|
89
|
+
request(name, payload, requestOptions) {
|
|
90
|
+
const requestId = createEmbeddedAppRequestId();
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const timeoutMs = requestOptions?.timeoutMs ?? 30_000;
|
|
93
|
+
const timer = timeoutMs > 0
|
|
94
|
+
? setTimeout(() => {
|
|
95
|
+
pending.delete(requestId);
|
|
96
|
+
reject(new Error(`Embedded app request timed out: ${name}`));
|
|
97
|
+
}, timeoutMs)
|
|
98
|
+
: undefined;
|
|
99
|
+
pending.set(requestId, {
|
|
100
|
+
resolve: (value) => resolve(value),
|
|
101
|
+
reject,
|
|
102
|
+
timer,
|
|
103
|
+
});
|
|
104
|
+
const posted = postEnvelope(createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST, { name, payload, requestId }));
|
|
105
|
+
if (!posted) {
|
|
106
|
+
pending.delete(requestId);
|
|
107
|
+
if (timer)
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
reject(new Error("Embedded app parent window is not available"));
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
ready(payload) {
|
|
114
|
+
return postEnvelope(createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY, {
|
|
115
|
+
payload,
|
|
116
|
+
}));
|
|
117
|
+
},
|
|
118
|
+
close(payload) {
|
|
119
|
+
return this.postMessage("close", payload);
|
|
120
|
+
},
|
|
121
|
+
destroy() {
|
|
122
|
+
destroyed = true;
|
|
123
|
+
win.removeEventListener("message", listener);
|
|
124
|
+
for (const entry of pending.values()) {
|
|
125
|
+
if (entry.timer)
|
|
126
|
+
clearTimeout(entry.timer);
|
|
127
|
+
entry.reject(new Error("Embedded app bridge destroyed"));
|
|
128
|
+
}
|
|
129
|
+
pending.clear();
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
export function sendEmbeddedAppMessage(name, payload, options = {}) {
|
|
134
|
+
const win = options.currentWindow ?? window;
|
|
135
|
+
const parentOrigin = resolveParentOrigin(win, options.parentOrigin);
|
|
136
|
+
return postToParent(win, options.targetWindow ?? null, parentOrigin, createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE, {
|
|
137
|
+
name,
|
|
138
|
+
payload,
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
export function announceEmbeddedAppReady(payload, options = {}) {
|
|
142
|
+
const win = options.currentWindow ?? window;
|
|
143
|
+
const parentOrigin = resolveParentOrigin(win, options.parentOrigin);
|
|
144
|
+
return postToParent(win, options.targetWindow ?? null, parentOrigin, createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY, {
|
|
145
|
+
payload,
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../../src/embedding/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC1B,iBAAiB,EACjB,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,GAEpB,MAAM,eAAe,CAAC;AAoDvB,SAAS,mBAAmB,CAAC,GAAW,EAAE,QAAiB;IACzD,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC9B,MAAM,cAAc,GAAG,iBAAiB,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAC;IACvE,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,SAAS,YAAY,CACnB,GAAW,EACX,MAAqB,EACrB,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AC,CAAC;gBAC3C,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC,CAAC;YAC3D,CAAC;YACD,OAAO,CAAC,KAAK,EAAE,CAAC;QAClB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,sBAAsB,CACpC,IAAY,EACZ,OAAkB,EAClB,UAGI,EAAE;IAEN,MAAM,GAAG,GAAG,OAAO,CAAC,aAAa,IAAI,MAAM,CAAC;IAC5C,MAAM,YAAY,GAAG,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IACpE,OAAO,YAAY,CACjB,GAAG,EACH,OAAO,CAAC,YAAY,IAAI,IAAI,EAC5B,YAAY,EACZ,8BAA8B,CAAC,gCAAgC,CAAC,OAAO,EAAE;QACvE,IAAI;QACJ,OAAO;KACR,CAAC,CACH,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,OAAkB,EAClB,UAGI,EAAE;IAEN,MAAM,GAAG,GAAG,OAAO,CAAC,aAAa,IAAI,MAAM,CAAC;IAC5C,MAAM,YAAY,GAAG,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IACpE,OAAO,YAAY,CACjB,GAAG,EACH,OAAO,CAAC,YAAY,IAAI,IAAI,EAC5B,YAAY,EACZ,8BAA8B,CAAC,gCAAgC,CAAC,KAAK,EAAE;QACrE,OAAO;KACR,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n AGENT_NATIVE_EMBED_MESSAGE_TYPES,\n createAgentNativeEmbedEnvelope,\n createEmbeddedAppRequestId,\n embeddedAppOrigin,\n isAgentNativeEmbedEnvelope,\n isAllowedEmbeddedAppOrigin,\n messageErrorPayload,\n type AgentNativeEmbedEnvelope,\n} from \"./protocol.js\";\n\nexport interface EmbeddedAppMessageEvent<TPayload = unknown> {\n name: string;\n payload: TPayload;\n event: MessageEvent;\n}\n\nexport type EmbeddedAppMessageHandler = (\n message: EmbeddedAppMessageEvent,\n) => void;\n\nexport type EmbeddedAppRequestHandler = (\n message: EmbeddedAppMessageEvent,\n) => unknown | Promise<unknown>;\n\nexport interface EmbeddedAppBridgeOptions {\n /**\n * Exact parent origin to post back to. Defaults to document.referrer's\n * origin when available. If no origin can be resolved, sends fail closed.\n */\n parentOrigin?: string;\n /**\n * Origins allowed to send messages to the embedded app. Defaults to the\n * resolved parent origin.\n */\n allowedOrigins?: string[];\n onMessage?: EmbeddedAppMessageHandler;\n onRequest?: EmbeddedAppRequestHandler;\n targetWindow?: Window | null;\n currentWindow?: Window;\n}\n\nexport interface EmbeddedAppBridge {\n parentOrigin: string | null;\n postMessage<TPayload = unknown>(name: string, payload?: TPayload): boolean;\n request<TResult = unknown, TPayload = unknown>(\n name: string,\n payload?: TPayload,\n options?: { timeoutMs?: number },\n ): Promise<TResult>;\n ready<TPayload = unknown>(payload?: TPayload): boolean;\n close<TPayload = unknown>(payload?: TPayload): boolean;\n destroy(): void;\n}\n\ntype PendingRequest = {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n timer: ReturnType<typeof setTimeout> | undefined;\n};\n\nfunction resolveParentOrigin(win: Window, explicit?: string): string | null {\n if (explicit) return explicit;\n const referrerOrigin = embeddedAppOrigin(win.document?.referrer ?? \"\");\n return referrerOrigin;\n}\n\nfunction postToParent(\n win: Window,\n parent: Window | null,\n parentOrigin: string | null,\n envelope: AgentNativeEmbedEnvelope,\n): boolean {\n const target = parent ?? (win.parent === win ? null : win.parent);\n if (!parentOrigin) return false;\n if (!target) return false;\n target.postMessage(envelope, parentOrigin);\n return true;\n}\n\nexport function createEmbeddedAppBridge(\n options: EmbeddedAppBridgeOptions = {},\n): EmbeddedAppBridge {\n const win = options.currentWindow ?? window;\n const parentOrigin = resolveParentOrigin(win, options.parentOrigin);\n const allowedOrigins =\n options.allowedOrigins ??\n (parentOrigin && parentOrigin !== \"*\" ? [parentOrigin] : []);\n const pending = new Map<string, PendingRequest>();\n let destroyed = false;\n\n const postEnvelope = (envelope: AgentNativeEmbedEnvelope) =>\n postToParent(win, options.targetWindow ?? null, parentOrigin, envelope);\n\n const listener = (event: MessageEvent) => {\n if (destroyed) return;\n if (!isAllowedEmbeddedAppOrigin(event.origin, allowedOrigins)) return;\n if (!isAgentNativeEmbedEnvelope(event.data)) return;\n\n const envelope = event.data;\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE) {\n const requestId = envelope.requestId;\n if (!requestId) return;\n const entry = pending.get(requestId);\n if (!entry) return;\n pending.delete(requestId);\n if (entry.timer) clearTimeout(entry.timer);\n entry.resolve(envelope.payload);\n return;\n }\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR) {\n const requestId = envelope.requestId;\n if (!requestId) return;\n const entry = pending.get(requestId);\n if (!entry) return;\n pending.delete(requestId);\n if (entry.timer) clearTimeout(entry.timer);\n entry.reject(new Error(envelope.error?.message ?? \"Embedded app error\"));\n return;\n }\n\n if (!envelope.name) return;\n const message = {\n name: envelope.name,\n payload: envelope.payload,\n event,\n } satisfies EmbeddedAppMessageEvent;\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST) {\n Promise.resolve(options.onRequest?.(message))\n .then((payload) => {\n postEnvelope(\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE,\n { requestId: envelope.requestId, payload },\n ),\n );\n })\n .catch((error) => {\n postEnvelope(\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR,\n {\n requestId: envelope.requestId,\n error: messageErrorPayload(error),\n },\n ),\n );\n });\n return;\n }\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE) {\n options.onMessage?.(message);\n }\n };\n\n win.addEventListener(\"message\", listener);\n\n return {\n parentOrigin,\n postMessage(name, payload) {\n return postEnvelope(\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE,\n { name, payload },\n ),\n );\n },\n request<TResult = unknown, TPayload = unknown>(\n name: string,\n payload?: TPayload,\n requestOptions?: { timeoutMs?: number },\n ) {\n const requestId = createEmbeddedAppRequestId();\n return new Promise<TResult>((resolve, reject) => {\n const timeoutMs = requestOptions?.timeoutMs ?? 30_000;\n const timer =\n timeoutMs > 0\n ? setTimeout(() => {\n pending.delete(requestId);\n reject(new Error(`Embedded app request timed out: ${name}`));\n }, timeoutMs)\n : undefined;\n pending.set(requestId, {\n resolve: (value) => resolve(value as TResult),\n reject,\n timer,\n });\n const posted = postEnvelope(\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST,\n { name, payload, requestId },\n ),\n );\n if (!posted) {\n pending.delete(requestId);\n if (timer) clearTimeout(timer);\n reject(new Error(\"Embedded app parent window is not available\"));\n }\n });\n },\n ready(payload) {\n return postEnvelope(\n createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY, {\n payload,\n }),\n );\n },\n close(payload) {\n return this.postMessage(\"close\", payload);\n },\n destroy() {\n destroyed = true;\n win.removeEventListener(\"message\", listener);\n for (const entry of pending.values()) {\n if (entry.timer) clearTimeout(entry.timer);\n entry.reject(new Error(\"Embedded app bridge destroyed\"));\n }\n pending.clear();\n },\n };\n}\n\nexport function sendEmbeddedAppMessage<TPayload = unknown>(\n name: string,\n payload?: TPayload,\n options: Pick<\n EmbeddedAppBridgeOptions,\n \"currentWindow\" | \"parentOrigin\" | \"targetWindow\"\n > = {},\n): boolean {\n const win = options.currentWindow ?? window;\n const parentOrigin = resolveParentOrigin(win, options.parentOrigin);\n return postToParent(\n win,\n options.targetWindow ?? null,\n parentOrigin,\n createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE, {\n name,\n payload,\n }),\n );\n}\n\nexport function announceEmbeddedAppReady<TPayload = unknown>(\n payload?: TPayload,\n options: Pick<\n EmbeddedAppBridgeOptions,\n \"currentWindow\" | \"parentOrigin\" | \"targetWindow\"\n > = {},\n): boolean {\n const win = options.currentWindow ?? window;\n const parentOrigin = resolveParentOrigin(win, options.parentOrigin);\n return postToParent(\n win,\n options.targetWindow ?? null,\n parentOrigin,\n createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY, {\n payload,\n }),\n );\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { EmbeddedApp, type EmbeddedAppMessageInfo, type EmbeddedAppProps, type EmbeddedAppRef, } from "./react.js";
|
|
2
|
+
export { announceEmbeddedAppReady, createEmbeddedAppBridge, sendEmbeddedAppMessage, type EmbeddedAppBridge, type EmbeddedAppBridgeOptions, type EmbeddedAppMessageEvent, type EmbeddedAppMessageHandler, type EmbeddedAppRequestHandler, } from "./bridge.js";
|
|
3
|
+
export { A2AClient, getA2AUrl, getAgentCardUrl, getMcpUrl, sendMessage, type AgentEndpointOptions, type Message, type SendMessageOptions, type Task, } from "./agent.js";
|
|
4
|
+
export { AGENT_NATIVE_EMBED_MESSAGE_TYPES, AGENT_NATIVE_EMBED_PROTOCOL, AGENT_NATIVE_EMBED_VERSION, createAgentNativeEmbedEnvelope, createEmbeddedAppRequestId, embeddedAppOrigin, isAgentNativeEmbedEnvelope, isAllowedEmbeddedAppOrigin, messageErrorPayload, withEmbeddedAppParams, type AgentNativeEmbedEnvelope, type AgentNativeEmbedErrorPayload, type AgentNativeEmbedMessageType, type EmbeddedAppUrlOptions, } from "./protocol.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/embedding/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,KAAK,sBAAsB,EAC3B,KAAK,gBAAgB,EACrB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,EACtB,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,EAC7B,KAAK,uBAAuB,EAC5B,KAAK,yBAAyB,EAC9B,KAAK,yBAAyB,GAC/B,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,SAAS,EACT,SAAS,EACT,eAAe,EACf,SAAS,EACT,WAAW,EACX,KAAK,oBAAoB,EACzB,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACvB,KAAK,IAAI,GACV,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,8BAA8B,EAC9B,0BAA0B,EAC1B,iBAAiB,EACjB,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,wBAAwB,EAC7B,KAAK,4BAA4B,EACjC,KAAK,2BAA2B,EAChC,KAAK,qBAAqB,GAC3B,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { EmbeddedApp, } from "./react.js";
|
|
2
|
+
export { announceEmbeddedAppReady, createEmbeddedAppBridge, sendEmbeddedAppMessage, } from "./bridge.js";
|
|
3
|
+
export { A2AClient, getA2AUrl, getAgentCardUrl, getMcpUrl, sendMessage, } from "./agent.js";
|
|
4
|
+
export { AGENT_NATIVE_EMBED_MESSAGE_TYPES, AGENT_NATIVE_EMBED_PROTOCOL, AGENT_NATIVE_EMBED_VERSION, createAgentNativeEmbedEnvelope, createEmbeddedAppRequestId, embeddedAppOrigin, isAgentNativeEmbedEnvelope, isAllowedEmbeddedAppOrigin, messageErrorPayload, withEmbeddedAppParams, } from "./protocol.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/embedding/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,GAIZ,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,GAMvB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,SAAS,EACT,SAAS,EACT,eAAe,EACf,SAAS,EACT,WAAW,GAKZ,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,8BAA8B,EAC9B,0BAA0B,EAC1B,iBAAiB,EACjB,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,GAKtB,MAAM,eAAe,CAAC","sourcesContent":["export {\n EmbeddedApp,\n type EmbeddedAppMessageInfo,\n type EmbeddedAppProps,\n type EmbeddedAppRef,\n} from \"./react.js\";\nexport {\n announceEmbeddedAppReady,\n createEmbeddedAppBridge,\n sendEmbeddedAppMessage,\n type EmbeddedAppBridge,\n type EmbeddedAppBridgeOptions,\n type EmbeddedAppMessageEvent,\n type EmbeddedAppMessageHandler,\n type EmbeddedAppRequestHandler,\n} from \"./bridge.js\";\nexport {\n A2AClient,\n getA2AUrl,\n getAgentCardUrl,\n getMcpUrl,\n sendMessage,\n type AgentEndpointOptions,\n type Message,\n type SendMessageOptions,\n type Task,\n} from \"./agent.js\";\nexport {\n AGENT_NATIVE_EMBED_MESSAGE_TYPES,\n AGENT_NATIVE_EMBED_PROTOCOL,\n AGENT_NATIVE_EMBED_VERSION,\n createAgentNativeEmbedEnvelope,\n createEmbeddedAppRequestId,\n embeddedAppOrigin,\n isAgentNativeEmbedEnvelope,\n isAllowedEmbeddedAppOrigin,\n messageErrorPayload,\n withEmbeddedAppParams,\n type AgentNativeEmbedEnvelope,\n type AgentNativeEmbedErrorPayload,\n type AgentNativeEmbedMessageType,\n type EmbeddedAppUrlOptions,\n} from \"./protocol.js\";\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare const AGENT_NATIVE_EMBED_PROTOCOL: "agent-native.embed";
|
|
2
|
+
export declare const AGENT_NATIVE_EMBED_VERSION: 1;
|
|
3
|
+
export declare const AGENT_NATIVE_EMBED_MESSAGE_TYPES: {
|
|
4
|
+
readonly READY: "ready";
|
|
5
|
+
readonly MESSAGE: "message";
|
|
6
|
+
readonly REQUEST: "request";
|
|
7
|
+
readonly RESPONSE: "response";
|
|
8
|
+
readonly ERROR: "error";
|
|
9
|
+
};
|
|
10
|
+
export type AgentNativeEmbedMessageType = (typeof AGENT_NATIVE_EMBED_MESSAGE_TYPES)[keyof typeof AGENT_NATIVE_EMBED_MESSAGE_TYPES];
|
|
11
|
+
export interface AgentNativeEmbedErrorPayload {
|
|
12
|
+
message: string;
|
|
13
|
+
code?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface AgentNativeEmbedEnvelope<TPayload = unknown> {
|
|
16
|
+
protocol: typeof AGENT_NATIVE_EMBED_PROTOCOL;
|
|
17
|
+
version: typeof AGENT_NATIVE_EMBED_VERSION;
|
|
18
|
+
type: AgentNativeEmbedMessageType;
|
|
19
|
+
name?: string;
|
|
20
|
+
payload?: TPayload;
|
|
21
|
+
requestId?: string;
|
|
22
|
+
error?: AgentNativeEmbedErrorPayload;
|
|
23
|
+
}
|
|
24
|
+
export interface EmbeddedAppUrlOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Add the framework's embedded route marker. Defaults to true.
|
|
27
|
+
*/
|
|
28
|
+
embedded?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Extra query params to include in the iframe URL.
|
|
31
|
+
*/
|
|
32
|
+
params?: Record<string, string | number | boolean | null | undefined>;
|
|
33
|
+
}
|
|
34
|
+
export declare function isAgentNativeEmbedEnvelope(value: unknown): value is AgentNativeEmbedEnvelope;
|
|
35
|
+
export declare function createAgentNativeEmbedEnvelope<TPayload>(type: AgentNativeEmbedMessageType, options?: {
|
|
36
|
+
name?: string;
|
|
37
|
+
payload?: TPayload;
|
|
38
|
+
requestId?: string;
|
|
39
|
+
error?: AgentNativeEmbedErrorPayload;
|
|
40
|
+
}): AgentNativeEmbedEnvelope<TPayload>;
|
|
41
|
+
export declare function embeddedAppOrigin(url: string, base?: string): string | null;
|
|
42
|
+
export declare function isAllowedEmbeddedAppOrigin(origin: string, allowedOrigins: readonly string[] | undefined): boolean;
|
|
43
|
+
export declare function withEmbeddedAppParams(url: string, options?: EmbeddedAppUrlOptions): string;
|
|
44
|
+
export declare function createEmbeddedAppRequestId(): string;
|
|
45
|
+
export declare function messageErrorPayload(error: unknown): AgentNativeEmbedErrorPayload;
|
|
46
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/embedding/protocol.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,2BAA2B,EAAG,oBAA6B,CAAC;AACzE,eAAO,MAAM,0BAA0B,EAAG,CAAU,CAAC;AAErD,eAAO,MAAM,gCAAgC;;;;;;CAMnC,CAAC;AAEX,MAAM,MAAM,2BAA2B,GACrC,CAAC,OAAO,gCAAgC,CAAC,CAAC,MAAM,OAAO,gCAAgC,CAAC,CAAC;AAE3F,MAAM,WAAW,4BAA4B;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,wBAAwB,CAAC,QAAQ,GAAG,OAAO;IAC1D,QAAQ,EAAE,OAAO,2BAA2B,CAAC;IAC7C,OAAO,EAAE,OAAO,0BAA0B,CAAC;IAC3C,IAAI,EAAE,2BAA2B,CAAC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,4BAA4B,CAAC;CACtC;AAED,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;CACvE;AAED,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,wBAAwB,CAuCnC;AAED,wBAAgB,8BAA8B,CAAC,QAAQ,EACrD,IAAI,EAAE,2BAA2B,EACjC,OAAO,GAAE;IACP,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,4BAA4B,CAAC;CACjC,GACL,wBAAwB,CAAC,QAAQ,CAAC,CAOpC;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAW3E;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAC5C,OAAO,CAKT;AAED,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,qBAA0B,GAClC,MAAM,CAyCR;AAED,wBAAgB,0BAA0B,IAAI,MAAM,CAQnD;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,GACb,4BAA4B,CAG9B"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export const AGENT_NATIVE_EMBED_PROTOCOL = "agent-native.embed";
|
|
2
|
+
export const AGENT_NATIVE_EMBED_VERSION = 1;
|
|
3
|
+
export const AGENT_NATIVE_EMBED_MESSAGE_TYPES = {
|
|
4
|
+
READY: "ready",
|
|
5
|
+
MESSAGE: "message",
|
|
6
|
+
REQUEST: "request",
|
|
7
|
+
RESPONSE: "response",
|
|
8
|
+
ERROR: "error",
|
|
9
|
+
};
|
|
10
|
+
export function isAgentNativeEmbedEnvelope(value) {
|
|
11
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const candidate = value;
|
|
15
|
+
if (candidate.protocol !== AGENT_NATIVE_EMBED_PROTOCOL ||
|
|
16
|
+
candidate.version !== AGENT_NATIVE_EMBED_VERSION ||
|
|
17
|
+
typeof candidate.type !== "string" ||
|
|
18
|
+
!Object.values(AGENT_NATIVE_EMBED_MESSAGE_TYPES).includes(candidate.type)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (candidate.name !== undefined && typeof candidate.name !== "string") {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if (candidate.requestId !== undefined &&
|
|
25
|
+
typeof candidate.requestId !== "string") {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (candidate.error !== undefined) {
|
|
29
|
+
if (!candidate.error ||
|
|
30
|
+
typeof candidate.error !== "object" ||
|
|
31
|
+
Array.isArray(candidate.error)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const error = candidate.error;
|
|
35
|
+
if (typeof error.message !== "string")
|
|
36
|
+
return false;
|
|
37
|
+
if (error.code !== undefined && typeof error.code !== "string") {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
export function createAgentNativeEmbedEnvelope(type, options = {}) {
|
|
44
|
+
return {
|
|
45
|
+
protocol: AGENT_NATIVE_EMBED_PROTOCOL,
|
|
46
|
+
version: AGENT_NATIVE_EMBED_VERSION,
|
|
47
|
+
type,
|
|
48
|
+
...options,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function embeddedAppOrigin(url, base) {
|
|
52
|
+
try {
|
|
53
|
+
const resolvedBase = base ??
|
|
54
|
+
(typeof window !== "undefined"
|
|
55
|
+
? window.location.href
|
|
56
|
+
: "http://agent-native.local");
|
|
57
|
+
return new URL(url, resolvedBase).origin;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function isAllowedEmbeddedAppOrigin(origin, allowedOrigins) {
|
|
64
|
+
if (!allowedOrigins?.length)
|
|
65
|
+
return false;
|
|
66
|
+
return allowedOrigins.some((allowed) => allowed === "*" || allowed === origin);
|
|
67
|
+
}
|
|
68
|
+
export function withEmbeddedAppParams(url, options = {}) {
|
|
69
|
+
const shouldMarkEmbedded = options.embedded ?? true;
|
|
70
|
+
const hasScheme = /^[a-z][a-z0-9+.-]*:/i.test(url);
|
|
71
|
+
const isProtocolRelative = url.startsWith("//");
|
|
72
|
+
const isAbsolutePath = url.startsWith("/");
|
|
73
|
+
const isRelative = !hasScheme && !isProtocolRelative;
|
|
74
|
+
try {
|
|
75
|
+
const base = typeof window !== "undefined"
|
|
76
|
+
? window.location.href
|
|
77
|
+
: "http://agent-native.local";
|
|
78
|
+
const parsed = new URL(url, base);
|
|
79
|
+
if (shouldMarkEmbedded && !parsed.searchParams.has("embedded")) {
|
|
80
|
+
parsed.searchParams.set("embedded", "1");
|
|
81
|
+
}
|
|
82
|
+
for (const [key, value] of Object.entries(options.params ?? {})) {
|
|
83
|
+
if (value === undefined || value === null)
|
|
84
|
+
continue;
|
|
85
|
+
parsed.searchParams.set(key, String(value));
|
|
86
|
+
}
|
|
87
|
+
if (isRelative) {
|
|
88
|
+
const path = `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
89
|
+
return isAbsolutePath ? path : path.replace(/^\//, "");
|
|
90
|
+
}
|
|
91
|
+
return parsed.toString();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
const [beforeHash, hash = ""] = url.split("#", 2);
|
|
95
|
+
const separator = beforeHash.includes("?") ? "&" : "?";
|
|
96
|
+
const params = new URLSearchParams();
|
|
97
|
+
if (shouldMarkEmbedded)
|
|
98
|
+
params.set("embedded", "1");
|
|
99
|
+
for (const [key, value] of Object.entries(options.params ?? {})) {
|
|
100
|
+
if (value === undefined || value === null)
|
|
101
|
+
continue;
|
|
102
|
+
params.set(key, String(value));
|
|
103
|
+
}
|
|
104
|
+
const query = params.toString();
|
|
105
|
+
if (!query)
|
|
106
|
+
return url;
|
|
107
|
+
return `${beforeHash}${separator}${query}${hash ? `#${hash}` : ""}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function createEmbeddedAppRequestId() {
|
|
111
|
+
if (typeof crypto !== "undefined" &&
|
|
112
|
+
typeof crypto.randomUUID === "function") {
|
|
113
|
+
return `embed-${crypto.randomUUID()}`;
|
|
114
|
+
}
|
|
115
|
+
return `embed-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
116
|
+
}
|
|
117
|
+
export function messageErrorPayload(error) {
|
|
118
|
+
if (error instanceof Error)
|
|
119
|
+
return { message: error.message };
|
|
120
|
+
return { message: String(error) };
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/embedding/protocol.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,2BAA2B,GAAG,oBAA6B,CAAC;AACzE,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAU,CAAC;AAErD,MAAM,CAAC,MAAM,gCAAgC,GAAG;IAC9C,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,QAAQ,EAAE,UAAU;IACpB,KAAK,EAAE,OAAO;CACN,CAAC;AA+BX,MAAM,UAAU,0BAA0B,CACxC,KAAc;IAEd,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAChE,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,SAAS,GAAG,KAA0C,CAAC;IAC7D,IACE,SAAS,CAAC,QAAQ,KAAK,2BAA2B;QAClD,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,CAAC;YAChE,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI;gBAAE,SAAS;YACpD,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACjC,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;QAChC,IAAI,CAAC,KAAK;YAAE,OAAO,GAAG,CAAC;QACvB,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IACtE,CAAC;AACH,CAAC;AAED,MAAM,UAAU,0BAA0B;IACxC,IACE,OAAO,MAAM,KAAK,WAAW;QAC7B,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,EACvC,CAAC;QACD,OAAO,SAAS,MAAM,CAAC,UAAU,EAAE,EAAE,CAAC;IACxC,CAAC;IACD,OAAO,SAAS,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;AAC1E,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,KAAc;IAEd,IAAI,KAAK,YAAY,KAAK;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACpC,CAAC","sourcesContent":["export const AGENT_NATIVE_EMBED_PROTOCOL = \"agent-native.embed\" as const;\nexport const AGENT_NATIVE_EMBED_VERSION = 1 as const;\n\nexport const AGENT_NATIVE_EMBED_MESSAGE_TYPES = {\n READY: \"ready\",\n MESSAGE: \"message\",\n REQUEST: \"request\",\n RESPONSE: \"response\",\n ERROR: \"error\",\n} as const;\n\nexport type AgentNativeEmbedMessageType =\n (typeof AGENT_NATIVE_EMBED_MESSAGE_TYPES)[keyof typeof AGENT_NATIVE_EMBED_MESSAGE_TYPES];\n\nexport interface AgentNativeEmbedErrorPayload {\n message: string;\n code?: string;\n}\n\nexport interface AgentNativeEmbedEnvelope<TPayload = unknown> {\n protocol: typeof AGENT_NATIVE_EMBED_PROTOCOL;\n version: typeof AGENT_NATIVE_EMBED_VERSION;\n type: AgentNativeEmbedMessageType;\n name?: string;\n payload?: TPayload;\n requestId?: string;\n error?: AgentNativeEmbedErrorPayload;\n}\n\nexport interface EmbeddedAppUrlOptions {\n /**\n * Add the framework's embedded route marker. Defaults to true.\n */\n embedded?: boolean;\n /**\n * Extra query params to include in the iframe URL.\n */\n params?: Record<string, string | number | boolean | null | undefined>;\n}\n\nexport function isAgentNativeEmbedEnvelope(\n value: unknown,\n): value is AgentNativeEmbedEnvelope {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n return false;\n }\n const candidate = value as Partial<AgentNativeEmbedEnvelope>;\n if (\n candidate.protocol !== AGENT_NATIVE_EMBED_PROTOCOL ||\n candidate.version !== AGENT_NATIVE_EMBED_VERSION ||\n typeof candidate.type !== \"string\" ||\n !Object.values(AGENT_NATIVE_EMBED_MESSAGE_TYPES).includes(\n candidate.type as AgentNativeEmbedMessageType,\n )\n ) {\n return false;\n }\n if (candidate.name !== undefined && typeof candidate.name !== \"string\") {\n return false;\n }\n if (\n candidate.requestId !== undefined &&\n typeof candidate.requestId !== \"string\"\n ) {\n return false;\n }\n if (candidate.error !== undefined) {\n if (\n !candidate.error ||\n typeof candidate.error !== \"object\" ||\n Array.isArray(candidate.error)\n ) {\n return false;\n }\n const error = candidate.error as Partial<AgentNativeEmbedErrorPayload>;\n if (typeof error.message !== \"string\") return false;\n if (error.code !== undefined && typeof error.code !== \"string\") {\n return false;\n }\n }\n return true;\n}\n\nexport function createAgentNativeEmbedEnvelope<TPayload>(\n type: AgentNativeEmbedMessageType,\n options: {\n name?: string;\n payload?: TPayload;\n requestId?: string;\n error?: AgentNativeEmbedErrorPayload;\n } = {},\n): AgentNativeEmbedEnvelope<TPayload> {\n return {\n protocol: AGENT_NATIVE_EMBED_PROTOCOL,\n version: AGENT_NATIVE_EMBED_VERSION,\n type,\n ...options,\n };\n}\n\nexport function embeddedAppOrigin(url: string, base?: string): string | null {\n try {\n const resolvedBase =\n base ??\n (typeof window !== \"undefined\"\n ? window.location.href\n : \"http://agent-native.local\");\n return new URL(url, resolvedBase).origin;\n } catch {\n return null;\n }\n}\n\nexport function isAllowedEmbeddedAppOrigin(\n origin: string,\n allowedOrigins: readonly string[] | undefined,\n): boolean {\n if (!allowedOrigins?.length) return false;\n return allowedOrigins.some(\n (allowed) => allowed === \"*\" || allowed === origin,\n );\n}\n\nexport function withEmbeddedAppParams(\n url: string,\n options: EmbeddedAppUrlOptions = {},\n): string {\n const shouldMarkEmbedded = options.embedded ?? true;\n const hasScheme = /^[a-z][a-z0-9+.-]*:/i.test(url);\n const isProtocolRelative = url.startsWith(\"//\");\n const isAbsolutePath = url.startsWith(\"/\");\n const isRelative = !hasScheme && !isProtocolRelative;\n\n try {\n const base =\n typeof window !== \"undefined\"\n ? window.location.href\n : \"http://agent-native.local\";\n const parsed = new URL(url, base);\n\n if (shouldMarkEmbedded && !parsed.searchParams.has(\"embedded\")) {\n parsed.searchParams.set(\"embedded\", \"1\");\n }\n\n for (const [key, value] of Object.entries(options.params ?? {})) {\n if (value === undefined || value === null) continue;\n parsed.searchParams.set(key, String(value));\n }\n\n if (isRelative) {\n const path = `${parsed.pathname}${parsed.search}${parsed.hash}`;\n return isAbsolutePath ? path : path.replace(/^\\//, \"\");\n }\n return parsed.toString();\n } catch {\n const [beforeHash, hash = \"\"] = url.split(\"#\", 2);\n const separator = beforeHash.includes(\"?\") ? \"&\" : \"?\";\n const params = new URLSearchParams();\n if (shouldMarkEmbedded) params.set(\"embedded\", \"1\");\n for (const [key, value] of Object.entries(options.params ?? {})) {\n if (value === undefined || value === null) continue;\n params.set(key, String(value));\n }\n const query = params.toString();\n if (!query) return url;\n return `${beforeHash}${separator}${query}${hash ? `#${hash}` : \"\"}`;\n }\n}\n\nexport function createEmbeddedAppRequestId(): string {\n if (\n typeof crypto !== \"undefined\" &&\n typeof crypto.randomUUID === \"function\"\n ) {\n return `embed-${crypto.randomUUID()}`;\n }\n return `embed-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;\n}\n\nexport function messageErrorPayload(\n error: unknown,\n): AgentNativeEmbedErrorPayload {\n if (error instanceof Error) return { message: error.message };\n return { message: String(error) };\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type IframeHTMLAttributes } from "react";
|
|
2
|
+
import { type EmbeddedAppUrlOptions } from "./protocol.js";
|
|
3
|
+
export interface EmbeddedAppRef {
|
|
4
|
+
iframe: HTMLIFrameElement | null;
|
|
5
|
+
origin: string | null;
|
|
6
|
+
postMessage<TPayload = unknown>(name: string, payload?: TPayload): boolean;
|
|
7
|
+
request<TResult = unknown, TPayload = unknown>(name: string, payload?: TPayload, options?: {
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
}): Promise<TResult>;
|
|
10
|
+
focus(): void;
|
|
11
|
+
}
|
|
12
|
+
export interface EmbeddedAppMessageInfo<TPayload = unknown> {
|
|
13
|
+
name: string;
|
|
14
|
+
payload: TPayload;
|
|
15
|
+
event: MessageEvent;
|
|
16
|
+
ref: EmbeddedAppRef;
|
|
17
|
+
}
|
|
18
|
+
export interface EmbeddedAppProps extends Omit<IframeHTMLAttributes<HTMLIFrameElement>, "src" | "onLoad"> {
|
|
19
|
+
url: string;
|
|
20
|
+
/**
|
|
21
|
+
* Origin used when posting messages to the iframe. Defaults to url's origin.
|
|
22
|
+
*/
|
|
23
|
+
targetOrigin?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Origins allowed to send messages back. Defaults to targetOrigin.
|
|
26
|
+
*/
|
|
27
|
+
allowedOrigins?: string[];
|
|
28
|
+
/**
|
|
29
|
+
* Adds `embedded=1` by default. Pass false to use the URL untouched.
|
|
30
|
+
*/
|
|
31
|
+
embed?: boolean | EmbeddedAppUrlOptions;
|
|
32
|
+
onLoad?: (ref: EmbeddedAppRef) => void;
|
|
33
|
+
onReady?: (payload: unknown, event: MessageEvent, ref: EmbeddedAppRef) => void;
|
|
34
|
+
onMessage?: <TPayload = unknown>(name: string, payload: TPayload, event: MessageEvent, ref: EmbeddedAppRef) => void;
|
|
35
|
+
onRequest?: (name: string, payload: unknown, event: MessageEvent, ref: EmbeddedAppRef) => unknown | Promise<unknown>;
|
|
36
|
+
}
|
|
37
|
+
export declare function isEmbeddedAppMessageSource(event: Pick<MessageEvent, "source">, iframe: Pick<HTMLIFrameElement, "contentWindow"> | null): boolean;
|
|
38
|
+
export declare const EmbeddedApp: import("react").ForwardRefExoticComponent<EmbeddedAppProps & import("react").RefAttributes<EmbeddedAppRef>>;
|
|
39
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/embedding/react.tsx"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,oBAAoB,EAC1B,MAAM,OAAO,CAAC;AACf,OAAO,EAUL,KAAK,qBAAqB,EAC3B,MAAM,eAAe,CAAC;AAEvB,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACjC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,WAAW,CAAC,QAAQ,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;IAC3E,OAAO,CAAC,OAAO,GAAG,OAAO,EAAE,QAAQ,GAAG,OAAO,EAC3C,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,QAAQ,EAClB,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAC/B,OAAO,CAAC,OAAO,C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"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, } from "react";
|
|
3
|
+
import { AGENT_NATIVE_EMBED_MESSAGE_TYPES, createAgentNativeEmbedEnvelope, createEmbeddedAppRequestId, embeddedAppOrigin, isAgentNativeEmbedEnvelope, isAllowedEmbeddedAppOrigin, messageErrorPayload, withEmbeddedAppParams, } from "./protocol.js";
|
|
4
|
+
const defaultStyle = {
|
|
5
|
+
border: 0,
|
|
6
|
+
width: "100%",
|
|
7
|
+
height: "100%",
|
|
8
|
+
};
|
|
9
|
+
function postEnvelope(iframe, targetOrigin, envelope) {
|
|
10
|
+
const targetWindow = iframe?.contentWindow;
|
|
11
|
+
if (!targetOrigin)
|
|
12
|
+
return false;
|
|
13
|
+
if (!targetWindow)
|
|
14
|
+
return false;
|
|
15
|
+
targetWindow.postMessage(envelope, targetOrigin);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
export function isEmbeddedAppMessageSource(event, iframe) {
|
|
19
|
+
return Boolean(iframe?.contentWindow && event.source === iframe.contentWindow);
|
|
20
|
+
}
|
|
21
|
+
export const EmbeddedApp = forwardRef(function EmbeddedApp({ url, targetOrigin, allowedOrigins, embed = true, onLoad, onReady, onMessage, onRequest, title = "Agent-Native embedded app", sandbox = "allow-scripts allow-same-origin allow-forms allow-popups allow-downloads", allow = "clipboard-read; clipboard-write; microphone; fullscreen", referrerPolicy = "strict-origin-when-cross-origin", style, ...iframeProps }, forwardedRef) {
|
|
22
|
+
const iframeRef = useRef(null);
|
|
23
|
+
const pendingRef = useRef(new Map());
|
|
24
|
+
const resolvedOrigin = useMemo(() => targetOrigin ?? embeddedAppOrigin(url), [targetOrigin, url]);
|
|
25
|
+
const trustedOrigins = useMemo(() => allowedOrigins ??
|
|
26
|
+
(resolvedOrigin && resolvedOrigin !== "*" ? [resolvedOrigin] : []), [allowedOrigins, resolvedOrigin]);
|
|
27
|
+
const src = useMemo(() => {
|
|
28
|
+
if (embed === false)
|
|
29
|
+
return url;
|
|
30
|
+
const options = typeof embed === "object" ? embed : {};
|
|
31
|
+
return withEmbeddedAppParams(url, options);
|
|
32
|
+
}, [embed, url]);
|
|
33
|
+
const refValue = useMemo(() => ({
|
|
34
|
+
get iframe() {
|
|
35
|
+
return iframeRef.current;
|
|
36
|
+
},
|
|
37
|
+
origin: resolvedOrigin,
|
|
38
|
+
postMessage(name, payload) {
|
|
39
|
+
return postEnvelope(iframeRef.current, resolvedOrigin ?? undefined, createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE, { name, payload }));
|
|
40
|
+
},
|
|
41
|
+
request(name, payload, options) {
|
|
42
|
+
const requestId = createEmbeddedAppRequestId();
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const timeoutMs = options?.timeoutMs ?? 30_000;
|
|
45
|
+
const timer = timeoutMs > 0
|
|
46
|
+
? setTimeout(() => {
|
|
47
|
+
pendingRef.current.delete(requestId);
|
|
48
|
+
reject(new Error(`Embedded app request timed out: ${name}`));
|
|
49
|
+
}, timeoutMs)
|
|
50
|
+
: undefined;
|
|
51
|
+
pendingRef.current.set(requestId, {
|
|
52
|
+
resolve: (value) => resolve(value),
|
|
53
|
+
reject,
|
|
54
|
+
timer,
|
|
55
|
+
});
|
|
56
|
+
const posted = postEnvelope(iframeRef.current, resolvedOrigin ?? undefined, createAgentNativeEmbedEnvelope(AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST, { name, payload, requestId }));
|
|
57
|
+
if (!posted) {
|
|
58
|
+
pendingRef.current.delete(requestId);
|
|
59
|
+
if (timer)
|
|
60
|
+
clearTimeout(timer);
|
|
61
|
+
reject(new Error("Embedded app iframe is not ready"));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
focus() {
|
|
66
|
+
iframeRef.current?.focus();
|
|
67
|
+
},
|
|
68
|
+
}), [resolvedOrigin]);
|
|
69
|
+
useImperativeHandle(forwardedRef, () => refValue, [refValue]);
|
|
70
|
+
const respond = useCallback((requestId, type, value) => {
|
|
71
|
+
if (!requestId)
|
|
72
|
+
return;
|
|
73
|
+
postEnvelope(iframeRef.current, resolvedOrigin ?? undefined, createAgentNativeEmbedEnvelope(type === "response"
|
|
74
|
+
? AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE
|
|
75
|
+
: AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR, type === "response"
|
|
76
|
+
? { requestId, payload: value }
|
|
77
|
+
: { requestId, error: messageErrorPayload(value) }));
|
|
78
|
+
}, [resolvedOrigin]);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const listener = (event) => {
|
|
81
|
+
if (!isEmbeddedAppMessageSource(event, iframeRef.current))
|
|
82
|
+
return;
|
|
83
|
+
if (!isAllowedEmbeddedAppOrigin(event.origin, trustedOrigins))
|
|
84
|
+
return;
|
|
85
|
+
if (!isAgentNativeEmbedEnvelope(event.data))
|
|
86
|
+
return;
|
|
87
|
+
const envelope = event.data;
|
|
88
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY) {
|
|
89
|
+
onReady?.(envelope.payload, event, refValue);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE) {
|
|
93
|
+
const requestId = envelope.requestId;
|
|
94
|
+
if (!requestId)
|
|
95
|
+
return;
|
|
96
|
+
const pending = pendingRef.current.get(requestId);
|
|
97
|
+
if (!pending)
|
|
98
|
+
return;
|
|
99
|
+
pendingRef.current.delete(requestId);
|
|
100
|
+
if (pending.timer)
|
|
101
|
+
clearTimeout(pending.timer);
|
|
102
|
+
pending.resolve(envelope.payload);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR) {
|
|
106
|
+
const requestId = envelope.requestId;
|
|
107
|
+
if (!requestId)
|
|
108
|
+
return;
|
|
109
|
+
const pending = pendingRef.current.get(requestId);
|
|
110
|
+
if (!pending)
|
|
111
|
+
return;
|
|
112
|
+
pendingRef.current.delete(requestId);
|
|
113
|
+
if (pending.timer)
|
|
114
|
+
clearTimeout(pending.timer);
|
|
115
|
+
pending.reject(new Error(envelope.error?.message ?? "Embedded app error"));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!envelope.name)
|
|
119
|
+
return;
|
|
120
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST) {
|
|
121
|
+
Promise.resolve(onRequest?.(envelope.name, envelope.payload, event, refValue))
|
|
122
|
+
.then((payload) => respond(envelope.requestId, "response", payload))
|
|
123
|
+
.catch((error) => respond(envelope.requestId, "error", error));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE) {
|
|
127
|
+
onMessage?.(envelope.name, envelope.payload, event, refValue);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
window.addEventListener("message", listener);
|
|
131
|
+
return () => {
|
|
132
|
+
window.removeEventListener("message", listener);
|
|
133
|
+
};
|
|
134
|
+
}, [onMessage, onReady, onRequest, refValue, respond, trustedOrigins]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
return () => {
|
|
137
|
+
for (const pending of pendingRef.current.values()) {
|
|
138
|
+
if (pending.timer)
|
|
139
|
+
clearTimeout(pending.timer);
|
|
140
|
+
pending.reject(new Error("Embedded app unmounted"));
|
|
141
|
+
}
|
|
142
|
+
pendingRef.current.clear();
|
|
143
|
+
};
|
|
144
|
+
}, []);
|
|
145
|
+
return (_jsx("iframe", { ...iframeProps, ref: iframeRef, src: src, title: title, sandbox: sandbox, allow: allow, referrerPolicy: referrerPolicy, style: { ...defaultStyle, ...style }, onLoad: () => onLoad?.(refValue) }));
|
|
146
|
+
});
|
|
147
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../../src/embedding/react.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,SAAS,EACT,mBAAmB,EACnB,OAAO,EACP,MAAM,GAGP,MAAM,OAAO,CAAC;AACf,OAAO,EACL,gCAAgC,EAChC,8BAA8B,EAC9B,0BAA0B,EAC1B,iBAAiB,EACjB,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,GAGtB,MAAM,eAAe,CAAC;AAgEvB,MAAM,YAAY,GAAkB;IAClC,MAAM,EAAE,CAAC;IACT,KAAK,EAAE,MAAM;IACb,MAAM,EAAE,MAAM;CACf,CAAC;AAEF,SAAS,YAAY,CACnB,MAAgC,EAChC,YAAuC,EACvC,QAAkC;IAElC,MAAM,YAAY,GAAG,MAAM,EAAE,aAAa,CAAC;IAC3C,IAAI,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC;IAChC,IAAI,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC;IAChC,YAAY,CAAC,WAAW,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IACjD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,KAAmC,EACnC,MAAuD;IAEvD,OAAO,OAAO,CACZ,MAAM,EAAE,aAAa,IAAI,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,aAAa,CAC/D,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CACnC,SAAS,WAAW,CAClB,EACE,GAAG,EACH,YAAY,EACZ,cAAc,EACd,KAAK,GAAG,IAAI,EACZ,MAAM,EACN,OAAO,EACP,SAAS,EACT,SAAS,EACT,KAAK,GAAG,2BAA2B,EACnC,OAAO,GAAG,0EAA0E,EACpF,KAAK,GAAG,y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,EACf,GAAG,EAAE,SAAS,EACd,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,KAAK,EACZ,cAAc,EAAE,cAAc,EAC9B,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE,EACpC,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,GAChC,CACH,CAAC;AACJ,CAAC,CACF,CAAC","sourcesContent":["import {\n forwardRef,\n useCallback,\n useEffect,\n useImperativeHandle,\n useMemo,\n useRef,\n type CSSProperties,\n type IframeHTMLAttributes,\n} from \"react\";\nimport {\n AGENT_NATIVE_EMBED_MESSAGE_TYPES,\n createAgentNativeEmbedEnvelope,\n createEmbeddedAppRequestId,\n embeddedAppOrigin,\n isAgentNativeEmbedEnvelope,\n isAllowedEmbeddedAppOrigin,\n messageErrorPayload,\n withEmbeddedAppParams,\n type AgentNativeEmbedEnvelope,\n type EmbeddedAppUrlOptions,\n} from \"./protocol.js\";\n\nexport interface EmbeddedAppRef {\n iframe: HTMLIFrameElement | null;\n origin: string | null;\n postMessage<TPayload = unknown>(name: string, payload?: TPayload): boolean;\n request<TResult = unknown, TPayload = unknown>(\n name: string,\n payload?: TPayload,\n options?: { timeoutMs?: number },\n ): Promise<TResult>;\n focus(): void;\n}\n\nexport interface EmbeddedAppMessageInfo<TPayload = unknown> {\n name: string;\n payload: TPayload;\n event: MessageEvent;\n ref: EmbeddedAppRef;\n}\n\nexport interface EmbeddedAppProps extends Omit<\n IframeHTMLAttributes<HTMLIFrameElement>,\n \"src\" | \"onLoad\"\n> {\n url: string;\n /**\n * Origin used when posting messages to the iframe. Defaults to url's origin.\n */\n targetOrigin?: string;\n /**\n * Origins allowed to send messages back. Defaults to targetOrigin.\n */\n allowedOrigins?: string[];\n /**\n * Adds `embedded=1` by default. Pass false to use the URL untouched.\n */\n embed?: boolean | EmbeddedAppUrlOptions;\n onLoad?: (ref: EmbeddedAppRef) => void;\n onReady?: (\n payload: unknown,\n event: MessageEvent,\n ref: EmbeddedAppRef,\n ) => void;\n onMessage?: <TPayload = unknown>(\n name: string,\n payload: TPayload,\n event: MessageEvent,\n ref: EmbeddedAppRef,\n ) => void;\n onRequest?: (\n name: string,\n payload: unknown,\n event: MessageEvent,\n ref: EmbeddedAppRef,\n ) => unknown | Promise<unknown>;\n}\n\ntype PendingRequest = {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n timer: ReturnType<typeof setTimeout> | undefined;\n};\n\nconst defaultStyle: CSSProperties = {\n border: 0,\n width: \"100%\",\n height: \"100%\",\n};\n\nfunction postEnvelope(\n iframe: HTMLIFrameElement | null,\n targetOrigin: string | null | undefined,\n envelope: AgentNativeEmbedEnvelope,\n): boolean {\n const targetWindow = iframe?.contentWindow;\n if (!targetOrigin) return false;\n if (!targetWindow) return false;\n targetWindow.postMessage(envelope, targetOrigin);\n return true;\n}\n\nexport function isEmbeddedAppMessageSource(\n event: Pick<MessageEvent, \"source\">,\n iframe: Pick<HTMLIFrameElement, \"contentWindow\"> | null,\n) {\n return Boolean(\n iframe?.contentWindow && event.source === iframe.contentWindow,\n );\n}\n\nexport const EmbeddedApp = forwardRef<EmbeddedAppRef, EmbeddedAppProps>(\n function EmbeddedApp(\n {\n url,\n targetOrigin,\n allowedOrigins,\n embed = true,\n onLoad,\n onReady,\n onMessage,\n onRequest,\n title = \"Agent-Native embedded app\",\n sandbox = \"allow-scripts allow-same-origin allow-forms allow-popups allow-downloads\",\n allow = \"clipboard-read; clipboard-write; microphone; fullscreen\",\n referrerPolicy = \"strict-origin-when-cross-origin\",\n style,\n ...iframeProps\n },\n forwardedRef,\n ) {\n const iframeRef = useRef<HTMLIFrameElement | null>(null);\n const pendingRef = useRef(new Map<string, PendingRequest>());\n const resolvedOrigin = useMemo(\n () => targetOrigin ?? embeddedAppOrigin(url),\n [targetOrigin, url],\n );\n const trustedOrigins = useMemo(\n () =>\n allowedOrigins ??\n (resolvedOrigin && resolvedOrigin !== \"*\" ? [resolvedOrigin] : []),\n [allowedOrigins, resolvedOrigin],\n );\n const src = useMemo(() => {\n if (embed === false) return url;\n const options = typeof embed === \"object\" ? embed : {};\n return withEmbeddedAppParams(url, options);\n }, [embed, url]);\n\n const refValue = useMemo<EmbeddedAppRef>(\n () => ({\n get iframe() {\n return iframeRef.current;\n },\n origin: resolvedOrigin,\n postMessage(name, payload) {\n return postEnvelope(\n iframeRef.current,\n resolvedOrigin ?? undefined,\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE,\n { name, payload },\n ),\n );\n },\n request<TResult = unknown, TPayload = unknown>(\n name: string,\n payload?: TPayload,\n options?: { timeoutMs?: number },\n ) {\n const requestId = createEmbeddedAppRequestId();\n return new Promise<TResult>((resolve, reject) => {\n const timeoutMs = options?.timeoutMs ?? 30_000;\n const timer =\n timeoutMs > 0\n ? setTimeout(() => {\n pendingRef.current.delete(requestId);\n reject(\n new Error(`Embedded app request timed out: ${name}`),\n );\n }, timeoutMs)\n : undefined;\n pendingRef.current.set(requestId, {\n resolve: (value) => resolve(value as TResult),\n reject,\n timer,\n });\n const posted = postEnvelope(\n iframeRef.current,\n resolvedOrigin ?? undefined,\n createAgentNativeEmbedEnvelope(\n AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST,\n { name, payload, requestId },\n ),\n );\n if (!posted) {\n pendingRef.current.delete(requestId);\n if (timer) clearTimeout(timer);\n reject(new Error(\"Embedded app iframe is not ready\"));\n }\n });\n },\n focus() {\n iframeRef.current?.focus();\n },\n }),\n [resolvedOrigin],\n );\n\n useImperativeHandle(forwardedRef, () => refValue, [refValue]);\n\n const respond = useCallback(\n (\n requestId: string | undefined,\n type: \"response\" | \"error\",\n value: unknown,\n ) => {\n if (!requestId) return;\n postEnvelope(\n iframeRef.current,\n resolvedOrigin ?? undefined,\n createAgentNativeEmbedEnvelope(\n type === \"response\"\n ? AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE\n : AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR,\n type === \"response\"\n ? { requestId, payload: value }\n : { requestId, error: messageErrorPayload(value) },\n ),\n );\n },\n [resolvedOrigin],\n );\n\n useEffect(() => {\n const listener = (event: MessageEvent) => {\n if (!isEmbeddedAppMessageSource(event, iframeRef.current)) return;\n if (!isAllowedEmbeddedAppOrigin(event.origin, trustedOrigins)) return;\n if (!isAgentNativeEmbedEnvelope(event.data)) return;\n\n const envelope = event.data;\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.READY) {\n onReady?.(envelope.payload, event, refValue);\n return;\n }\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.RESPONSE) {\n const requestId = envelope.requestId;\n if (!requestId) return;\n const pending = pendingRef.current.get(requestId);\n if (!pending) return;\n pendingRef.current.delete(requestId);\n if (pending.timer) clearTimeout(pending.timer);\n pending.resolve(envelope.payload);\n return;\n }\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.ERROR) {\n const requestId = envelope.requestId;\n if (!requestId) return;\n const pending = pendingRef.current.get(requestId);\n if (!pending) return;\n pendingRef.current.delete(requestId);\n if (pending.timer) clearTimeout(pending.timer);\n pending.reject(\n new Error(envelope.error?.message ?? \"Embedded app error\"),\n );\n return;\n }\n\n if (!envelope.name) return;\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.REQUEST) {\n Promise.resolve(\n onRequest?.(envelope.name, envelope.payload, event, refValue),\n )\n .then((payload) => respond(envelope.requestId, \"response\", payload))\n .catch((error) => respond(envelope.requestId, \"error\", error));\n return;\n }\n\n if (envelope.type === AGENT_NATIVE_EMBED_MESSAGE_TYPES.MESSAGE) {\n onMessage?.(envelope.name, envelope.payload, event, refValue);\n }\n };\n\n window.addEventListener(\"message\", listener);\n return () => {\n window.removeEventListener(\"message\", listener);\n };\n }, [onMessage, onReady, onRequest, refValue, respond, trustedOrigins]);\n\n useEffect(() => {\n return () => {\n for (const pending of pendingRef.current.values()) {\n if (pending.timer) clearTimeout(pending.timer);\n pending.reject(new Error(\"Embedded app unmounted\"));\n }\n pendingRef.current.clear();\n };\n }, []);\n\n return (\n <iframe\n {...iframeProps}\n ref={iframeRef}\n src={src}\n title={title}\n sandbox={sandbox}\n allow={allow}\n referrerPolicy={referrerPolicy}\n style={{ ...defaultStyle, ...style }}\n onLoad={() => onLoad?.(refValue)}\n />\n );\n },\n);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-native/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.65.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -116,6 +116,11 @@
|
|
|
116
116
|
"./router": "./dist/router/index.js",
|
|
117
117
|
"./collab": "./dist/collab/index.js",
|
|
118
118
|
"./a2a": "./dist/a2a/index.js",
|
|
119
|
+
"./embedding": "./dist/embedding/index.js",
|
|
120
|
+
"./embedding/react": "./dist/embedding/react.js",
|
|
121
|
+
"./embedding/bridge": "./dist/embedding/bridge.js",
|
|
122
|
+
"./embedding/agent": "./dist/embedding/agent.js",
|
|
123
|
+
"./embedding/protocol": "./dist/embedding/protocol.js",
|
|
119
124
|
"./mcp": "./dist/mcp/index.js",
|
|
120
125
|
"./mcp-client": "./dist/mcp-client/index.js",
|
|
121
126
|
"./tracking": "./dist/tracking/index.js",
|
|
@@ -196,23 +201,23 @@
|
|
|
196
201
|
"@sentry/node": "^10.50.0",
|
|
197
202
|
"@standard-schema/spec": "^1.1.0",
|
|
198
203
|
"@tanstack/react-table": "^8.21.3",
|
|
199
|
-
"@tiptap/core": "
|
|
200
|
-
"@tiptap/extension-code-block-lowlight": "
|
|
201
|
-
"@tiptap/extension-collaboration": "
|
|
202
|
-
"@tiptap/extension-collaboration-caret": "
|
|
203
|
-
"@tiptap/extension-image": "
|
|
204
|
-
"@tiptap/extension-link": "
|
|
205
|
-
"@tiptap/extension-placeholder": "
|
|
206
|
-
"@tiptap/extension-table": "
|
|
207
|
-
"@tiptap/extension-table-cell": "
|
|
208
|
-
"@tiptap/extension-table-header": "
|
|
209
|
-
"@tiptap/extension-table-row": "
|
|
210
|
-
"@tiptap/extension-task-item": "
|
|
211
|
-
"@tiptap/extension-task-list": "
|
|
212
|
-
"@tiptap/pm": "
|
|
213
|
-
"@tiptap/react": "
|
|
214
|
-
"@tiptap/starter-kit": "
|
|
215
|
-
"@tiptap/y-tiptap": "^3.0.
|
|
204
|
+
"@tiptap/core": "3.27.1",
|
|
205
|
+
"@tiptap/extension-code-block-lowlight": "3.27.1",
|
|
206
|
+
"@tiptap/extension-collaboration": "3.27.1",
|
|
207
|
+
"@tiptap/extension-collaboration-caret": "3.27.1",
|
|
208
|
+
"@tiptap/extension-image": "3.27.1",
|
|
209
|
+
"@tiptap/extension-link": "3.27.1",
|
|
210
|
+
"@tiptap/extension-placeholder": "3.27.1",
|
|
211
|
+
"@tiptap/extension-table": "3.27.1",
|
|
212
|
+
"@tiptap/extension-table-cell": "3.27.1",
|
|
213
|
+
"@tiptap/extension-table-header": "3.27.1",
|
|
214
|
+
"@tiptap/extension-table-row": "3.27.1",
|
|
215
|
+
"@tiptap/extension-task-item": "3.27.1",
|
|
216
|
+
"@tiptap/extension-task-list": "3.27.1",
|
|
217
|
+
"@tiptap/pm": "3.27.1",
|
|
218
|
+
"@tiptap/react": "3.27.1",
|
|
219
|
+
"@tiptap/starter-kit": "3.27.1",
|
|
220
|
+
"@tiptap/y-tiptap": "^3.0.5",
|
|
216
221
|
"@uiw/react-codemirror": "^4.25.10",
|
|
217
222
|
"ajv": "^8.20.0",
|
|
218
223
|
"better-auth": "1.6.16",
|