ai-publish-sdk 1.2.0 → 1.3.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 +19 -20
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/index.mjs +57 -44
- package/lib/functions.d.ts +8 -9
- package/lib/rpc-bridge.d.ts +4 -5
- package/lib/tcp-socket.d.ts +4 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,17 +8,18 @@ SDK for embedded apps to communicate with the host environment.
|
|
|
8
8
|
npm install ai-publish-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Timeout Configuration
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Optional config passed to any function call.
|
|
13
|
+
Default timeout is 15 seconds. You can change it globally or wrap a single call:
|
|
16
14
|
|
|
17
15
|
```typescript
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
import { setGlobalTimeout, withTimeout, getUserInfo } from 'ai-publish-sdk'
|
|
17
|
+
|
|
18
|
+
// Change the default for all calls
|
|
19
|
+
setGlobalTimeout(30_000)
|
|
20
|
+
|
|
21
|
+
// Override for a single call
|
|
22
|
+
await withTimeout(() => getUserInfo(), 10_000)
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
## Core API
|
|
@@ -28,7 +29,7 @@ interface RpcConfig {
|
|
|
28
29
|
Get current user info.
|
|
29
30
|
|
|
30
31
|
```typescript
|
|
31
|
-
getUserInfo(
|
|
32
|
+
getUserInfo(): Promise<UserInfo | null>
|
|
32
33
|
|
|
33
34
|
interface UserInfo {
|
|
34
35
|
userId: string
|
|
@@ -48,7 +49,7 @@ interface UserInfo {
|
|
|
48
49
|
Get OAuth token for an integration.
|
|
49
50
|
|
|
50
51
|
```typescript
|
|
51
|
-
getToken(appName: IntegrationAppName, options?: GetTokenOptions
|
|
52
|
+
getToken(appName: IntegrationAppName, options?: GetTokenOptions): Promise<GetTokenResult | null>
|
|
52
53
|
|
|
53
54
|
type IntegrationAppName = 'gmail' | 'jira' | 'googledrive' | 'salesforce' | 'googlecalendar' | 'slack'
|
|
54
55
|
|
|
@@ -67,7 +68,7 @@ interface GetTokenResult {
|
|
|
67
68
|
Generate an AI message.
|
|
68
69
|
|
|
69
70
|
```typescript
|
|
70
|
-
generateMessage(prompt: string, options: GenerateMessageOptions
|
|
71
|
+
generateMessage(prompt: string, options: GenerateMessageOptions): Promise<string | null>
|
|
71
72
|
|
|
72
73
|
interface GenerateMessageOptions {
|
|
73
74
|
withPageContext?: boolean // include current page content as context
|
|
@@ -80,7 +81,7 @@ interface GenerateMessageOptions {
|
|
|
80
81
|
Execute JavaScript on the host page.
|
|
81
82
|
|
|
82
83
|
```typescript
|
|
83
|
-
executeScript(code: string
|
|
84
|
+
executeScript(code: string): Promise<void>
|
|
84
85
|
```
|
|
85
86
|
|
|
86
87
|
## TCP API
|
|
@@ -92,12 +93,12 @@ All data is base64-encoded. Import as `import { tcp }`.
|
|
|
92
93
|
Open a TCP connection. Returns a `TcpSocket` object for subsequent operations, or `null` if TCP is unavailable.
|
|
93
94
|
|
|
94
95
|
```typescript
|
|
95
|
-
tcp.connect(host: string, port: number
|
|
96
|
+
tcp.connect(host: string, port: number): Promise<TcpSocket | null>
|
|
96
97
|
|
|
97
98
|
interface TcpSocket {
|
|
98
|
-
send(dataBase64: string
|
|
99
|
-
receive(
|
|
100
|
-
close(
|
|
99
|
+
send(dataBase64: string): Promise<number | null>
|
|
100
|
+
receive(): Promise<string | null>
|
|
101
|
+
close(): Promise<void>
|
|
101
102
|
}
|
|
102
103
|
```
|
|
103
104
|
|
|
@@ -105,11 +106,9 @@ interface TcpSocket {
|
|
|
105
106
|
|
|
106
107
|
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
|
|
|
108
|
-
### TcpSocket.receive(
|
|
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.
|
|
109
|
+
### TcpSocket.receive()
|
|
111
110
|
|
|
112
|
-
|
|
111
|
+
Wait for and receive data from an open socket. Returns base64-encoded data, or `null` if TCP is unavailable. Use `withTimeout(() => socket.receive(), ms)` for custom timeouts.
|
|
113
112
|
|
|
114
113
|
### TcpSocket.close()
|
|
115
114
|
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { generateMessage, getToken, getUserInfo } from './lib/functions';
|
|
2
2
|
export { tcp } from './lib/tcp-socket';
|
|
3
3
|
export type { TcpSocket } from './lib/tcp-socket';
|
|
4
|
-
export
|
|
4
|
+
export { setGlobalTimeout, withTimeout } from './lib/rpc-bridge';
|
|
5
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"});
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let u=15e3;const c=new Map;let a=!1;function m(e){u=e}let i;function d(e,t){i=t;try{return e()}finally{i=void 0}}function f(){a||(a=!0,window.addEventListener("message",e=>{const t=e.data;if(!t?.messageId||!("name"in t))return;const n=c.get(t.messageId);n&&(clearTimeout(n.timer),c.delete(t.messageId),t.error?n.reject(new Error(t.error.message??"RPC error")):n.resolve(t.data))}))}function r(e,t){return f(),new Promise((n,s)=>{const o=crypto.randomUUID(),l=setTimeout(()=>{c.delete(o),s(new Error(`RPC timeout: ${e}`))},i??u);c.set(o,{resolve:n,reject:s,timer:l});const g={action:e,params:t,messageId:o};window.parent.postMessage(g,"*")})}async function p(e,t){return r("generateMessage",[e,t])}async function y(e,t){return r("getToken",[e,t])}async function w(){return r("getUserInfo",[])}async function T(e){return r("tcpConnect",[e])}async function v(e,t){return r("tcpSend",[e,t])}async function I(e,t){return r("tcpReceive",[e,t])}async function C(e){return r("tcpClose",[e])}const b={async connect(e,t){const n=await T({host:e,port:t});return n===null?null:{send:async s=>(await v(n,{dataBase64:s}))?.bytesSent??null,receive:async()=>(await I(n))?.data??null,close:()=>C(n)}}};exports.generateMessage=p;exports.getToken=y;exports.getUserInfo=w;exports.setGlobalTimeout=m;exports.tcp=b;exports.withTimeout=d;
|
package/index.mjs
CHANGED
|
@@ -1,51 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
let u = 15e3;
|
|
2
|
+
const c = /* @__PURE__ */ new Map();
|
|
3
|
+
let i = !1;
|
|
4
|
+
function T(e) {
|
|
5
|
+
u = e;
|
|
6
|
+
}
|
|
7
|
+
let a;
|
|
8
|
+
function v(e, t) {
|
|
9
|
+
a = t;
|
|
10
|
+
try {
|
|
11
|
+
return e();
|
|
12
|
+
} finally {
|
|
13
|
+
a = void 0;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
3
16
|
function m() {
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
if (!
|
|
7
|
-
const n =
|
|
8
|
-
n && (clearTimeout(n.timer),
|
|
17
|
+
i || (i = !0, window.addEventListener("message", (e) => {
|
|
18
|
+
const t = e.data;
|
|
19
|
+
if (!t?.messageId || !("name" in t)) return;
|
|
20
|
+
const n = c.get(t.messageId);
|
|
21
|
+
n && (clearTimeout(n.timer), c.delete(t.messageId), t.error ? n.reject(new Error(t.error.message ?? "RPC error")) : n.resolve(t.data));
|
|
9
22
|
}));
|
|
10
23
|
}
|
|
11
|
-
function
|
|
12
|
-
m()
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
reject: a,
|
|
21
|
-
timer: d
|
|
24
|
+
function r(e, t) {
|
|
25
|
+
return m(), new Promise((n, s) => {
|
|
26
|
+
const o = crypto.randomUUID(), l = setTimeout(() => {
|
|
27
|
+
c.delete(o), s(new Error(`RPC timeout: ${e}`));
|
|
28
|
+
}, a ?? u);
|
|
29
|
+
c.set(o, {
|
|
30
|
+
resolve: n,
|
|
31
|
+
reject: s,
|
|
32
|
+
timer: l
|
|
22
33
|
});
|
|
23
|
-
const
|
|
24
|
-
window.parent.postMessage(
|
|
34
|
+
const d = { action: e, params: t, messageId: o };
|
|
35
|
+
window.parent.postMessage(d, "*");
|
|
25
36
|
});
|
|
26
37
|
}
|
|
27
|
-
async function
|
|
28
|
-
return
|
|
38
|
+
async function C(e, t) {
|
|
39
|
+
return r("generateMessage", [e, t]);
|
|
29
40
|
}
|
|
30
|
-
async function I(
|
|
31
|
-
return
|
|
41
|
+
async function I(e, t) {
|
|
42
|
+
return r("getToken", [e, t]);
|
|
32
43
|
}
|
|
33
|
-
async function
|
|
34
|
-
return
|
|
44
|
+
async function M() {
|
|
45
|
+
return r("getUserInfo", []);
|
|
35
46
|
}
|
|
36
|
-
async function
|
|
37
|
-
return
|
|
47
|
+
async function f(e) {
|
|
48
|
+
return r("tcpConnect", [e]);
|
|
38
49
|
}
|
|
39
|
-
async function
|
|
40
|
-
return
|
|
50
|
+
async function g(e, t) {
|
|
51
|
+
return r("tcpSend", [e, t]);
|
|
41
52
|
}
|
|
42
|
-
async function
|
|
43
|
-
return
|
|
53
|
+
async function p(e, t) {
|
|
54
|
+
return r("tcpReceive", [e, t]);
|
|
44
55
|
}
|
|
45
|
-
async function
|
|
46
|
-
return
|
|
56
|
+
async function y(e) {
|
|
57
|
+
return r("tcpClose", [e]);
|
|
47
58
|
}
|
|
48
|
-
const
|
|
59
|
+
const R = {
|
|
49
60
|
/**
|
|
50
61
|
* Opens a TCP connection and returns a `TcpSocket` wrapper.
|
|
51
62
|
*
|
|
@@ -54,18 +65,20 @@ const v = {
|
|
|
54
65
|
* use a socket) is enforced on the host side — do not assume the closure provides
|
|
55
66
|
* security guarantees.
|
|
56
67
|
*/
|
|
57
|
-
async connect(
|
|
58
|
-
const
|
|
59
|
-
return
|
|
60
|
-
send: async (
|
|
61
|
-
receive: async (
|
|
62
|
-
close: (
|
|
68
|
+
async connect(e, t) {
|
|
69
|
+
const n = await f({ host: e, port: t });
|
|
70
|
+
return n === null ? null : {
|
|
71
|
+
send: async (s) => (await g(n, { dataBase64: s }))?.bytesSent ?? null,
|
|
72
|
+
receive: async () => (await p(n))?.data ?? null,
|
|
73
|
+
close: () => y(n)
|
|
63
74
|
};
|
|
64
75
|
}
|
|
65
76
|
};
|
|
66
77
|
export {
|
|
67
|
-
|
|
78
|
+
C as generateMessage,
|
|
68
79
|
I as getToken,
|
|
69
|
-
|
|
70
|
-
|
|
80
|
+
M as getUserInfo,
|
|
81
|
+
T as setGlobalTimeout,
|
|
82
|
+
R as tcp,
|
|
83
|
+
v as withTimeout
|
|
71
84
|
};
|
package/lib/functions.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { RpcConfig } from './rpc-bridge';
|
|
2
1
|
import { GenerateMessageOptions, GetTokenOptions, GetTokenResult, IntegrationAppName, TcpConnectOptions, TcpConnectionSettings, TcpReceiveOptions, TcpReceiveResult, TcpSendMessage, TcpSendResult, UserInfo } from './types';
|
|
3
|
-
export declare function generateMessage(prompt: string, options: GenerateMessageOptions
|
|
4
|
-
export declare function getToken(appName: IntegrationAppName, options?: GetTokenOptions
|
|
5
|
-
export declare function getUserInfo(
|
|
6
|
-
export declare function executeScript(code: string
|
|
7
|
-
export declare function tcpConnect(options: TcpConnectOptions
|
|
8
|
-
export declare function tcpSend(connection: TcpConnectionSettings, message: TcpSendMessage
|
|
9
|
-
export declare function tcpReceive(connection: TcpConnectionSettings, options?: TcpReceiveOptions
|
|
10
|
-
export declare function tcpClose(connection: TcpConnectionSettings
|
|
2
|
+
export declare function generateMessage(prompt: string, options: GenerateMessageOptions): Promise<string | null>;
|
|
3
|
+
export declare function getToken(appName: IntegrationAppName, options?: GetTokenOptions): Promise<GetTokenResult | null>;
|
|
4
|
+
export declare function getUserInfo(): Promise<UserInfo | null>;
|
|
5
|
+
export declare function executeScript(code: string): Promise<void>;
|
|
6
|
+
export declare function tcpConnect(options: TcpConnectOptions): Promise<TcpConnectionSettings | null>;
|
|
7
|
+
export declare function tcpSend(connection: TcpConnectionSettings, message: TcpSendMessage): Promise<TcpSendResult | null>;
|
|
8
|
+
export declare function tcpReceive(connection: TcpConnectionSettings, options?: TcpReceiveOptions): Promise<TcpReceiveResult | null>;
|
|
9
|
+
export declare function tcpClose(connection: TcpConnectionSettings): Promise<void>;
|
package/lib/rpc-bridge.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare function rpcCall<T = unknown>(action: string, params: unknown[], config?: RpcConfig): Promise<T>;
|
|
1
|
+
export declare function setGlobalTimeout(ms: number): void;
|
|
2
|
+
export declare function resetForTesting(): void;
|
|
3
|
+
export declare function withTimeout<T>(fn: () => Promise<T>, ms: number): Promise<T>;
|
|
4
|
+
export declare function rpcCall<T = unknown>(action: string, params: unknown[]): Promise<T>;
|
package/lib/tcp-socket.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { RpcConfig } from './rpc-bridge';
|
|
2
1
|
export interface TcpSocket {
|
|
3
|
-
send(dataBase64: string
|
|
4
|
-
receive(
|
|
5
|
-
close(
|
|
2
|
+
send(dataBase64: string): Promise<number | null>;
|
|
3
|
+
receive(): Promise<string | null>;
|
|
4
|
+
close(): Promise<void>;
|
|
6
5
|
}
|
|
7
6
|
export declare const tcp: {
|
|
8
7
|
/**
|
|
@@ -13,5 +12,5 @@ export declare const tcp: {
|
|
|
13
12
|
* use a socket) is enforced on the host side — do not assume the closure provides
|
|
14
13
|
* security guarantees.
|
|
15
14
|
*/
|
|
16
|
-
connect(host: string, port: number
|
|
15
|
+
connect(host: string, port: number): Promise<TcpSocket | null>;
|
|
17
16
|
};
|