@shadowob/sdk 1.1.7 → 1.1.9
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/bin/shadow-server-app.mjs +58 -0
- package/dist/bridge.cjs +364 -0
- package/dist/bridge.d.cts +105 -0
- package/dist/bridge.d.ts +105 -0
- package/dist/bridge.js +26 -0
- package/dist/chunk-5QQ7ZR6B.js +192 -0
- package/dist/chunk-OMTGSMTQ.js +525 -0
- package/dist/index.cjs +925 -4
- package/dist/index.d.cts +183 -1876
- package/dist/index.d.ts +183 -1876
- package/dist/index.js +252 -3
- package/dist/server-app-node.cjs +82 -0
- package/dist/server-app-node.d.cts +21 -0
- package/dist/server-app-node.d.ts +21 -0
- package/dist/server-app-node.js +56 -0
- package/dist/server-app-qqsBtGmE.d.cts +2445 -0
- package/dist/server-app-qqsBtGmE.d.ts +2445 -0
- package/dist/server-app.cjs +575 -0
- package/dist/server-app.d.cts +2 -0
- package/dist/server-app.d.ts +2 -0
- package/dist/server-app.js +58 -0
- package/package.json +29 -2
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { dirname, relative, resolve } from 'node:path'
|
|
4
|
+
|
|
5
|
+
function usage(exitCode = 1) {
|
|
6
|
+
const out = exitCode === 0 ? console.log : console.error
|
|
7
|
+
out(`Usage:
|
|
8
|
+
shadow-server-app typegen <manifest.json> <output.ts>
|
|
9
|
+
|
|
10
|
+
Generates a typed TypeScript manifest module from a Shadow server app JSON manifest.`)
|
|
11
|
+
process.exit(exitCode)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readJson(filePath) {
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(readFileSync(filePath, 'utf8'))
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw new Error(`Failed to read JSON manifest at ${filePath}: ${error.message}`)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function importPath(fromFile, toFile) {
|
|
23
|
+
let value = relative(dirname(fromFile), toFile).replaceAll('\\', '/')
|
|
24
|
+
if (!value.startsWith('.')) value = `./${value}`
|
|
25
|
+
return value.replace(/\.[cm]?ts$/, '.js')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function generateTypeModule(manifestPath, outputPath) {
|
|
29
|
+
const manifest = readJson(manifestPath)
|
|
30
|
+
const sdkImport = '@shadowob/sdk'
|
|
31
|
+
const sourceImport = importPath(outputPath, manifestPath)
|
|
32
|
+
const contents = `// Generated by shadow-server-app typegen from ${sourceImport}.
|
|
33
|
+
// Do not edit by hand. Update the JSON manifest and regenerate this file.
|
|
34
|
+
import type { ShadowServerAppManifest } from '${sdkImport}'
|
|
35
|
+
|
|
36
|
+
export const shadowServerAppManifest = ${JSON.stringify(manifest, null, 2)} as const satisfies ShadowServerAppManifest
|
|
37
|
+
|
|
38
|
+
export type ShadowServerAppManifestDefinition = typeof shadowServerAppManifest
|
|
39
|
+
`
|
|
40
|
+
mkdirSync(dirname(outputPath), { recursive: true })
|
|
41
|
+
writeFileSync(outputPath, contents)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const [, , command, manifestArg, outputArg] = process.argv
|
|
45
|
+
|
|
46
|
+
if (!command || command === '--help' || command === '-h') usage(command ? 0 : 1)
|
|
47
|
+
|
|
48
|
+
if (command !== 'typegen' || !manifestArg || !outputArg) usage()
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const manifestPath = resolve(process.cwd(), manifestArg)
|
|
52
|
+
const outputPath = resolve(process.cwd(), outputArg)
|
|
53
|
+
generateTypeModule(manifestPath, outputPath)
|
|
54
|
+
console.log(`Generated ${relative(process.cwd(), outputPath)}`)
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error instanceof Error ? error.message : String(error))
|
|
57
|
+
process.exit(1)
|
|
58
|
+
}
|
package/dist/bridge.cjs
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/bridge.ts
|
|
21
|
+
var bridge_exports = {};
|
|
22
|
+
__export(bridge_exports, {
|
|
23
|
+
SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT: () => SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT,
|
|
24
|
+
SHADOW_SERVER_APP_COMMAND_EVENTS: () => SHADOW_SERVER_APP_COMMAND_EVENTS,
|
|
25
|
+
SHADOW_SERVER_APP_COMMAND_FAILED_EVENT: () => SHADOW_SERVER_APP_COMMAND_FAILED_EVENT,
|
|
26
|
+
ShadowBridge: () => ShadowBridge,
|
|
27
|
+
buildShadowServerAppInboxDelivery: () => buildShadowServerAppInboxDelivery,
|
|
28
|
+
buildShadowServerAppInboxTaskRequest: () => buildShadowServerAppInboxTaskRequest,
|
|
29
|
+
getShadowServerAppChannelMessageDeliveries: () => getShadowServerAppChannelMessageDeliveries,
|
|
30
|
+
getShadowServerAppChannelMessageErrors: () => getShadowServerAppChannelMessageErrors,
|
|
31
|
+
getShadowServerAppTaskCardId: () => getShadowServerAppTaskCardId,
|
|
32
|
+
shadowServerAppInboxTaskEndpoint: () => shadowServerAppInboxTaskEndpoint
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(bridge_exports);
|
|
35
|
+
|
|
36
|
+
// src/server-app.ts
|
|
37
|
+
var SHADOW_SERVER_APP_PROTOCOL = "shadow.app/1";
|
|
38
|
+
var SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT = "server_app.command.completed";
|
|
39
|
+
var SHADOW_SERVER_APP_COMMAND_FAILED_EVENT = "server_app.command.failed";
|
|
40
|
+
var SHADOW_SERVER_APP_COMMAND_EVENTS = [
|
|
41
|
+
SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT,
|
|
42
|
+
SHADOW_SERVER_APP_COMMAND_FAILED_EVENT
|
|
43
|
+
];
|
|
44
|
+
function isProtocolRecord(value) {
|
|
45
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
46
|
+
}
|
|
47
|
+
function optionalProtocolString(value) {
|
|
48
|
+
return typeof value === "string" && value ? value : void 0;
|
|
49
|
+
}
|
|
50
|
+
function protocolPathSegment(value) {
|
|
51
|
+
return encodeURIComponent(value);
|
|
52
|
+
}
|
|
53
|
+
function shadowServerAppInboxTaskEndpoint(serverIdOrSlug, target) {
|
|
54
|
+
if ("channelId" in target && target.channelId) {
|
|
55
|
+
return `/api/channels/${protocolPathSegment(target.channelId)}/inbox/tasks`;
|
|
56
|
+
}
|
|
57
|
+
if ("agentId" in target && target.agentId) {
|
|
58
|
+
return `/api/servers/${protocolPathSegment(serverIdOrSlug)}/inboxes/${protocolPathSegment(
|
|
59
|
+
target.agentId
|
|
60
|
+
)}/tasks`;
|
|
61
|
+
}
|
|
62
|
+
throw new Error("Missing Inbox task target");
|
|
63
|
+
}
|
|
64
|
+
function buildShadowServerAppInboxTaskRequest(input) {
|
|
65
|
+
const appId = input.app.id ?? input.app.appId ?? input.app.appKey;
|
|
66
|
+
const serverAppData = isProtocolRecord(input.task.data?.serverApp) ? input.task.data.serverApp : {};
|
|
67
|
+
return {
|
|
68
|
+
endpoint: shadowServerAppInboxTaskEndpoint(input.serverIdOrSlug, input.target),
|
|
69
|
+
body: {
|
|
70
|
+
title: input.task.title,
|
|
71
|
+
body: input.task.body,
|
|
72
|
+
priority: input.task.priority,
|
|
73
|
+
idempotencyKey: input.task.idempotencyKey,
|
|
74
|
+
source: {
|
|
75
|
+
kind: "server_app",
|
|
76
|
+
id: appId,
|
|
77
|
+
appId,
|
|
78
|
+
appKey: input.app.appKey,
|
|
79
|
+
...input.app.serverId ? { serverId: input.app.serverId } : {},
|
|
80
|
+
...input.commandName ? { command: input.commandName } : {},
|
|
81
|
+
label: input.app.label ?? input.app.name ?? input.app.appKey,
|
|
82
|
+
...input.task.resource ? { resource: input.task.resource } : {}
|
|
83
|
+
},
|
|
84
|
+
data: {
|
|
85
|
+
...input.task.data ?? {},
|
|
86
|
+
serverApp: {
|
|
87
|
+
...serverAppData,
|
|
88
|
+
appKey: input.app.appKey,
|
|
89
|
+
...input.commandName ? { command: input.commandName } : {}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function getShadowServerAppTaskCardId(message) {
|
|
96
|
+
const metadata = isProtocolRecord(message) ? message.metadata : null;
|
|
97
|
+
const cards = isProtocolRecord(metadata) && Array.isArray(metadata.cards) ? metadata.cards : [];
|
|
98
|
+
for (const item of cards) {
|
|
99
|
+
if (!isProtocolRecord(item)) continue;
|
|
100
|
+
if (item.kind === "task" && typeof item.id === "string" && item.id) return item.id;
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
function buildShadowServerAppInboxDelivery(input) {
|
|
105
|
+
const message = isProtocolRecord(input.message) ? input.message : {};
|
|
106
|
+
return {
|
|
107
|
+
..."agentId" in input.target && input.target.agentId ? { agentId: input.target.agentId } : {},
|
|
108
|
+
channelId: optionalProtocolString(message.channelId),
|
|
109
|
+
messageId: optionalProtocolString(message.id),
|
|
110
|
+
cardId: getShadowServerAppTaskCardId(message),
|
|
111
|
+
idempotencyKey: input.idempotencyKey
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function shadowFromPayload(payload) {
|
|
115
|
+
if (payload.protocol === SHADOW_SERVER_APP_PROTOCOL) {
|
|
116
|
+
return payload;
|
|
117
|
+
}
|
|
118
|
+
const shadow = isProtocolRecord(payload.shadow) ? payload.shadow : null;
|
|
119
|
+
if (shadow?.protocol === SHADOW_SERVER_APP_PROTOCOL) {
|
|
120
|
+
return shadow;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function mergeShadowResult(value, shadow) {
|
|
125
|
+
if (!shadow) return value;
|
|
126
|
+
const existing = shadowFromPayload(value);
|
|
127
|
+
if (!existing) return { ...value, shadow };
|
|
128
|
+
return {
|
|
129
|
+
...value,
|
|
130
|
+
shadow: {
|
|
131
|
+
protocol: SHADOW_SERVER_APP_PROTOCOL,
|
|
132
|
+
outbox: {
|
|
133
|
+
...existing.outbox ?? {},
|
|
134
|
+
...shadow.outbox ?? {}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function getShadowServerAppInboxDeliveries(payload) {
|
|
140
|
+
if (!isProtocolRecord(payload)) return [];
|
|
141
|
+
const shadow = shadowFromPayload(payload);
|
|
142
|
+
return shadow?.outbox?.deliveries ?? [];
|
|
143
|
+
}
|
|
144
|
+
function getShadowServerAppInboxErrors(payload) {
|
|
145
|
+
if (!isProtocolRecord(payload)) return [];
|
|
146
|
+
const shadow = shadowFromPayload(payload);
|
|
147
|
+
return shadow?.outbox?.errors ?? [];
|
|
148
|
+
}
|
|
149
|
+
function getShadowServerAppChannelMessageDeliveries(payload) {
|
|
150
|
+
if (!isProtocolRecord(payload)) return [];
|
|
151
|
+
const shadow = shadowFromPayload(payload);
|
|
152
|
+
return shadow?.outbox?.channelMessageDeliveries ?? [];
|
|
153
|
+
}
|
|
154
|
+
function getShadowServerAppChannelMessageErrors(payload) {
|
|
155
|
+
if (!isProtocolRecord(payload)) return [];
|
|
156
|
+
const shadow = shadowFromPayload(payload);
|
|
157
|
+
return shadow?.outbox?.channelMessageErrors ?? [];
|
|
158
|
+
}
|
|
159
|
+
function unwrapShadowServerAppCommandPayload(payload) {
|
|
160
|
+
if (isProtocolRecord(payload) && payload.ok === false) {
|
|
161
|
+
throw new Error(typeof payload.error === "string" ? payload.error : "Command failed");
|
|
162
|
+
}
|
|
163
|
+
if (isProtocolRecord(payload) && "result" in payload && payload.result !== void 0) {
|
|
164
|
+
const nested = unwrapShadowServerAppCommandPayload(payload.result);
|
|
165
|
+
const shadow = shadowFromPayload(payload);
|
|
166
|
+
if (isProtocolRecord(nested)) return mergeShadowResult(nested, shadow);
|
|
167
|
+
return nested;
|
|
168
|
+
}
|
|
169
|
+
return payload;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/bridge.ts
|
|
173
|
+
var ShadowBridge = class _ShadowBridge {
|
|
174
|
+
static commandRequestType = "shadow.app.command.request";
|
|
175
|
+
static commandResponseType = "shadow.app.command.response";
|
|
176
|
+
static inboxesRequestType = "shadow.app.inboxes.request";
|
|
177
|
+
static inboxesResponseType = "shadow.app.inboxes.response";
|
|
178
|
+
static enqueueInboxTaskRequestType = "shadow.app.inbox.enqueue.request";
|
|
179
|
+
static enqueueInboxTaskResponseType = "shadow.app.inbox.enqueue.response";
|
|
180
|
+
static openBuddyCreatorRequestType = "shadow.app.buddy.create.request";
|
|
181
|
+
static openBuddyCreatorResponseType = "shadow.app.buddy.create.response";
|
|
182
|
+
static inboxDeliveries(payload) {
|
|
183
|
+
return getShadowServerAppInboxDeliveries(payload);
|
|
184
|
+
}
|
|
185
|
+
static inboxErrors(payload) {
|
|
186
|
+
return getShadowServerAppInboxErrors(payload);
|
|
187
|
+
}
|
|
188
|
+
static channelMessageDeliveries(payload) {
|
|
189
|
+
return getShadowServerAppChannelMessageDeliveries(payload);
|
|
190
|
+
}
|
|
191
|
+
static channelMessageErrors(payload) {
|
|
192
|
+
return getShadowServerAppChannelMessageErrors(payload);
|
|
193
|
+
}
|
|
194
|
+
static unwrapCommandPayload(payload) {
|
|
195
|
+
return unwrapShadowServerAppCommandPayload(payload);
|
|
196
|
+
}
|
|
197
|
+
appKey;
|
|
198
|
+
targetOrigin;
|
|
199
|
+
timeoutMs;
|
|
200
|
+
win;
|
|
201
|
+
hasLaunchContext;
|
|
202
|
+
pending = /* @__PURE__ */ new Map();
|
|
203
|
+
onMessage = (event) => {
|
|
204
|
+
let data = event.data;
|
|
205
|
+
if (typeof data === "string") {
|
|
206
|
+
try {
|
|
207
|
+
data = JSON.parse(data || "{}");
|
|
208
|
+
} catch {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!data || typeof data !== "object") return;
|
|
213
|
+
const record = data;
|
|
214
|
+
if (typeof record.requestId !== "string" || typeof record.type !== "string") return;
|
|
215
|
+
const entry = this.pending.get(record.requestId);
|
|
216
|
+
if (!entry || record.type !== entry.responseType) return;
|
|
217
|
+
this.pending.delete(record.requestId);
|
|
218
|
+
if (record.ok) entry.resolve(record.result);
|
|
219
|
+
else
|
|
220
|
+
entry.reject(
|
|
221
|
+
new Error(typeof record.error === "string" ? record.error : "Bridge request failed")
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
constructor(options) {
|
|
225
|
+
this.appKey = options.appKey;
|
|
226
|
+
this.targetOrigin = options.targetOrigin ?? "*";
|
|
227
|
+
this.timeoutMs = options.timeoutMs ?? 6e4;
|
|
228
|
+
this.win = options.windowRef ?? (typeof window === "undefined" ? null : window);
|
|
229
|
+
this.hasLaunchContext = this.resolveLaunchContext();
|
|
230
|
+
this.win?.addEventListener("message", this.onMessage);
|
|
231
|
+
}
|
|
232
|
+
dispose() {
|
|
233
|
+
this.win?.removeEventListener("message", this.onMessage);
|
|
234
|
+
for (const entry of this.pending.values()) {
|
|
235
|
+
entry.reject(new Error("ShadowBridge disposed"));
|
|
236
|
+
}
|
|
237
|
+
this.pending.clear();
|
|
238
|
+
}
|
|
239
|
+
isAvailable() {
|
|
240
|
+
if (!this.win) return false;
|
|
241
|
+
return this.hasLaunchContext && (this.win.parent !== this.win || !!this.win.ReactNativeWebView);
|
|
242
|
+
}
|
|
243
|
+
command(commandName, input, options = {}) {
|
|
244
|
+
return this.request(
|
|
245
|
+
_ShadowBridge.commandRequestType,
|
|
246
|
+
_ShadowBridge.commandResponseType,
|
|
247
|
+
{
|
|
248
|
+
commandName,
|
|
249
|
+
input,
|
|
250
|
+
...options.channelId ? { channelId: options.channelId } : {},
|
|
251
|
+
...options.task ? { task: options.task } : {}
|
|
252
|
+
},
|
|
253
|
+
options.timeoutMs
|
|
254
|
+
).then(
|
|
255
|
+
(payload) => unwrapShadowServerAppCommandPayload(payload)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
inboxes(options = {}) {
|
|
259
|
+
return this.request(
|
|
260
|
+
_ShadowBridge.inboxesRequestType,
|
|
261
|
+
_ShadowBridge.inboxesResponseType,
|
|
262
|
+
{},
|
|
263
|
+
options.timeoutMs ?? 15e3
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
enqueueInboxTask(input, options = {}) {
|
|
267
|
+
return this.request(
|
|
268
|
+
_ShadowBridge.enqueueInboxTaskRequestType,
|
|
269
|
+
_ShadowBridge.enqueueInboxTaskResponseType,
|
|
270
|
+
input,
|
|
271
|
+
options.timeoutMs
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
openBuddyCreator(input = {}, options = {}) {
|
|
275
|
+
return this.request(
|
|
276
|
+
_ShadowBridge.openBuddyCreatorRequestType,
|
|
277
|
+
_ShadowBridge.openBuddyCreatorResponseType,
|
|
278
|
+
input,
|
|
279
|
+
options.timeoutMs ?? 10 * 60 * 1e3
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
unwrapCommandPayload(payload) {
|
|
283
|
+
return unwrapShadowServerAppCommandPayload(payload);
|
|
284
|
+
}
|
|
285
|
+
inboxDeliveries(payload) {
|
|
286
|
+
return getShadowServerAppInboxDeliveries(payload);
|
|
287
|
+
}
|
|
288
|
+
inboxErrors(payload) {
|
|
289
|
+
return getShadowServerAppInboxErrors(payload);
|
|
290
|
+
}
|
|
291
|
+
channelMessageDeliveries(payload) {
|
|
292
|
+
return getShadowServerAppChannelMessageDeliveries(payload);
|
|
293
|
+
}
|
|
294
|
+
channelMessageErrors(payload) {
|
|
295
|
+
return getShadowServerAppChannelMessageErrors(payload);
|
|
296
|
+
}
|
|
297
|
+
request(requestType, responseType, payload, timeoutMs = this.timeoutMs) {
|
|
298
|
+
if (!this.isAvailable()) {
|
|
299
|
+
return Promise.reject(
|
|
300
|
+
new Error("ShadowBridge is not available outside a Shadow launch frame")
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
const requestId = `req_${Math.random().toString(36).slice(2)}`;
|
|
304
|
+
this.postMessage({
|
|
305
|
+
type: requestType,
|
|
306
|
+
requestId,
|
|
307
|
+
appKey: this.appKey,
|
|
308
|
+
...payload
|
|
309
|
+
});
|
|
310
|
+
return new Promise((resolve, reject) => {
|
|
311
|
+
this.pending.set(requestId, {
|
|
312
|
+
responseType,
|
|
313
|
+
resolve,
|
|
314
|
+
reject
|
|
315
|
+
});
|
|
316
|
+
this.win?.setTimeout(() => {
|
|
317
|
+
if (!this.pending.has(requestId)) return;
|
|
318
|
+
this.pending.delete(requestId);
|
|
319
|
+
reject(new Error("Bridge request timed out"));
|
|
320
|
+
}, timeoutMs);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
postMessage(message) {
|
|
324
|
+
if (!this.win) return;
|
|
325
|
+
if (this.win.ReactNativeWebView) {
|
|
326
|
+
this.win.ReactNativeWebView.postMessage(JSON.stringify(message));
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
this.win.parent.postMessage(message, this.targetOrigin);
|
|
330
|
+
}
|
|
331
|
+
resolveLaunchContext() {
|
|
332
|
+
if (!this.win) return false;
|
|
333
|
+
const storageKey = `shadow.bridge.launch:${this.appKey}`;
|
|
334
|
+
const memoryContexts = this.win.__shadowBridgeLaunchContexts ??= {};
|
|
335
|
+
const hasLaunchToken = new URLSearchParams(this.win.location.search).has("shadow_launch");
|
|
336
|
+
if (hasLaunchToken) {
|
|
337
|
+
memoryContexts[this.appKey] = true;
|
|
338
|
+
try {
|
|
339
|
+
this.win.sessionStorage?.setItem(storageKey, "1");
|
|
340
|
+
} catch {
|
|
341
|
+
}
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
if (memoryContexts[this.appKey]) return true;
|
|
345
|
+
try {
|
|
346
|
+
return this.win.sessionStorage?.getItem(storageKey) === "1";
|
|
347
|
+
} catch {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
353
|
+
0 && (module.exports = {
|
|
354
|
+
SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT,
|
|
355
|
+
SHADOW_SERVER_APP_COMMAND_EVENTS,
|
|
356
|
+
SHADOW_SERVER_APP_COMMAND_FAILED_EVENT,
|
|
357
|
+
ShadowBridge,
|
|
358
|
+
buildShadowServerAppInboxDelivery,
|
|
359
|
+
buildShadowServerAppInboxTaskRequest,
|
|
360
|
+
getShadowServerAppChannelMessageDeliveries,
|
|
361
|
+
getShadowServerAppChannelMessageErrors,
|
|
362
|
+
getShadowServerAppTaskCardId,
|
|
363
|
+
shadowServerAppInboxTaskEndpoint
|
|
364
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { c9 as ShadowServerAppInboxDelivery, ca as ShadowServerAppInboxDeliveryError, bK as ShadowServerAppChannelMessageDelivery, bL as ShadowServerAppChannelMessageDeliveryError, cc as ShadowServerAppInboxTarget, cd as ShadowServerAppInboxTaskOutbox } from './server-app-qqsBtGmE.cjs';
|
|
2
|
+
export { b7 as SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT, b8 as SHADOW_SERVER_APP_COMMAND_EVENTS, b9 as SHADOW_SERVER_APP_COMMAND_FAILED_EVENT, bM as ShadowServerAppChannelMessageOutbox, bS as ShadowServerAppCommandEventType, c7 as ShadowServerAppHostAppRef, c8 as ShadowServerAppHostInboxTaskRequestInput, cb as ShadowServerAppInboxDeliveryFromMessageInput, cl as ShadowServerAppResolvedInboxTaskRequest, cm as ShadowServerAppResultShadow, cx as buildShadowServerAppInboxDelivery, cy as buildShadowServerAppInboxTaskRequest, cC as getShadowServerAppChannelMessageDeliveries, cD as getShadowServerAppChannelMessageErrors, cE as getShadowServerAppTaskCardId, cM as shadowServerAppInboxTaskEndpoint } from './server-app-qqsBtGmE.cjs';
|
|
3
|
+
import '@shadowob/shared';
|
|
4
|
+
|
|
5
|
+
type ShadowBridgeCommandSpec<TInput = unknown, TResult = unknown> = {
|
|
6
|
+
input: TInput;
|
|
7
|
+
result: TResult;
|
|
8
|
+
};
|
|
9
|
+
type ShadowBridgeCommandMap = Record<string, ShadowBridgeCommandSpec>;
|
|
10
|
+
type BridgeCommandInput<TCommand> = TCommand extends ShadowBridgeCommandSpec<infer TInput, unknown> ? TInput : unknown;
|
|
11
|
+
type BridgeCommandResult<TCommand> = TCommand extends ShadowBridgeCommandSpec<unknown, infer TResult> ? TResult : unknown;
|
|
12
|
+
interface ShadowBridgeBuddyInbox {
|
|
13
|
+
agent: {
|
|
14
|
+
id: string;
|
|
15
|
+
ownerId?: string | null;
|
|
16
|
+
status?: string | null;
|
|
17
|
+
user?: {
|
|
18
|
+
id?: string | null;
|
|
19
|
+
username?: string | null;
|
|
20
|
+
displayName?: string | null;
|
|
21
|
+
avatarUrl?: string | null;
|
|
22
|
+
isBot?: boolean | null;
|
|
23
|
+
} | null;
|
|
24
|
+
};
|
|
25
|
+
channel?: {
|
|
26
|
+
id?: string | null;
|
|
27
|
+
name?: string | null;
|
|
28
|
+
} | null;
|
|
29
|
+
canManage?: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface ShadowBridgeEnqueueInboxTaskInput {
|
|
32
|
+
target: ShadowServerAppInboxTarget;
|
|
33
|
+
task: ShadowServerAppInboxTaskOutbox;
|
|
34
|
+
}
|
|
35
|
+
interface ShadowBridgeOpenBuddyCreatorInput {
|
|
36
|
+
landing?: {
|
|
37
|
+
title?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
source?: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface ShadowBridgeOptions {
|
|
43
|
+
appKey: string;
|
|
44
|
+
targetOrigin?: string;
|
|
45
|
+
timeoutMs?: number;
|
|
46
|
+
windowRef?: Window;
|
|
47
|
+
}
|
|
48
|
+
declare class ShadowBridge<TCommands extends ShadowBridgeCommandMap = ShadowBridgeCommandMap> {
|
|
49
|
+
static readonly commandRequestType = "shadow.app.command.request";
|
|
50
|
+
static readonly commandResponseType = "shadow.app.command.response";
|
|
51
|
+
static readonly inboxesRequestType = "shadow.app.inboxes.request";
|
|
52
|
+
static readonly inboxesResponseType = "shadow.app.inboxes.response";
|
|
53
|
+
static readonly enqueueInboxTaskRequestType = "shadow.app.inbox.enqueue.request";
|
|
54
|
+
static readonly enqueueInboxTaskResponseType = "shadow.app.inbox.enqueue.response";
|
|
55
|
+
static readonly openBuddyCreatorRequestType = "shadow.app.buddy.create.request";
|
|
56
|
+
static readonly openBuddyCreatorResponseType = "shadow.app.buddy.create.response";
|
|
57
|
+
static inboxDeliveries(payload: unknown): ShadowServerAppInboxDelivery[];
|
|
58
|
+
static inboxErrors(payload: unknown): ShadowServerAppInboxDeliveryError[];
|
|
59
|
+
static channelMessageDeliveries(payload: unknown): ShadowServerAppChannelMessageDelivery[];
|
|
60
|
+
static channelMessageErrors(payload: unknown): ShadowServerAppChannelMessageDeliveryError[];
|
|
61
|
+
static unwrapCommandPayload<TResult = unknown>(payload: unknown): TResult;
|
|
62
|
+
private readonly appKey;
|
|
63
|
+
private readonly targetOrigin;
|
|
64
|
+
private readonly timeoutMs;
|
|
65
|
+
private readonly win;
|
|
66
|
+
private readonly hasLaunchContext;
|
|
67
|
+
private readonly pending;
|
|
68
|
+
private readonly onMessage;
|
|
69
|
+
constructor(options: ShadowBridgeOptions);
|
|
70
|
+
dispose(): void;
|
|
71
|
+
isAvailable(): boolean;
|
|
72
|
+
command<TCommandName extends Extract<keyof TCommands, string>>(commandName: TCommandName, input: BridgeCommandInput<TCommands[TCommandName]>, options?: {
|
|
73
|
+
channelId?: string;
|
|
74
|
+
timeoutMs?: number;
|
|
75
|
+
task?: {
|
|
76
|
+
messageId: string;
|
|
77
|
+
cardId: string;
|
|
78
|
+
claimId?: string;
|
|
79
|
+
};
|
|
80
|
+
}): Promise<BridgeCommandResult<TCommands[TCommandName]>>;
|
|
81
|
+
inboxes(options?: {
|
|
82
|
+
timeoutMs?: number;
|
|
83
|
+
}): Promise<{
|
|
84
|
+
inboxes: ShadowBridgeBuddyInbox[];
|
|
85
|
+
}>;
|
|
86
|
+
enqueueInboxTask(input: ShadowBridgeEnqueueInboxTaskInput, options?: {
|
|
87
|
+
timeoutMs?: number;
|
|
88
|
+
}): Promise<ShadowServerAppInboxDelivery>;
|
|
89
|
+
openBuddyCreator(input?: ShadowBridgeOpenBuddyCreatorInput, options?: {
|
|
90
|
+
timeoutMs?: number;
|
|
91
|
+
}): Promise<{
|
|
92
|
+
opened: boolean;
|
|
93
|
+
agent?: unknown;
|
|
94
|
+
}>;
|
|
95
|
+
unwrapCommandPayload<TResult = unknown>(payload: unknown): TResult;
|
|
96
|
+
inboxDeliveries(payload: unknown): ShadowServerAppInboxDelivery[];
|
|
97
|
+
inboxErrors(payload: unknown): ShadowServerAppInboxDeliveryError[];
|
|
98
|
+
channelMessageDeliveries(payload: unknown): ShadowServerAppChannelMessageDelivery[];
|
|
99
|
+
channelMessageErrors(payload: unknown): ShadowServerAppChannelMessageDeliveryError[];
|
|
100
|
+
private request;
|
|
101
|
+
private postMessage;
|
|
102
|
+
private resolveLaunchContext;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { ShadowBridge, type ShadowBridgeBuddyInbox, type ShadowBridgeCommandMap, type ShadowBridgeCommandSpec, type ShadowBridgeEnqueueInboxTaskInput, type ShadowBridgeOpenBuddyCreatorInput, type ShadowBridgeOptions, ShadowServerAppChannelMessageDelivery, ShadowServerAppChannelMessageDeliveryError, ShadowServerAppInboxDelivery, ShadowServerAppInboxDeliveryError, ShadowServerAppInboxTarget, ShadowServerAppInboxTaskOutbox };
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { c9 as ShadowServerAppInboxDelivery, ca as ShadowServerAppInboxDeliveryError, bK as ShadowServerAppChannelMessageDelivery, bL as ShadowServerAppChannelMessageDeliveryError, cc as ShadowServerAppInboxTarget, cd as ShadowServerAppInboxTaskOutbox } from './server-app-qqsBtGmE.js';
|
|
2
|
+
export { b7 as SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT, b8 as SHADOW_SERVER_APP_COMMAND_EVENTS, b9 as SHADOW_SERVER_APP_COMMAND_FAILED_EVENT, bM as ShadowServerAppChannelMessageOutbox, bS as ShadowServerAppCommandEventType, c7 as ShadowServerAppHostAppRef, c8 as ShadowServerAppHostInboxTaskRequestInput, cb as ShadowServerAppInboxDeliveryFromMessageInput, cl as ShadowServerAppResolvedInboxTaskRequest, cm as ShadowServerAppResultShadow, cx as buildShadowServerAppInboxDelivery, cy as buildShadowServerAppInboxTaskRequest, cC as getShadowServerAppChannelMessageDeliveries, cD as getShadowServerAppChannelMessageErrors, cE as getShadowServerAppTaskCardId, cM as shadowServerAppInboxTaskEndpoint } from './server-app-qqsBtGmE.js';
|
|
3
|
+
import '@shadowob/shared';
|
|
4
|
+
|
|
5
|
+
type ShadowBridgeCommandSpec<TInput = unknown, TResult = unknown> = {
|
|
6
|
+
input: TInput;
|
|
7
|
+
result: TResult;
|
|
8
|
+
};
|
|
9
|
+
type ShadowBridgeCommandMap = Record<string, ShadowBridgeCommandSpec>;
|
|
10
|
+
type BridgeCommandInput<TCommand> = TCommand extends ShadowBridgeCommandSpec<infer TInput, unknown> ? TInput : unknown;
|
|
11
|
+
type BridgeCommandResult<TCommand> = TCommand extends ShadowBridgeCommandSpec<unknown, infer TResult> ? TResult : unknown;
|
|
12
|
+
interface ShadowBridgeBuddyInbox {
|
|
13
|
+
agent: {
|
|
14
|
+
id: string;
|
|
15
|
+
ownerId?: string | null;
|
|
16
|
+
status?: string | null;
|
|
17
|
+
user?: {
|
|
18
|
+
id?: string | null;
|
|
19
|
+
username?: string | null;
|
|
20
|
+
displayName?: string | null;
|
|
21
|
+
avatarUrl?: string | null;
|
|
22
|
+
isBot?: boolean | null;
|
|
23
|
+
} | null;
|
|
24
|
+
};
|
|
25
|
+
channel?: {
|
|
26
|
+
id?: string | null;
|
|
27
|
+
name?: string | null;
|
|
28
|
+
} | null;
|
|
29
|
+
canManage?: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface ShadowBridgeEnqueueInboxTaskInput {
|
|
32
|
+
target: ShadowServerAppInboxTarget;
|
|
33
|
+
task: ShadowServerAppInboxTaskOutbox;
|
|
34
|
+
}
|
|
35
|
+
interface ShadowBridgeOpenBuddyCreatorInput {
|
|
36
|
+
landing?: {
|
|
37
|
+
title?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
source?: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface ShadowBridgeOptions {
|
|
43
|
+
appKey: string;
|
|
44
|
+
targetOrigin?: string;
|
|
45
|
+
timeoutMs?: number;
|
|
46
|
+
windowRef?: Window;
|
|
47
|
+
}
|
|
48
|
+
declare class ShadowBridge<TCommands extends ShadowBridgeCommandMap = ShadowBridgeCommandMap> {
|
|
49
|
+
static readonly commandRequestType = "shadow.app.command.request";
|
|
50
|
+
static readonly commandResponseType = "shadow.app.command.response";
|
|
51
|
+
static readonly inboxesRequestType = "shadow.app.inboxes.request";
|
|
52
|
+
static readonly inboxesResponseType = "shadow.app.inboxes.response";
|
|
53
|
+
static readonly enqueueInboxTaskRequestType = "shadow.app.inbox.enqueue.request";
|
|
54
|
+
static readonly enqueueInboxTaskResponseType = "shadow.app.inbox.enqueue.response";
|
|
55
|
+
static readonly openBuddyCreatorRequestType = "shadow.app.buddy.create.request";
|
|
56
|
+
static readonly openBuddyCreatorResponseType = "shadow.app.buddy.create.response";
|
|
57
|
+
static inboxDeliveries(payload: unknown): ShadowServerAppInboxDelivery[];
|
|
58
|
+
static inboxErrors(payload: unknown): ShadowServerAppInboxDeliveryError[];
|
|
59
|
+
static channelMessageDeliveries(payload: unknown): ShadowServerAppChannelMessageDelivery[];
|
|
60
|
+
static channelMessageErrors(payload: unknown): ShadowServerAppChannelMessageDeliveryError[];
|
|
61
|
+
static unwrapCommandPayload<TResult = unknown>(payload: unknown): TResult;
|
|
62
|
+
private readonly appKey;
|
|
63
|
+
private readonly targetOrigin;
|
|
64
|
+
private readonly timeoutMs;
|
|
65
|
+
private readonly win;
|
|
66
|
+
private readonly hasLaunchContext;
|
|
67
|
+
private readonly pending;
|
|
68
|
+
private readonly onMessage;
|
|
69
|
+
constructor(options: ShadowBridgeOptions);
|
|
70
|
+
dispose(): void;
|
|
71
|
+
isAvailable(): boolean;
|
|
72
|
+
command<TCommandName extends Extract<keyof TCommands, string>>(commandName: TCommandName, input: BridgeCommandInput<TCommands[TCommandName]>, options?: {
|
|
73
|
+
channelId?: string;
|
|
74
|
+
timeoutMs?: number;
|
|
75
|
+
task?: {
|
|
76
|
+
messageId: string;
|
|
77
|
+
cardId: string;
|
|
78
|
+
claimId?: string;
|
|
79
|
+
};
|
|
80
|
+
}): Promise<BridgeCommandResult<TCommands[TCommandName]>>;
|
|
81
|
+
inboxes(options?: {
|
|
82
|
+
timeoutMs?: number;
|
|
83
|
+
}): Promise<{
|
|
84
|
+
inboxes: ShadowBridgeBuddyInbox[];
|
|
85
|
+
}>;
|
|
86
|
+
enqueueInboxTask(input: ShadowBridgeEnqueueInboxTaskInput, options?: {
|
|
87
|
+
timeoutMs?: number;
|
|
88
|
+
}): Promise<ShadowServerAppInboxDelivery>;
|
|
89
|
+
openBuddyCreator(input?: ShadowBridgeOpenBuddyCreatorInput, options?: {
|
|
90
|
+
timeoutMs?: number;
|
|
91
|
+
}): Promise<{
|
|
92
|
+
opened: boolean;
|
|
93
|
+
agent?: unknown;
|
|
94
|
+
}>;
|
|
95
|
+
unwrapCommandPayload<TResult = unknown>(payload: unknown): TResult;
|
|
96
|
+
inboxDeliveries(payload: unknown): ShadowServerAppInboxDelivery[];
|
|
97
|
+
inboxErrors(payload: unknown): ShadowServerAppInboxDeliveryError[];
|
|
98
|
+
channelMessageDeliveries(payload: unknown): ShadowServerAppChannelMessageDelivery[];
|
|
99
|
+
channelMessageErrors(payload: unknown): ShadowServerAppChannelMessageDeliveryError[];
|
|
100
|
+
private request;
|
|
101
|
+
private postMessage;
|
|
102
|
+
private resolveLaunchContext;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { ShadowBridge, type ShadowBridgeBuddyInbox, type ShadowBridgeCommandMap, type ShadowBridgeCommandSpec, type ShadowBridgeEnqueueInboxTaskInput, type ShadowBridgeOpenBuddyCreatorInput, type ShadowBridgeOptions, ShadowServerAppChannelMessageDelivery, ShadowServerAppChannelMessageDeliveryError, ShadowServerAppInboxDelivery, ShadowServerAppInboxDeliveryError, ShadowServerAppInboxTarget, ShadowServerAppInboxTaskOutbox };
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ShadowBridge
|
|
3
|
+
} from "./chunk-5QQ7ZR6B.js";
|
|
4
|
+
import {
|
|
5
|
+
SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT,
|
|
6
|
+
SHADOW_SERVER_APP_COMMAND_EVENTS,
|
|
7
|
+
SHADOW_SERVER_APP_COMMAND_FAILED_EVENT,
|
|
8
|
+
buildShadowServerAppInboxDelivery,
|
|
9
|
+
buildShadowServerAppInboxTaskRequest,
|
|
10
|
+
getShadowServerAppChannelMessageDeliveries,
|
|
11
|
+
getShadowServerAppChannelMessageErrors,
|
|
12
|
+
getShadowServerAppTaskCardId,
|
|
13
|
+
shadowServerAppInboxTaskEndpoint
|
|
14
|
+
} from "./chunk-OMTGSMTQ.js";
|
|
15
|
+
export {
|
|
16
|
+
SHADOW_SERVER_APP_COMMAND_COMPLETED_EVENT,
|
|
17
|
+
SHADOW_SERVER_APP_COMMAND_EVENTS,
|
|
18
|
+
SHADOW_SERVER_APP_COMMAND_FAILED_EVENT,
|
|
19
|
+
ShadowBridge,
|
|
20
|
+
buildShadowServerAppInboxDelivery,
|
|
21
|
+
buildShadowServerAppInboxTaskRequest,
|
|
22
|
+
getShadowServerAppChannelMessageDeliveries,
|
|
23
|
+
getShadowServerAppChannelMessageErrors,
|
|
24
|
+
getShadowServerAppTaskCardId,
|
|
25
|
+
shadowServerAppInboxTaskEndpoint
|
|
26
|
+
};
|