ai-publish-sdk 1.0.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 +74 -0
- package/index.d.ts +3 -0
- package/index.js +1 -0
- package/index.mjs +40 -0
- package/lib/functions.d.ts +5 -0
- package/lib/rpc-bridge.d.ts +5 -0
- package/lib/types.d.ts +23 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# ai-publish-sdk
|
|
2
|
+
|
|
3
|
+
SDK for embedded apps to communicate with the host environment.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install ai-publish-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Core API
|
|
12
|
+
|
|
13
|
+
### getUserInfo()
|
|
14
|
+
|
|
15
|
+
Get current user info.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
getUserInfo(config?: RpcConfig): Promise<UserInfo | null>
|
|
19
|
+
|
|
20
|
+
interface UserInfo {
|
|
21
|
+
userId: string
|
|
22
|
+
tenantId: string
|
|
23
|
+
tenantName?: string
|
|
24
|
+
name?: string
|
|
25
|
+
givenName?: string
|
|
26
|
+
familyName?: string
|
|
27
|
+
displayName?: string
|
|
28
|
+
email?: string
|
|
29
|
+
picture?: string
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### getToken(appName, options?)
|
|
34
|
+
|
|
35
|
+
Get OAuth token for an integration.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
getToken(appName: IntegrationAppName, options?: GetTokenOptions, config?: RpcConfig): Promise<GetTokenResult | null>
|
|
39
|
+
|
|
40
|
+
type IntegrationAppName = 'gmail' | 'jira' | 'googledrive' | 'salesforce' | 'googlecalendar'
|
|
41
|
+
|
|
42
|
+
interface GetTokenOptions {
|
|
43
|
+
interactive?: boolean // show OAuth flow if not yet authorized
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface GetTokenResult {
|
|
47
|
+
token: string
|
|
48
|
+
instanceUrl?: string // e.g. Salesforce instance URL
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### generateMessage(prompt, options)
|
|
53
|
+
|
|
54
|
+
Generate an AI message.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
generateMessage(prompt: string, options: GenerateMessageOptions, config?: RpcConfig): Promise<string | null>
|
|
58
|
+
|
|
59
|
+
interface GenerateMessageOptions {
|
|
60
|
+
withPageContext?: boolean // include current page content as context
|
|
61
|
+
systemPrompt?: string // guide AI behavior
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### RpcConfig
|
|
66
|
+
|
|
67
|
+
Optional config passed to any function call.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface RpcConfig {
|
|
71
|
+
timeout?: number // ms, default 15000
|
|
72
|
+
targetOrigin?: string // postMessage target origin, default '*'
|
|
73
|
+
}
|
|
74
|
+
```
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=15e3,n=new Map;let i=!1;function I(){i||(i=!0,window.addEventListener("message",r=>{const e=r.data;if(!(e!=null&&e.messageId)||!("name"in e))return;const t=n.get(e.messageId);t&&(clearTimeout(t.timer),n.delete(e.messageId),e.error?t.reject(new Error(e.error.message??"RPC error")):t.resolve(e.data))}))}function o(r,e,t){I();const u=(t==null?void 0:t.timeout)??c,g=(t==null?void 0:t.targetOrigin)??"*";return new Promise((m,a)=>{const s=crypto.randomUUID(),d=setTimeout(()=>{n.delete(s),a(new Error(`RPC timeout: ${r}`))},u);n.set(s,{resolve:m,reject:a,timer:d});const l={action:r,params:e,messageId:s};window.parent.postMessage(l,g)})}async function T(r,e,t){return o("generateMessage",[r,e],t)}async function w(r,e,t){return o("getToken",[r,e],t)}async function p(r){return o("getUserInfo",[],r)}exports.generateMessage=T;exports.getToken=w;exports.getUserInfo=p;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const n = /* @__PURE__ */ new Map();
|
|
2
|
+
let i = !1;
|
|
3
|
+
function l() {
|
|
4
|
+
i || (i = !0, window.addEventListener("message", (r) => {
|
|
5
|
+
const e = r.data;
|
|
6
|
+
if (!(e != null && e.messageId) || !("name" in e)) return;
|
|
7
|
+
const t = n.get(e.messageId);
|
|
8
|
+
t && (clearTimeout(t.timer), n.delete(e.messageId), e.error ? t.reject(new Error(e.error.message ?? "RPC error")) : t.resolve(e.data));
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
function a(r, e, t) {
|
|
12
|
+
l();
|
|
13
|
+
const u = (t == null ? void 0 : t.timeout) ?? 15e3, m = (t == null ? void 0 : t.targetOrigin) ?? "*";
|
|
14
|
+
return new Promise((g, o) => {
|
|
15
|
+
const s = crypto.randomUUID(), d = setTimeout(() => {
|
|
16
|
+
n.delete(s), o(new Error(`RPC timeout: ${r}`));
|
|
17
|
+
}, u);
|
|
18
|
+
n.set(s, {
|
|
19
|
+
resolve: g,
|
|
20
|
+
reject: o,
|
|
21
|
+
timer: d
|
|
22
|
+
});
|
|
23
|
+
const c = { action: r, params: e, messageId: s };
|
|
24
|
+
window.parent.postMessage(c, m);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async function T(r, e, t) {
|
|
28
|
+
return a("generateMessage", [r, e], t);
|
|
29
|
+
}
|
|
30
|
+
async function I(r, e, t) {
|
|
31
|
+
return a("getToken", [r, e], t);
|
|
32
|
+
}
|
|
33
|
+
async function w(r) {
|
|
34
|
+
return a("getUserInfo", [], r);
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
T as generateMessage,
|
|
38
|
+
I as getToken,
|
|
39
|
+
w as getUserInfo
|
|
40
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { RpcConfig } from './rpc-bridge';
|
|
2
|
+
import { GenerateMessageOptions, GetTokenOptions, GetTokenResult, IntegrationAppName, 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>;
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type IntegrationAppName = 'gmail' | 'jira' | 'googledrive' | 'salesforce' | 'googlecalendar' | 'slack';
|
|
2
|
+
export interface GenerateMessageOptions {
|
|
3
|
+
withPageContext?: boolean;
|
|
4
|
+
systemPrompt?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface GetTokenOptions {
|
|
7
|
+
interactive?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface GetTokenResult {
|
|
10
|
+
token: string;
|
|
11
|
+
instanceUrl?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface UserInfo {
|
|
14
|
+
userId: string;
|
|
15
|
+
tenantId: string;
|
|
16
|
+
tenantName?: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
givenName?: string;
|
|
19
|
+
familyName?: string;
|
|
20
|
+
displayName?: string;
|
|
21
|
+
email?: string;
|
|
22
|
+
picture?: string;
|
|
23
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-publish-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"module": "./index.mjs",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"description": "SDK for embedded apps to communicate with the host environment via RPC over postMessage",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.mjs",
|
|
13
|
+
"require": "./index.js",
|
|
14
|
+
"default": "./index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"keywords": [
|
|
22
|
+
"iframe",
|
|
23
|
+
"rpc",
|
|
24
|
+
"postmessage",
|
|
25
|
+
"sdk",
|
|
26
|
+
"embedded-app"
|
|
27
|
+
]
|
|
28
|
+
}
|