@creature-ai/sdk 0.1.1 → 0.1.3
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/dist/core/index.d.ts +314 -72
- package/dist/core/index.js +250 -267
- package/dist/core/index.js.map +1 -1
- package/dist/react/index.d.ts +61 -36
- package/dist/react/index.js +356 -305
- package/dist/react/index.js.map +1 -1
- package/dist/server/index.d.ts +187 -218
- package/dist/server/index.js +567 -521
- package/dist/server/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/types-DcoqlK46.d.ts +0 -163
package/dist/core/index.js
CHANGED
|
@@ -9325,8 +9325,11 @@ var Subscribable = class {
|
|
|
9325
9325
|
}
|
|
9326
9326
|
};
|
|
9327
9327
|
|
|
9328
|
-
// src/core/
|
|
9329
|
-
var
|
|
9328
|
+
// src/core/McpAppHostClient.ts
|
|
9329
|
+
var McpAppHostClient = class extends Subscribable {
|
|
9330
|
+
// ============================================================================
|
|
9331
|
+
// Private Properties
|
|
9332
|
+
// ============================================================================
|
|
9330
9333
|
state = {
|
|
9331
9334
|
isReady: false,
|
|
9332
9335
|
environment: "mcp-apps",
|
|
@@ -9335,18 +9338,22 @@ var McpHostClient = class extends Subscribable {
|
|
|
9335
9338
|
config;
|
|
9336
9339
|
app = null;
|
|
9337
9340
|
connected = false;
|
|
9341
|
+
// ============================================================================
|
|
9342
|
+
// Constructor
|
|
9343
|
+
// ============================================================================
|
|
9338
9344
|
constructor(config) {
|
|
9339
9345
|
super();
|
|
9340
9346
|
this.config = config;
|
|
9341
9347
|
}
|
|
9348
|
+
// ============================================================================
|
|
9349
|
+
// Public API
|
|
9350
|
+
// ============================================================================
|
|
9351
|
+
/**
|
|
9352
|
+
* Get the current client state.
|
|
9353
|
+
*/
|
|
9342
9354
|
getState() {
|
|
9343
9355
|
return this.state;
|
|
9344
9356
|
}
|
|
9345
|
-
setState(partial) {
|
|
9346
|
-
const prev = this.state;
|
|
9347
|
-
this.state = { ...this.state, ...partial };
|
|
9348
|
-
this.notifyStateChange(this.state, prev);
|
|
9349
|
-
}
|
|
9350
9357
|
/**
|
|
9351
9358
|
* Connect to the MCP Apps host.
|
|
9352
9359
|
*
|
|
@@ -9368,90 +9375,6 @@ var McpHostClient = class extends Subscribable {
|
|
|
9368
9375
|
this.setupHandlers();
|
|
9369
9376
|
this.initiateConnection();
|
|
9370
9377
|
}
|
|
9371
|
-
/**
|
|
9372
|
-
* Set up notification handlers on the App instance.
|
|
9373
|
-
*
|
|
9374
|
-
* Maps the official SDK's callback pattern to our event emitter pattern,
|
|
9375
|
-
* allowing consumers to use `.on("tool-result", ...)` etc.
|
|
9376
|
-
*/
|
|
9377
|
-
setupHandlers() {
|
|
9378
|
-
if (!this.app) return;
|
|
9379
|
-
this.app.ontoolinput = (params) => {
|
|
9380
|
-
console.debug(`[${this.config.name}] Received tool-input`, { args: params.arguments });
|
|
9381
|
-
this.emit("tool-input", params.arguments || {});
|
|
9382
|
-
};
|
|
9383
|
-
this.app.ontoolresult = (params) => {
|
|
9384
|
-
console.log(`[McpHostClient] ontoolresult called`, {
|
|
9385
|
-
isError: params.isError,
|
|
9386
|
-
source: params.source,
|
|
9387
|
-
hasContent: !!params.content,
|
|
9388
|
-
hasStructuredContent: !!params.structuredContent,
|
|
9389
|
-
structuredContent: params.structuredContent
|
|
9390
|
-
});
|
|
9391
|
-
const textContent = params.content?.filter((item) => item.type === "text").map((item) => ({ type: item.type, text: item.text }));
|
|
9392
|
-
const result = {
|
|
9393
|
-
content: textContent,
|
|
9394
|
-
structuredContent: params.structuredContent,
|
|
9395
|
-
isError: params.isError,
|
|
9396
|
-
source: params.source
|
|
9397
|
-
};
|
|
9398
|
-
console.log(`[McpHostClient] Emitting tool-result event`, result);
|
|
9399
|
-
this.emit("tool-result", result);
|
|
9400
|
-
};
|
|
9401
|
-
this.app.onhostcontextchanged = (params) => {
|
|
9402
|
-
console.debug(`[${this.config.name}] Host context changed`, { theme: params.theme });
|
|
9403
|
-
if (params.theme) {
|
|
9404
|
-
YU(params.theme);
|
|
9405
|
-
this.emit("theme-change", params.theme);
|
|
9406
|
-
}
|
|
9407
|
-
if (params.styles?.variables) {
|
|
9408
|
-
QU(params.styles.variables);
|
|
9409
|
-
}
|
|
9410
|
-
if (params.styles?.css?.fonts) {
|
|
9411
|
-
qU(params.styles.css.fonts);
|
|
9412
|
-
}
|
|
9413
|
-
};
|
|
9414
|
-
this.app.onteardown = async (_params, _extra) => {
|
|
9415
|
-
console.debug(`[${this.config.name}] Teardown requested`);
|
|
9416
|
-
await this.emit("teardown");
|
|
9417
|
-
return {};
|
|
9418
|
-
};
|
|
9419
|
-
}
|
|
9420
|
-
/**
|
|
9421
|
-
* Initiate connection using PostMessageTransport.
|
|
9422
|
-
*
|
|
9423
|
-
* The SDK's App.connect() handles the protocol handshake correctly:
|
|
9424
|
-
* the guest (App) sends `ui/initialize` to the host.
|
|
9425
|
-
*/
|
|
9426
|
-
async initiateConnection() {
|
|
9427
|
-
if (!this.app) {
|
|
9428
|
-
return;
|
|
9429
|
-
}
|
|
9430
|
-
try {
|
|
9431
|
-
const transport = new Yn(window.parent, window.parent);
|
|
9432
|
-
await this.app.connect(transport);
|
|
9433
|
-
const hostContext = this.app.getHostContext();
|
|
9434
|
-
console.debug(`[${this.config.name}] Connected to host`, { theme: hostContext?.theme });
|
|
9435
|
-
if (hostContext?.theme) {
|
|
9436
|
-
YU(hostContext.theme);
|
|
9437
|
-
this.emit("theme-change", hostContext.theme);
|
|
9438
|
-
}
|
|
9439
|
-
if (hostContext?.styles?.variables) {
|
|
9440
|
-
QU(hostContext.styles.variables);
|
|
9441
|
-
}
|
|
9442
|
-
if (hostContext?.styles?.css?.fonts) {
|
|
9443
|
-
qU(hostContext.styles.css.fonts);
|
|
9444
|
-
}
|
|
9445
|
-
const widgetState = hostContext?.widgetState;
|
|
9446
|
-
if (widgetState) {
|
|
9447
|
-
this.setState({ widgetState });
|
|
9448
|
-
this.emit("widget-state-change", widgetState);
|
|
9449
|
-
}
|
|
9450
|
-
this.setState({ isReady: true });
|
|
9451
|
-
} catch (error) {
|
|
9452
|
-
console.error(`[${this.config.name}] Connection failed`, { error });
|
|
9453
|
-
}
|
|
9454
|
-
}
|
|
9455
9378
|
/**
|
|
9456
9379
|
* Disconnect from the host.
|
|
9457
9380
|
*
|
|
@@ -9484,9 +9407,8 @@ var McpHostClient = class extends Subscribable {
|
|
|
9484
9407
|
name: toolName,
|
|
9485
9408
|
arguments: args
|
|
9486
9409
|
});
|
|
9487
|
-
const textContent = sdkResult.content?.filter((item) => item.type === "text").map((item) => ({ type: item.type, text: item.text }));
|
|
9488
9410
|
const result = {
|
|
9489
|
-
content:
|
|
9411
|
+
content: this.extractTextContent(sdkResult.content),
|
|
9490
9412
|
structuredContent: sdkResult.structuredContent,
|
|
9491
9413
|
isError: sdkResult.isError
|
|
9492
9414
|
};
|
|
@@ -9527,6 +9449,13 @@ var McpHostClient = class extends Subscribable {
|
|
|
9527
9449
|
widgetState: state
|
|
9528
9450
|
});
|
|
9529
9451
|
}
|
|
9452
|
+
async requestDisplayMode(params) {
|
|
9453
|
+
if (!this.app) {
|
|
9454
|
+
return { mode: params.mode };
|
|
9455
|
+
}
|
|
9456
|
+
const result = await this.app.requestDisplayMode({ mode: params.mode });
|
|
9457
|
+
return { mode: result.mode };
|
|
9458
|
+
}
|
|
9530
9459
|
/**
|
|
9531
9460
|
* Send a log message to the host's DevConsole.
|
|
9532
9461
|
*
|
|
@@ -9534,8 +9463,6 @@ var McpHostClient = class extends Subscribable {
|
|
|
9534
9463
|
* to the host. Logs appear in the unified DevConsole alongside server logs,
|
|
9535
9464
|
* with appropriate color coding based on level.
|
|
9536
9465
|
*
|
|
9537
|
-
* The logger name is automatically set to the app name from config.
|
|
9538
|
-
*
|
|
9539
9466
|
* @param level - Log severity level (debug, info, notice, warning, error)
|
|
9540
9467
|
* @param message - Log message
|
|
9541
9468
|
* @param data - Optional structured data to include with the log
|
|
@@ -9552,10 +9479,113 @@ var McpHostClient = class extends Subscribable {
|
|
|
9552
9479
|
data: data ? { message, ...data } : message
|
|
9553
9480
|
});
|
|
9554
9481
|
}
|
|
9482
|
+
// ============================================================================
|
|
9483
|
+
// Private Methods
|
|
9484
|
+
// ============================================================================
|
|
9485
|
+
/**
|
|
9486
|
+
* Update internal state and notify listeners.
|
|
9487
|
+
*/
|
|
9488
|
+
setState(partial) {
|
|
9489
|
+
const prev = this.state;
|
|
9490
|
+
this.state = { ...this.state, ...partial };
|
|
9491
|
+
this.notifyStateChange(this.state, prev);
|
|
9492
|
+
}
|
|
9493
|
+
/**
|
|
9494
|
+
* Set up notification handlers on the App instance.
|
|
9495
|
+
*
|
|
9496
|
+
* Maps the official SDK's callback pattern to our event emitter pattern,
|
|
9497
|
+
* allowing consumers to use `.on("tool-result", ...)` etc.
|
|
9498
|
+
*/
|
|
9499
|
+
setupHandlers() {
|
|
9500
|
+
if (!this.app) return;
|
|
9501
|
+
this.app.ontoolinput = (params) => {
|
|
9502
|
+
console.debug(`[${this.config.name}] Received tool-input`, { args: params.arguments });
|
|
9503
|
+
this.emit("tool-input", params.arguments || {});
|
|
9504
|
+
};
|
|
9505
|
+
this.app.ontoolresult = (params) => {
|
|
9506
|
+
console.log(`[McpAppHostClient] ontoolresult called`, {
|
|
9507
|
+
isError: params.isError,
|
|
9508
|
+
source: params.source,
|
|
9509
|
+
hasContent: !!params.content,
|
|
9510
|
+
hasStructuredContent: !!params.structuredContent,
|
|
9511
|
+
structuredContent: params.structuredContent
|
|
9512
|
+
});
|
|
9513
|
+
const result = {
|
|
9514
|
+
content: this.extractTextContent(params.content),
|
|
9515
|
+
structuredContent: params.structuredContent,
|
|
9516
|
+
isError: params.isError,
|
|
9517
|
+
source: params.source
|
|
9518
|
+
};
|
|
9519
|
+
console.log(`[McpAppHostClient] Emitting tool-result event`, result);
|
|
9520
|
+
this.emit("tool-result", result);
|
|
9521
|
+
};
|
|
9522
|
+
this.app.onhostcontextchanged = (params) => {
|
|
9523
|
+
console.debug(`[${this.config.name}] Host context changed`, { theme: params.theme });
|
|
9524
|
+
this.applyHostContext(params);
|
|
9525
|
+
};
|
|
9526
|
+
this.app.onteardown = async (_params, _extra) => {
|
|
9527
|
+
console.debug(`[${this.config.name}] Teardown requested`);
|
|
9528
|
+
await this.emit("teardown");
|
|
9529
|
+
return {};
|
|
9530
|
+
};
|
|
9531
|
+
}
|
|
9532
|
+
/**
|
|
9533
|
+
* Initiate connection using PostMessageTransport.
|
|
9534
|
+
*
|
|
9535
|
+
* The SDK's App.connect() handles the protocol handshake correctly:
|
|
9536
|
+
* the guest (App) sends `ui/initialize` to the host.
|
|
9537
|
+
*/
|
|
9538
|
+
async initiateConnection() {
|
|
9539
|
+
if (!this.app) {
|
|
9540
|
+
return;
|
|
9541
|
+
}
|
|
9542
|
+
try {
|
|
9543
|
+
const transport = new Yn(window.parent, window.parent);
|
|
9544
|
+
await this.app.connect(transport);
|
|
9545
|
+
const hostContext = this.app.getHostContext();
|
|
9546
|
+
console.debug(`[${this.config.name}] Connected to host`, { theme: hostContext?.theme });
|
|
9547
|
+
if (hostContext) {
|
|
9548
|
+
this.applyHostContext(hostContext);
|
|
9549
|
+
const widgetState = hostContext.widgetState;
|
|
9550
|
+
if (widgetState) {
|
|
9551
|
+
this.setState({ widgetState });
|
|
9552
|
+
this.emit("widget-state-change", widgetState);
|
|
9553
|
+
}
|
|
9554
|
+
}
|
|
9555
|
+
this.setState({ isReady: true });
|
|
9556
|
+
} catch (error) {
|
|
9557
|
+
console.error(`[${this.config.name}] Connection failed`, { error });
|
|
9558
|
+
}
|
|
9559
|
+
}
|
|
9560
|
+
/**
|
|
9561
|
+
* Apply theme, styles, and fonts from host context.
|
|
9562
|
+
*/
|
|
9563
|
+
applyHostContext(context) {
|
|
9564
|
+
if (context.theme) {
|
|
9565
|
+
YU(context.theme);
|
|
9566
|
+
this.emit("theme-change", context.theme);
|
|
9567
|
+
}
|
|
9568
|
+
if (context.styles?.variables) {
|
|
9569
|
+
QU(context.styles.variables);
|
|
9570
|
+
}
|
|
9571
|
+
if (context.styles?.css?.fonts) {
|
|
9572
|
+
qU(context.styles.css.fonts);
|
|
9573
|
+
}
|
|
9574
|
+
}
|
|
9575
|
+
/**
|
|
9576
|
+
* Extract text content from SDK result content array.
|
|
9577
|
+
* Filters to only include text items since our ToolResult type expects text.
|
|
9578
|
+
*/
|
|
9579
|
+
extractTextContent(content) {
|
|
9580
|
+
return content?.filter((item) => item.type === "text").map((item) => ({ type: item.type, text: item.text }));
|
|
9581
|
+
}
|
|
9555
9582
|
};
|
|
9556
9583
|
|
|
9557
|
-
// src/core/
|
|
9558
|
-
var
|
|
9584
|
+
// src/core/ChatGptAppHostClient.ts
|
|
9585
|
+
var ChatGptAppHostClient = class extends Subscribable {
|
|
9586
|
+
// ============================================================================
|
|
9587
|
+
// Private Properties
|
|
9588
|
+
// ============================================================================
|
|
9559
9589
|
state = {
|
|
9560
9590
|
isReady: false,
|
|
9561
9591
|
environment: "chatgpt",
|
|
@@ -9565,24 +9595,39 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9565
9595
|
connected = false;
|
|
9566
9596
|
hasProcessedInitialData = false;
|
|
9567
9597
|
globalsHandler = null;
|
|
9598
|
+
// ============================================================================
|
|
9599
|
+
// Constructor
|
|
9600
|
+
// ============================================================================
|
|
9568
9601
|
constructor(config) {
|
|
9569
9602
|
super();
|
|
9570
9603
|
this.config = config;
|
|
9571
9604
|
}
|
|
9605
|
+
// ============================================================================
|
|
9606
|
+
// Public API
|
|
9607
|
+
// ============================================================================
|
|
9608
|
+
/**
|
|
9609
|
+
* Get the current client state.
|
|
9610
|
+
*/
|
|
9572
9611
|
getState() {
|
|
9573
9612
|
return this.state;
|
|
9574
9613
|
}
|
|
9575
|
-
|
|
9576
|
-
|
|
9577
|
-
|
|
9578
|
-
|
|
9579
|
-
|
|
9614
|
+
/**
|
|
9615
|
+
* Connect to the ChatGPT host.
|
|
9616
|
+
*
|
|
9617
|
+
* Processes initial data from `window.openai` and sets up a listener
|
|
9618
|
+
* for subsequent `openai:set_globals` events.
|
|
9619
|
+
*/
|
|
9580
9620
|
connect() {
|
|
9581
9621
|
if (this.connected) return;
|
|
9582
9622
|
this.connected = true;
|
|
9583
9623
|
this.processInitialData();
|
|
9584
9624
|
this.setupGlobalsListener();
|
|
9585
9625
|
}
|
|
9626
|
+
/**
|
|
9627
|
+
* Disconnect from the host.
|
|
9628
|
+
*
|
|
9629
|
+
* Removes the globals event listener.
|
|
9630
|
+
*/
|
|
9586
9631
|
disconnect() {
|
|
9587
9632
|
if (!this.connected) return;
|
|
9588
9633
|
this.connected = false;
|
|
@@ -9591,39 +9636,13 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9591
9636
|
this.globalsHandler = null;
|
|
9592
9637
|
}
|
|
9593
9638
|
}
|
|
9594
|
-
|
|
9595
|
-
|
|
9596
|
-
|
|
9597
|
-
|
|
9598
|
-
|
|
9599
|
-
|
|
9600
|
-
|
|
9601
|
-
this.hasProcessedInitialData = true;
|
|
9602
|
-
this.setState({ isReady: true });
|
|
9603
|
-
if (openai.toolOutput) {
|
|
9604
|
-
this.emit("tool-input", openai.toolOutput);
|
|
9605
|
-
this.emit("tool-result", { structuredContent: openai.toolOutput });
|
|
9606
|
-
}
|
|
9607
|
-
if (openai.widgetState) {
|
|
9608
|
-
this.setState({ widgetState: openai.widgetState });
|
|
9609
|
-
this.emit("widget-state-change", openai.widgetState);
|
|
9610
|
-
}
|
|
9611
|
-
}
|
|
9612
|
-
setupGlobalsListener() {
|
|
9613
|
-
this.globalsHandler = (event) => {
|
|
9614
|
-
const customEvent = event;
|
|
9615
|
-
const globals = customEvent.detail?.globals;
|
|
9616
|
-
if (globals?.toolOutput) {
|
|
9617
|
-
this.emit("tool-input", globals.toolOutput);
|
|
9618
|
-
this.emit("tool-result", { structuredContent: globals.toolOutput });
|
|
9619
|
-
}
|
|
9620
|
-
if (globals?.widgetState !== void 0) {
|
|
9621
|
-
this.setState({ widgetState: globals.widgetState });
|
|
9622
|
-
this.emit("widget-state-change", globals.widgetState);
|
|
9623
|
-
}
|
|
9624
|
-
};
|
|
9625
|
-
window.addEventListener("openai:set_globals", this.globalsHandler, { passive: true });
|
|
9626
|
-
}
|
|
9639
|
+
/**
|
|
9640
|
+
* Call a tool on the MCP server via the ChatGPT bridge.
|
|
9641
|
+
*
|
|
9642
|
+
* @param toolName - Name of the tool to call
|
|
9643
|
+
* @param args - Arguments to pass to the tool
|
|
9644
|
+
* @returns Tool result with content and structuredContent
|
|
9645
|
+
*/
|
|
9627
9646
|
async callTool(toolName, args) {
|
|
9628
9647
|
const openai = window.openai;
|
|
9629
9648
|
if (!openai?.callTool) {
|
|
@@ -9641,8 +9660,18 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9641
9660
|
this.emit("tool-result", result);
|
|
9642
9661
|
return result;
|
|
9643
9662
|
}
|
|
9663
|
+
/**
|
|
9664
|
+
* Send a notification to the host.
|
|
9665
|
+
*
|
|
9666
|
+
* No-op on ChatGPT — notifications are not supported.
|
|
9667
|
+
*/
|
|
9644
9668
|
sendNotification(_method, _params) {
|
|
9645
9669
|
}
|
|
9670
|
+
/**
|
|
9671
|
+
* Set widget state and sync with the ChatGPT host.
|
|
9672
|
+
*
|
|
9673
|
+
* @param state - New widget state (or null to clear)
|
|
9674
|
+
*/
|
|
9646
9675
|
setWidgetState(state) {
|
|
9647
9676
|
this.setState({ widgetState: state });
|
|
9648
9677
|
if (window.openai?.setWidgetState) {
|
|
@@ -9654,7 +9683,7 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9654
9683
|
* Log a message to the console.
|
|
9655
9684
|
*
|
|
9656
9685
|
* ChatGPT doesn't have a DevConsole, so logs go to browser console only.
|
|
9657
|
-
* This provides API parity with
|
|
9686
|
+
* This provides API parity with McpAppHostClient.
|
|
9658
9687
|
*
|
|
9659
9688
|
* @param level - Log severity level
|
|
9660
9689
|
* @param message - Log message
|
|
@@ -9664,6 +9693,76 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9664
9693
|
const consoleMethod = level === "error" ? "error" : level === "warning" ? "warn" : "log";
|
|
9665
9694
|
console[consoleMethod](`[${this.config.name}]`, message, data ?? "");
|
|
9666
9695
|
}
|
|
9696
|
+
// ============================================================================
|
|
9697
|
+
// Private Methods
|
|
9698
|
+
// ============================================================================
|
|
9699
|
+
/**
|
|
9700
|
+
* Update internal state and notify listeners.
|
|
9701
|
+
*/
|
|
9702
|
+
setState(partial) {
|
|
9703
|
+
const prev = this.state;
|
|
9704
|
+
this.state = { ...this.state, ...partial };
|
|
9705
|
+
this.notifyStateChange(this.state, prev);
|
|
9706
|
+
}
|
|
9707
|
+
/**
|
|
9708
|
+
* Process initial data from `window.openai`.
|
|
9709
|
+
*
|
|
9710
|
+
* Called once on connect to handle any tool output or widget state
|
|
9711
|
+
* that was set before the client connected.
|
|
9712
|
+
*/
|
|
9713
|
+
processInitialData() {
|
|
9714
|
+
if (this.hasProcessedInitialData) return;
|
|
9715
|
+
const openai = window.openai;
|
|
9716
|
+
if (!openai) {
|
|
9717
|
+
console.warn("[SDK] window.openai not available");
|
|
9718
|
+
return;
|
|
9719
|
+
}
|
|
9720
|
+
this.hasProcessedInitialData = true;
|
|
9721
|
+
this.setState({ isReady: true });
|
|
9722
|
+
if (openai.toolOutput) {
|
|
9723
|
+
this.emit("tool-input", openai.toolOutput);
|
|
9724
|
+
this.emit("tool-result", { structuredContent: openai.toolOutput });
|
|
9725
|
+
}
|
|
9726
|
+
if (openai.widgetState) {
|
|
9727
|
+
this.setState({ widgetState: openai.widgetState });
|
|
9728
|
+
this.emit("widget-state-change", openai.widgetState);
|
|
9729
|
+
}
|
|
9730
|
+
}
|
|
9731
|
+
/**
|
|
9732
|
+
* Set up listener for `openai:set_globals` events.
|
|
9733
|
+
*
|
|
9734
|
+
* ChatGPT dispatches this event when tool output or widget state
|
|
9735
|
+
* changes after initial load.
|
|
9736
|
+
*/
|
|
9737
|
+
setupGlobalsListener() {
|
|
9738
|
+
this.globalsHandler = (event) => {
|
|
9739
|
+
const customEvent = event;
|
|
9740
|
+
const globals = customEvent.detail?.globals;
|
|
9741
|
+
if (globals?.toolOutput) {
|
|
9742
|
+
this.emit("tool-input", globals.toolOutput);
|
|
9743
|
+
this.emit("tool-result", { structuredContent: globals.toolOutput });
|
|
9744
|
+
}
|
|
9745
|
+
if (globals?.widgetState !== void 0) {
|
|
9746
|
+
this.setState({ widgetState: globals.widgetState });
|
|
9747
|
+
this.emit("widget-state-change", globals.widgetState);
|
|
9748
|
+
}
|
|
9749
|
+
};
|
|
9750
|
+
window.addEventListener("openai:set_globals", this.globalsHandler, { passive: true });
|
|
9751
|
+
}
|
|
9752
|
+
/**
|
|
9753
|
+
* Request a display mode change from the ChatGPT host.
|
|
9754
|
+
*
|
|
9755
|
+
* @param params - Display mode to request
|
|
9756
|
+
* @returns The resulting display mode
|
|
9757
|
+
*/
|
|
9758
|
+
async requestDisplayMode(params) {
|
|
9759
|
+
const openai = window.openai;
|
|
9760
|
+
if (openai?.requestDisplayMode) {
|
|
9761
|
+
const result = await openai.requestDisplayMode({ mode: params.mode });
|
|
9762
|
+
return { mode: result.mode };
|
|
9763
|
+
}
|
|
9764
|
+
return { mode: params.mode };
|
|
9765
|
+
}
|
|
9667
9766
|
};
|
|
9668
9767
|
|
|
9669
9768
|
// src/core/utils.ts
|
|
@@ -9680,123 +9779,8 @@ function detectEnvironment() {
|
|
|
9680
9779
|
return "standalone";
|
|
9681
9780
|
}
|
|
9682
9781
|
|
|
9683
|
-
// src/core/
|
|
9684
|
-
|
|
9685
|
-
id;
|
|
9686
|
-
_state;
|
|
9687
|
-
listeners = /* @__PURE__ */ new Set();
|
|
9688
|
-
hostClient = null;
|
|
9689
|
-
hostUnsubscribe = null;
|
|
9690
|
-
constructor(initialState = {}, options = {}) {
|
|
9691
|
-
this.id = options.id ?? this.generateId();
|
|
9692
|
-
this._state = {
|
|
9693
|
-
internal: initialState.internal ?? {},
|
|
9694
|
-
backend: initialState.backend ?? {},
|
|
9695
|
-
ui: initialState.ui ?? null
|
|
9696
|
-
};
|
|
9697
|
-
}
|
|
9698
|
-
generateId() {
|
|
9699
|
-
return `session_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
9700
|
-
}
|
|
9701
|
-
get state() {
|
|
9702
|
-
return this._state;
|
|
9703
|
-
}
|
|
9704
|
-
get internal() {
|
|
9705
|
-
return this._state.internal;
|
|
9706
|
-
}
|
|
9707
|
-
get backend() {
|
|
9708
|
-
return this._state.backend;
|
|
9709
|
-
}
|
|
9710
|
-
get ui() {
|
|
9711
|
-
return this._state.ui;
|
|
9712
|
-
}
|
|
9713
|
-
subscribe(listener) {
|
|
9714
|
-
this.listeners.add(listener);
|
|
9715
|
-
return () => {
|
|
9716
|
-
this.listeners.delete(listener);
|
|
9717
|
-
};
|
|
9718
|
-
}
|
|
9719
|
-
notify(prevState) {
|
|
9720
|
-
this.listeners.forEach((listener) => listener(this._state, prevState));
|
|
9721
|
-
}
|
|
9722
|
-
setState(partial) {
|
|
9723
|
-
const prev = this._state;
|
|
9724
|
-
this._state = {
|
|
9725
|
-
internal: partial.internal !== void 0 ? { ...prev.internal, ...partial.internal } : prev.internal,
|
|
9726
|
-
backend: partial.backend !== void 0 ? { ...prev.backend, ...partial.backend } : prev.backend,
|
|
9727
|
-
ui: partial.ui !== void 0 ? partial.ui : prev.ui
|
|
9728
|
-
};
|
|
9729
|
-
this.notify(prev);
|
|
9730
|
-
}
|
|
9731
|
-
setInternal(internal) {
|
|
9732
|
-
const prev = this._state;
|
|
9733
|
-
this._state = {
|
|
9734
|
-
...prev,
|
|
9735
|
-
internal: { ...prev.internal, ...internal }
|
|
9736
|
-
};
|
|
9737
|
-
this.notify(prev);
|
|
9738
|
-
}
|
|
9739
|
-
setBackend(backend) {
|
|
9740
|
-
const prev = this._state;
|
|
9741
|
-
this._state = {
|
|
9742
|
-
...prev,
|
|
9743
|
-
backend: { ...prev.backend, ...backend }
|
|
9744
|
-
};
|
|
9745
|
-
this.notify(prev);
|
|
9746
|
-
}
|
|
9747
|
-
setUi(ui2) {
|
|
9748
|
-
console.log(`[AppSession:${this.id}] setUi`, ui2);
|
|
9749
|
-
const prev = this._state;
|
|
9750
|
-
this._state = { ...prev, ui: ui2 };
|
|
9751
|
-
this.notify(prev);
|
|
9752
|
-
if (this.hostClient) {
|
|
9753
|
-
this.hostClient.setWidgetState(ui2);
|
|
9754
|
-
}
|
|
9755
|
-
}
|
|
9756
|
-
updateUi(partial) {
|
|
9757
|
-
const prev = this._state;
|
|
9758
|
-
const newUi = prev.ui !== null ? { ...prev.ui, ...partial } : partial;
|
|
9759
|
-
this._state = { ...prev, ui: newUi };
|
|
9760
|
-
this.notify(prev);
|
|
9761
|
-
if (this.hostClient) {
|
|
9762
|
-
this.hostClient.setWidgetState(newUi);
|
|
9763
|
-
}
|
|
9764
|
-
}
|
|
9765
|
-
bindHost(host) {
|
|
9766
|
-
this.hostClient = host;
|
|
9767
|
-
console.log(`[AppSession:${this.id}] Binding to host`);
|
|
9768
|
-
const currentHostState = host.getState();
|
|
9769
|
-
if (currentHostState.widgetState !== null && currentHostState.widgetState !== void 0) {
|
|
9770
|
-
console.log(`[AppSession:${this.id}] Restoring UI state from host`, currentHostState.widgetState);
|
|
9771
|
-
const prev = this._state;
|
|
9772
|
-
this._state = { ...prev, ui: currentHostState.widgetState };
|
|
9773
|
-
this.notify(prev);
|
|
9774
|
-
}
|
|
9775
|
-
this.hostUnsubscribe = host.on("widget-state-change", (widgetState) => {
|
|
9776
|
-
console.log(`[AppSession:${this.id}] Host widget state changed`, widgetState);
|
|
9777
|
-
const prev = this._state;
|
|
9778
|
-
this._state = { ...prev, ui: widgetState };
|
|
9779
|
-
this.notify(prev);
|
|
9780
|
-
});
|
|
9781
|
-
return () => {
|
|
9782
|
-
this.unbindHost();
|
|
9783
|
-
};
|
|
9784
|
-
}
|
|
9785
|
-
unbindHost() {
|
|
9786
|
-
console.log(`[AppSession:${this.id}] Unbinding from host`);
|
|
9787
|
-
if (this.hostUnsubscribe) {
|
|
9788
|
-
this.hostUnsubscribe();
|
|
9789
|
-
this.hostUnsubscribe = null;
|
|
9790
|
-
}
|
|
9791
|
-
this.hostClient = null;
|
|
9792
|
-
}
|
|
9793
|
-
injectAppSessionId(data) {
|
|
9794
|
-
return { ...data, appSessionId: this.id };
|
|
9795
|
-
}
|
|
9796
|
-
};
|
|
9797
|
-
|
|
9798
|
-
// src/core/channel.ts
|
|
9799
|
-
function createChannel(url, config = {}) {
|
|
9782
|
+
// src/core/websocket.ts
|
|
9783
|
+
function createWebSocket(url, config = {}) {
|
|
9800
9784
|
const {
|
|
9801
9785
|
onMessage,
|
|
9802
9786
|
onStatusChange,
|
|
@@ -9816,36 +9800,36 @@ function createChannel(url, config = {}) {
|
|
|
9816
9800
|
};
|
|
9817
9801
|
const connect = () => {
|
|
9818
9802
|
if (ws?.readyState === WebSocket.OPEN || ws?.readyState === WebSocket.CONNECTING) {
|
|
9819
|
-
console.log("[
|
|
9803
|
+
console.log("[WebSocket] Already connected/connecting to:", url);
|
|
9820
9804
|
return;
|
|
9821
9805
|
}
|
|
9822
9806
|
intentionalClose = false;
|
|
9823
9807
|
setStatus("connecting");
|
|
9824
|
-
console.log("[
|
|
9808
|
+
console.log("[WebSocket] Connecting to:", url);
|
|
9825
9809
|
try {
|
|
9826
9810
|
ws = new WebSocket(url);
|
|
9827
9811
|
} catch (e2) {
|
|
9828
|
-
console.error("[
|
|
9812
|
+
console.error("[WebSocket] Failed to create WebSocket:", e2);
|
|
9829
9813
|
setStatus("error", "Failed to create WebSocket");
|
|
9830
9814
|
scheduleReconnect();
|
|
9831
9815
|
return;
|
|
9832
9816
|
}
|
|
9833
9817
|
ws.onopen = () => {
|
|
9834
|
-
console.log("[
|
|
9818
|
+
console.log("[WebSocket] Connected to:", url);
|
|
9835
9819
|
reconnectAttempts = 0;
|
|
9836
9820
|
setStatus("connected");
|
|
9837
9821
|
};
|
|
9838
9822
|
ws.onmessage = (event) => {
|
|
9839
9823
|
try {
|
|
9840
9824
|
const message = JSON.parse(event.data);
|
|
9841
|
-
console.log("[
|
|
9825
|
+
console.log("[WebSocket] Received message:", message);
|
|
9842
9826
|
onMessage?.(message);
|
|
9843
9827
|
} catch (e2) {
|
|
9844
|
-
console.error("[
|
|
9828
|
+
console.error("[WebSocket] Failed to parse message:", e2);
|
|
9845
9829
|
}
|
|
9846
9830
|
};
|
|
9847
9831
|
ws.onerror = (e2) => {
|
|
9848
|
-
console.error("[
|
|
9832
|
+
console.error("[WebSocket] Error:", e2);
|
|
9849
9833
|
setStatus("error", "Connection error");
|
|
9850
9834
|
};
|
|
9851
9835
|
ws.onclose = (event) => {
|
|
@@ -9855,7 +9839,7 @@ function createChannel(url, config = {}) {
|
|
|
9855
9839
|
return;
|
|
9856
9840
|
}
|
|
9857
9841
|
if (event.code === 4004) {
|
|
9858
|
-
setStatus("error", "
|
|
9842
|
+
setStatus("error", "Instance WebSocket not found");
|
|
9859
9843
|
return;
|
|
9860
9844
|
}
|
|
9861
9845
|
scheduleReconnect();
|
|
@@ -9903,19 +9887,18 @@ function createChannel(url, config = {}) {
|
|
|
9903
9887
|
function createHost(config) {
|
|
9904
9888
|
const environment = detectEnvironment();
|
|
9905
9889
|
if (environment === "chatgpt") {
|
|
9906
|
-
return new
|
|
9890
|
+
return new ChatGptAppHostClient(config);
|
|
9907
9891
|
}
|
|
9908
|
-
return new
|
|
9892
|
+
return new McpAppHostClient(config);
|
|
9909
9893
|
}
|
|
9910
9894
|
export {
|
|
9911
|
-
|
|
9912
|
-
|
|
9913
|
-
McpHostClient,
|
|
9895
|
+
ChatGptAppHostClient,
|
|
9896
|
+
McpAppHostClient,
|
|
9914
9897
|
YU as applyDocumentTheme,
|
|
9915
9898
|
qU as applyHostFonts,
|
|
9916
9899
|
QU as applyHostStyleVariables,
|
|
9917
|
-
createChannel,
|
|
9918
9900
|
createHost,
|
|
9901
|
+
createWebSocket,
|
|
9919
9902
|
detectEnvironment,
|
|
9920
9903
|
VU as getDocumentTheme
|
|
9921
9904
|
};
|