ai-publish-sdk 1.1.0 → 1.2.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/README.md +40 -6
- package/index.d.ts +2 -0
- package/index.js +1 -1
- package/index.mjs +58 -27
- package/lib/functions.d.ts +5 -1
- package/lib/tcp-socket.d.ts +17 -0
- package/lib/types.d.ts +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,19 @@ SDK for embedded apps to communicate with the host environment.
|
|
|
8
8
|
npm install ai-publish-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## General
|
|
12
|
+
|
|
13
|
+
### RpcConfig
|
|
14
|
+
|
|
15
|
+
Optional config passed to any function call.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
interface RpcConfig {
|
|
19
|
+
timeout?: number // ms, default 15000
|
|
20
|
+
targetOrigin?: string // postMessage target origin, default '*'
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
11
24
|
## Core API
|
|
12
25
|
|
|
13
26
|
### getUserInfo()
|
|
@@ -37,7 +50,7 @@ Get OAuth token for an integration.
|
|
|
37
50
|
```typescript
|
|
38
51
|
getToken(appName: IntegrationAppName, options?: GetTokenOptions, config?: RpcConfig): Promise<GetTokenResult | null>
|
|
39
52
|
|
|
40
|
-
type IntegrationAppName = 'gmail' | 'jira' | 'googledrive' | 'salesforce' | 'googlecalendar'
|
|
53
|
+
type IntegrationAppName = 'gmail' | 'jira' | 'googledrive' | 'salesforce' | 'googlecalendar' | 'slack'
|
|
41
54
|
|
|
42
55
|
interface GetTokenOptions {
|
|
43
56
|
interactive?: boolean // show OAuth flow if not yet authorized
|
|
@@ -70,13 +83,34 @@ Execute JavaScript on the host page.
|
|
|
70
83
|
executeScript(code: string, config?: RpcConfig): Promise<void>
|
|
71
84
|
```
|
|
72
85
|
|
|
73
|
-
|
|
86
|
+
## TCP API
|
|
74
87
|
|
|
75
|
-
|
|
88
|
+
All data is base64-encoded. Import as `import { tcp }`.
|
|
89
|
+
|
|
90
|
+
### tcp.connect(host, port)
|
|
91
|
+
|
|
92
|
+
Open a TCP connection. Returns a `TcpSocket` object for subsequent operations, or `null` if TCP is unavailable.
|
|
76
93
|
|
|
77
94
|
```typescript
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
95
|
+
tcp.connect(host: string, port: number, config?: RpcConfig): Promise<TcpSocket | null>
|
|
96
|
+
|
|
97
|
+
interface TcpSocket {
|
|
98
|
+
send(dataBase64: string, config?: RpcConfig): Promise<number | null>
|
|
99
|
+
receive(timeoutMs?: number, config?: RpcConfig): Promise<string | null>
|
|
100
|
+
close(config?: RpcConfig): Promise<void>
|
|
81
101
|
}
|
|
82
102
|
```
|
|
103
|
+
|
|
104
|
+
### TcpSocket.send(dataBase64)
|
|
105
|
+
|
|
106
|
+
Send data over an open socket. Data must be base64-encoded. Returns the number of bytes sent, or `null` if TCP is unavailable.
|
|
107
|
+
|
|
108
|
+
### TcpSocket.receive(timeoutMs?)
|
|
109
|
+
|
|
110
|
+
Wait for and receive data from an open socket. Returns base64-encoded data, or `null` if TCP is unavailable. Defaults to a 10 second timeout.
|
|
111
|
+
|
|
112
|
+
**Two-timeout interaction:** `receive()` has two independent timeouts: `timeoutMs` (host-side, how long to wait for data) and `RpcConfig.timeout` (SDK-side postMessage round-trip, default 15 s). If `timeoutMs` is longer than `RpcConfig.timeout`, the RPC layer will reject before the host-side timeout fires. Set `RpcConfig.timeout` to at least `timeoutMs + 5000` to avoid spurious RPC timeout rejections.
|
|
113
|
+
|
|
114
|
+
### TcpSocket.close()
|
|
115
|
+
|
|
116
|
+
Disconnect and close an open socket.
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { generateMessage, getToken, getUserInfo } from './lib/functions';
|
|
2
|
+
export { tcp } from './lib/tcp-socket';
|
|
3
|
+
export type { TcpSocket } from './lib/tcp-socket';
|
|
2
4
|
export type { RpcConfig } from './lib/rpc-bridge';
|
|
3
5
|
export type { GenerateMessageOptions, GetTokenOptions, GetTokenResult, IntegrationAppName, UserInfo } from './lib/types';
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=15e3,u=new Map;let l=!1;function p(){l||(l=!0,window.addEventListener("message",t=>{const e=t.data;if(!e?.messageId||!("name"in e))return;const n=u.get(e.messageId);n&&(clearTimeout(n.timer),u.delete(e.messageId),e.error?n.reject(new Error(e.error.message??"RPC error")):n.resolve(e.data))}))}function s(t,e,n){p();const o=n?.timeout??m,r=n?.targetOrigin??"*";return new Promise((c,a)=>{const i=crypto.randomUUID(),g=setTimeout(()=>{u.delete(i),a(new Error(`RPC timeout: ${t}`))},o);u.set(i,{resolve:c,reject:a,timer:g});const d={action:t,params:e,messageId:i};window.parent.postMessage(d,r)})}async function f(t,e,n){return s("generateMessage",[t,e],n)}async function y(t,e,n){return s("getToken",[t,e],n)}async function w(t){return s("getUserInfo",[],t)}async function I(t,e){return s("tcpConnect",[t],e)}async function T(t,e,n){return s("tcpSend",[t,e],n)}async function v(t,e,n){return s("tcpReceive",[t,e],n)}async function C(t,e){return s("tcpClose",[t],e)}const U={async connect(t,e,n){const o=await I({host:t,port:e},n);return o===null?null:{send:async(r,c)=>(await T(o,{dataBase64:r},c??n))?.bytesSent??null,receive:async(r,c)=>(await v(o,r!==void 0?{timeoutMs:r}:void 0,c??n))?.data??null,close:r=>C(o,r??n)}}};exports.generateMessage=f;exports.getToken=y;exports.getUserInfo=w;exports.tcp=U;
|
package/index.mjs
CHANGED
|
@@ -1,40 +1,71 @@
|
|
|
1
|
-
const
|
|
2
|
-
let
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
const e =
|
|
6
|
-
if (!
|
|
7
|
-
const
|
|
8
|
-
|
|
1
|
+
const u = /* @__PURE__ */ new Map();
|
|
2
|
+
let l = !1;
|
|
3
|
+
function m() {
|
|
4
|
+
l || (l = !0, window.addEventListener("message", (t) => {
|
|
5
|
+
const e = t.data;
|
|
6
|
+
if (!e?.messageId || !("name" in e)) return;
|
|
7
|
+
const n = u.get(e.messageId);
|
|
8
|
+
n && (clearTimeout(n.timer), u.delete(e.messageId), e.error ? n.reject(new Error(e.error.message ?? "RPC error")) : n.resolve(e.data));
|
|
9
9
|
}));
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
return new Promise((
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
resolve:
|
|
20
|
-
reject:
|
|
11
|
+
function s(t, e, n) {
|
|
12
|
+
m();
|
|
13
|
+
const c = n?.timeout ?? 15e3, r = n?.targetOrigin ?? "*";
|
|
14
|
+
return new Promise((o, a) => {
|
|
15
|
+
const i = crypto.randomUUID(), d = setTimeout(() => {
|
|
16
|
+
u.delete(i), a(new Error(`RPC timeout: ${t}`));
|
|
17
|
+
}, c);
|
|
18
|
+
u.set(i, {
|
|
19
|
+
resolve: o,
|
|
20
|
+
reject: a,
|
|
21
21
|
timer: d
|
|
22
22
|
});
|
|
23
|
-
const
|
|
24
|
-
window.parent.postMessage(
|
|
23
|
+
const g = { action: t, params: e, messageId: i };
|
|
24
|
+
window.parent.postMessage(g, r);
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
async function T(
|
|
28
|
-
return
|
|
27
|
+
async function T(t, e, n) {
|
|
28
|
+
return s("generateMessage", [t, e], n);
|
|
29
29
|
}
|
|
30
|
-
async function I(
|
|
31
|
-
return
|
|
30
|
+
async function I(t, e, n) {
|
|
31
|
+
return s("getToken", [t, e], n);
|
|
32
32
|
}
|
|
33
|
-
async function
|
|
34
|
-
return
|
|
33
|
+
async function U(t) {
|
|
34
|
+
return s("getUserInfo", [], t);
|
|
35
35
|
}
|
|
36
|
+
async function p(t, e) {
|
|
37
|
+
return s("tcpConnect", [t], e);
|
|
38
|
+
}
|
|
39
|
+
async function f(t, e, n) {
|
|
40
|
+
return s("tcpSend", [t, e], n);
|
|
41
|
+
}
|
|
42
|
+
async function y(t, e, n) {
|
|
43
|
+
return s("tcpReceive", [t, e], n);
|
|
44
|
+
}
|
|
45
|
+
async function w(t, e) {
|
|
46
|
+
return s("tcpClose", [t], e);
|
|
47
|
+
}
|
|
48
|
+
const v = {
|
|
49
|
+
/**
|
|
50
|
+
* Opens a TCP connection and returns a `TcpSocket` wrapper.
|
|
51
|
+
*
|
|
52
|
+
* The `TcpSocket` wrapper hides the socket ID as an implementation detail,
|
|
53
|
+
* preventing accidental misuse. Authorization (ensuring only the owning app can
|
|
54
|
+
* use a socket) is enforced on the host side — do not assume the closure provides
|
|
55
|
+
* security guarantees.
|
|
56
|
+
*/
|
|
57
|
+
async connect(t, e, n) {
|
|
58
|
+
const c = await p({ host: t, port: e }, n);
|
|
59
|
+
return c === null ? null : {
|
|
60
|
+
send: async (r, o) => (await f(c, { dataBase64: r }, o ?? n))?.bytesSent ?? null,
|
|
61
|
+
receive: async (r, o) => (await y(c, r !== void 0 ? { timeoutMs: r } : void 0, o ?? n))?.data ?? null,
|
|
62
|
+
close: (r) => w(c, r ?? n)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
36
66
|
export {
|
|
37
67
|
T as generateMessage,
|
|
38
68
|
I as getToken,
|
|
39
|
-
|
|
69
|
+
U as getUserInfo,
|
|
70
|
+
v as tcp
|
|
40
71
|
};
|
package/lib/functions.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { RpcConfig } from './rpc-bridge';
|
|
2
|
-
import { GenerateMessageOptions, GetTokenOptions, GetTokenResult, IntegrationAppName, UserInfo } from './types';
|
|
2
|
+
import { GenerateMessageOptions, GetTokenOptions, GetTokenResult, IntegrationAppName, TcpConnectOptions, TcpConnectionSettings, TcpReceiveOptions, TcpReceiveResult, TcpSendMessage, TcpSendResult, UserInfo } from './types';
|
|
3
3
|
export declare function generateMessage(prompt: string, options: GenerateMessageOptions, config?: RpcConfig): Promise<string | null>;
|
|
4
4
|
export declare function getToken(appName: IntegrationAppName, options?: GetTokenOptions, config?: RpcConfig): Promise<GetTokenResult | null>;
|
|
5
5
|
export declare function getUserInfo(config?: RpcConfig): Promise<UserInfo | null>;
|
|
6
6
|
export declare function executeScript(code: string, config?: RpcConfig): Promise<void>;
|
|
7
|
+
export declare function tcpConnect(options: TcpConnectOptions, config?: RpcConfig): Promise<TcpConnectionSettings | null>;
|
|
8
|
+
export declare function tcpSend(connection: TcpConnectionSettings, message: TcpSendMessage, config?: RpcConfig): Promise<TcpSendResult | null>;
|
|
9
|
+
export declare function tcpReceive(connection: TcpConnectionSettings, options?: TcpReceiveOptions, config?: RpcConfig): Promise<TcpReceiveResult | null>;
|
|
10
|
+
export declare function tcpClose(connection: TcpConnectionSettings, config?: RpcConfig): Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RpcConfig } from './rpc-bridge';
|
|
2
|
+
export interface TcpSocket {
|
|
3
|
+
send(dataBase64: string, config?: RpcConfig): Promise<number | null>;
|
|
4
|
+
receive(timeoutMs?: number, config?: RpcConfig): Promise<string | null>;
|
|
5
|
+
close(config?: RpcConfig): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare const tcp: {
|
|
8
|
+
/**
|
|
9
|
+
* Opens a TCP connection and returns a `TcpSocket` wrapper.
|
|
10
|
+
*
|
|
11
|
+
* The `TcpSocket` wrapper hides the socket ID as an implementation detail,
|
|
12
|
+
* preventing accidental misuse. Authorization (ensuring only the owning app can
|
|
13
|
+
* use a socket) is enforced on the host side — do not assume the closure provides
|
|
14
|
+
* security guarantees.
|
|
15
|
+
*/
|
|
16
|
+
connect(host: string, port: number, config?: RpcConfig): Promise<TcpSocket | null>;
|
|
17
|
+
};
|
package/lib/types.d.ts
CHANGED
|
@@ -21,3 +21,22 @@ export interface UserInfo {
|
|
|
21
21
|
email?: string;
|
|
22
22
|
picture?: string;
|
|
23
23
|
}
|
|
24
|
+
export interface TcpConnectOptions {
|
|
25
|
+
host: string;
|
|
26
|
+
port: number;
|
|
27
|
+
}
|
|
28
|
+
export interface TcpConnectionSettings {
|
|
29
|
+
socketId: number;
|
|
30
|
+
}
|
|
31
|
+
export interface TcpSendMessage {
|
|
32
|
+
dataBase64: string;
|
|
33
|
+
}
|
|
34
|
+
export interface TcpSendResult {
|
|
35
|
+
bytesSent: number;
|
|
36
|
+
}
|
|
37
|
+
export interface TcpReceiveOptions {
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface TcpReceiveResult {
|
|
41
|
+
data: string;
|
|
42
|
+
}
|