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 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
- ## General
11
+ ## Timeout Configuration
12
12
 
13
- ### RpcConfig
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
- interface RpcConfig {
19
- timeout?: number // ms, default 15000
20
- targetOrigin?: string // postMessage target origin, default '*'
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(config?: RpcConfig): Promise<UserInfo | null>
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, config?: RpcConfig): Promise<GetTokenResult | null>
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, config?: RpcConfig): Promise<string | null>
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, config?: RpcConfig): Promise<void>
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, config?: RpcConfig): Promise<TcpSocket | null>
96
+ tcp.connect(host: string, port: number): Promise<TcpSocket | null>
96
97
 
97
98
  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>
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(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.
109
+ ### TcpSocket.receive()
111
110
 
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.
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 type { RpcConfig } from './lib/rpc-bridge';
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"});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;
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
- const u = /* @__PURE__ */ new Map();
2
- let l = !1;
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
- 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));
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 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
- 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 g = { action: t, params: e, messageId: i };
24
- window.parent.postMessage(g, r);
34
+ const d = { action: e, params: t, messageId: o };
35
+ window.parent.postMessage(d, "*");
25
36
  });
26
37
  }
27
- async function T(t, e, n) {
28
- return s("generateMessage", [t, e], n);
38
+ async function C(e, t) {
39
+ return r("generateMessage", [e, t]);
29
40
  }
30
- async function I(t, e, n) {
31
- return s("getToken", [t, e], n);
41
+ async function I(e, t) {
42
+ return r("getToken", [e, t]);
32
43
  }
33
- async function U(t) {
34
- return s("getUserInfo", [], t);
44
+ async function M() {
45
+ return r("getUserInfo", []);
35
46
  }
36
- async function p(t, e) {
37
- return s("tcpConnect", [t], e);
47
+ async function f(e) {
48
+ return r("tcpConnect", [e]);
38
49
  }
39
- async function f(t, e, n) {
40
- return s("tcpSend", [t, e], n);
50
+ async function g(e, t) {
51
+ return r("tcpSend", [e, t]);
41
52
  }
42
- async function y(t, e, n) {
43
- return s("tcpReceive", [t, e], n);
53
+ async function p(e, t) {
54
+ return r("tcpReceive", [e, t]);
44
55
  }
45
- async function w(t, e) {
46
- return s("tcpClose", [t], e);
56
+ async function y(e) {
57
+ return r("tcpClose", [e]);
47
58
  }
48
- const v = {
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(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)
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
- T as generateMessage,
78
+ C as generateMessage,
68
79
  I as getToken,
69
- U as getUserInfo,
70
- v as tcp
80
+ M as getUserInfo,
81
+ T as setGlobalTimeout,
82
+ R as tcp,
83
+ v as withTimeout
71
84
  };
@@ -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, config?: RpcConfig): Promise<string | null>;
4
- export declare function getToken(appName: IntegrationAppName, options?: GetTokenOptions, config?: RpcConfig): Promise<GetTokenResult | null>;
5
- export declare function getUserInfo(config?: RpcConfig): Promise<UserInfo | null>;
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>;
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>;
@@ -1,5 +1,4 @@
1
- export interface RpcConfig {
2
- timeout?: number;
3
- targetOrigin?: string;
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>;
@@ -1,8 +1,7 @@
1
- import { RpcConfig } from './rpc-bridge';
2
1
  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>;
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, config?: RpcConfig): Promise<TcpSocket | null>;
15
+ connect(host: string, port: number): Promise<TcpSocket | null>;
17
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-publish-sdk",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "module": "./index.mjs",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",