@creature-ai/sdk 0.1.2 → 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 +299 -72
- package/dist/core/index.js +235 -267
- package/dist/core/index.js.map +1 -1
- package/dist/react/index.d.ts +47 -51
- package/dist/react/index.js +249 -311
- 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-JBEuUzEi.d.ts +0 -186
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
|
};
|
|
@@ -9541,8 +9463,6 @@ var McpHostClient = class extends Subscribable {
|
|
|
9541
9463
|
* to the host. Logs appear in the unified DevConsole alongside server logs,
|
|
9542
9464
|
* with appropriate color coding based on level.
|
|
9543
9465
|
*
|
|
9544
|
-
* The logger name is automatically set to the app name from config.
|
|
9545
|
-
*
|
|
9546
9466
|
* @param level - Log severity level (debug, info, notice, warning, error)
|
|
9547
9467
|
* @param message - Log message
|
|
9548
9468
|
* @param data - Optional structured data to include with the log
|
|
@@ -9559,10 +9479,113 @@ var McpHostClient = class extends Subscribable {
|
|
|
9559
9479
|
data: data ? { message, ...data } : message
|
|
9560
9480
|
});
|
|
9561
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
|
+
}
|
|
9562
9582
|
};
|
|
9563
9583
|
|
|
9564
|
-
// src/core/
|
|
9565
|
-
var
|
|
9584
|
+
// src/core/ChatGptAppHostClient.ts
|
|
9585
|
+
var ChatGptAppHostClient = class extends Subscribable {
|
|
9586
|
+
// ============================================================================
|
|
9587
|
+
// Private Properties
|
|
9588
|
+
// ============================================================================
|
|
9566
9589
|
state = {
|
|
9567
9590
|
isReady: false,
|
|
9568
9591
|
environment: "chatgpt",
|
|
@@ -9572,24 +9595,39 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9572
9595
|
connected = false;
|
|
9573
9596
|
hasProcessedInitialData = false;
|
|
9574
9597
|
globalsHandler = null;
|
|
9598
|
+
// ============================================================================
|
|
9599
|
+
// Constructor
|
|
9600
|
+
// ============================================================================
|
|
9575
9601
|
constructor(config) {
|
|
9576
9602
|
super();
|
|
9577
9603
|
this.config = config;
|
|
9578
9604
|
}
|
|
9605
|
+
// ============================================================================
|
|
9606
|
+
// Public API
|
|
9607
|
+
// ============================================================================
|
|
9608
|
+
/**
|
|
9609
|
+
* Get the current client state.
|
|
9610
|
+
*/
|
|
9579
9611
|
getState() {
|
|
9580
9612
|
return this.state;
|
|
9581
9613
|
}
|
|
9582
|
-
|
|
9583
|
-
|
|
9584
|
-
|
|
9585
|
-
|
|
9586
|
-
|
|
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
|
+
*/
|
|
9587
9620
|
connect() {
|
|
9588
9621
|
if (this.connected) return;
|
|
9589
9622
|
this.connected = true;
|
|
9590
9623
|
this.processInitialData();
|
|
9591
9624
|
this.setupGlobalsListener();
|
|
9592
9625
|
}
|
|
9626
|
+
/**
|
|
9627
|
+
* Disconnect from the host.
|
|
9628
|
+
*
|
|
9629
|
+
* Removes the globals event listener.
|
|
9630
|
+
*/
|
|
9593
9631
|
disconnect() {
|
|
9594
9632
|
if (!this.connected) return;
|
|
9595
9633
|
this.connected = false;
|
|
@@ -9598,39 +9636,13 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9598
9636
|
this.globalsHandler = null;
|
|
9599
9637
|
}
|
|
9600
9638
|
}
|
|
9601
|
-
|
|
9602
|
-
|
|
9603
|
-
|
|
9604
|
-
|
|
9605
|
-
|
|
9606
|
-
|
|
9607
|
-
|
|
9608
|
-
this.hasProcessedInitialData = true;
|
|
9609
|
-
this.setState({ isReady: true });
|
|
9610
|
-
if (openai.toolOutput) {
|
|
9611
|
-
this.emit("tool-input", openai.toolOutput);
|
|
9612
|
-
this.emit("tool-result", { structuredContent: openai.toolOutput });
|
|
9613
|
-
}
|
|
9614
|
-
if (openai.widgetState) {
|
|
9615
|
-
this.setState({ widgetState: openai.widgetState });
|
|
9616
|
-
this.emit("widget-state-change", openai.widgetState);
|
|
9617
|
-
}
|
|
9618
|
-
}
|
|
9619
|
-
setupGlobalsListener() {
|
|
9620
|
-
this.globalsHandler = (event) => {
|
|
9621
|
-
const customEvent = event;
|
|
9622
|
-
const globals = customEvent.detail?.globals;
|
|
9623
|
-
if (globals?.toolOutput) {
|
|
9624
|
-
this.emit("tool-input", globals.toolOutput);
|
|
9625
|
-
this.emit("tool-result", { structuredContent: globals.toolOutput });
|
|
9626
|
-
}
|
|
9627
|
-
if (globals?.widgetState !== void 0) {
|
|
9628
|
-
this.setState({ widgetState: globals.widgetState });
|
|
9629
|
-
this.emit("widget-state-change", globals.widgetState);
|
|
9630
|
-
}
|
|
9631
|
-
};
|
|
9632
|
-
window.addEventListener("openai:set_globals", this.globalsHandler, { passive: true });
|
|
9633
|
-
}
|
|
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
|
+
*/
|
|
9634
9646
|
async callTool(toolName, args) {
|
|
9635
9647
|
const openai = window.openai;
|
|
9636
9648
|
if (!openai?.callTool) {
|
|
@@ -9648,8 +9660,18 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9648
9660
|
this.emit("tool-result", result);
|
|
9649
9661
|
return result;
|
|
9650
9662
|
}
|
|
9663
|
+
/**
|
|
9664
|
+
* Send a notification to the host.
|
|
9665
|
+
*
|
|
9666
|
+
* No-op on ChatGPT — notifications are not supported.
|
|
9667
|
+
*/
|
|
9651
9668
|
sendNotification(_method, _params) {
|
|
9652
9669
|
}
|
|
9670
|
+
/**
|
|
9671
|
+
* Set widget state and sync with the ChatGPT host.
|
|
9672
|
+
*
|
|
9673
|
+
* @param state - New widget state (or null to clear)
|
|
9674
|
+
*/
|
|
9653
9675
|
setWidgetState(state) {
|
|
9654
9676
|
this.setState({ widgetState: state });
|
|
9655
9677
|
if (window.openai?.setWidgetState) {
|
|
@@ -9661,7 +9683,7 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9661
9683
|
* Log a message to the console.
|
|
9662
9684
|
*
|
|
9663
9685
|
* ChatGPT doesn't have a DevConsole, so logs go to browser console only.
|
|
9664
|
-
* This provides API parity with
|
|
9686
|
+
* This provides API parity with McpAppHostClient.
|
|
9665
9687
|
*
|
|
9666
9688
|
* @param level - Log severity level
|
|
9667
9689
|
* @param message - Log message
|
|
@@ -9671,6 +9693,68 @@ var ChatGPTHostClient = class extends Subscribable {
|
|
|
9671
9693
|
const consoleMethod = level === "error" ? "error" : level === "warning" ? "warn" : "log";
|
|
9672
9694
|
console[consoleMethod](`[${this.config.name}]`, message, data ?? "");
|
|
9673
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
|
+
*/
|
|
9674
9758
|
async requestDisplayMode(params) {
|
|
9675
9759
|
const openai = window.openai;
|
|
9676
9760
|
if (openai?.requestDisplayMode) {
|
|
@@ -9695,123 +9779,8 @@ function detectEnvironment() {
|
|
|
9695
9779
|
return "standalone";
|
|
9696
9780
|
}
|
|
9697
9781
|
|
|
9698
|
-
// src/core/
|
|
9699
|
-
|
|
9700
|
-
id;
|
|
9701
|
-
_state;
|
|
9702
|
-
listeners = /* @__PURE__ */ new Set();
|
|
9703
|
-
hostClient = null;
|
|
9704
|
-
hostUnsubscribe = null;
|
|
9705
|
-
constructor(initialState = {}, options = {}) {
|
|
9706
|
-
this.id = options.id ?? this.generateId();
|
|
9707
|
-
this._state = {
|
|
9708
|
-
internal: initialState.internal ?? {},
|
|
9709
|
-
backend: initialState.backend ?? {},
|
|
9710
|
-
ui: initialState.ui ?? null
|
|
9711
|
-
};
|
|
9712
|
-
}
|
|
9713
|
-
generateId() {
|
|
9714
|
-
return `session_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
9715
|
-
}
|
|
9716
|
-
get state() {
|
|
9717
|
-
return this._state;
|
|
9718
|
-
}
|
|
9719
|
-
get internal() {
|
|
9720
|
-
return this._state.internal;
|
|
9721
|
-
}
|
|
9722
|
-
get backend() {
|
|
9723
|
-
return this._state.backend;
|
|
9724
|
-
}
|
|
9725
|
-
get ui() {
|
|
9726
|
-
return this._state.ui;
|
|
9727
|
-
}
|
|
9728
|
-
subscribe(listener) {
|
|
9729
|
-
this.listeners.add(listener);
|
|
9730
|
-
return () => {
|
|
9731
|
-
this.listeners.delete(listener);
|
|
9732
|
-
};
|
|
9733
|
-
}
|
|
9734
|
-
notify(prevState) {
|
|
9735
|
-
this.listeners.forEach((listener) => listener(this._state, prevState));
|
|
9736
|
-
}
|
|
9737
|
-
setState(partial) {
|
|
9738
|
-
const prev = this._state;
|
|
9739
|
-
this._state = {
|
|
9740
|
-
internal: partial.internal !== void 0 ? { ...prev.internal, ...partial.internal } : prev.internal,
|
|
9741
|
-
backend: partial.backend !== void 0 ? { ...prev.backend, ...partial.backend } : prev.backend,
|
|
9742
|
-
ui: partial.ui !== void 0 ? partial.ui : prev.ui
|
|
9743
|
-
};
|
|
9744
|
-
this.notify(prev);
|
|
9745
|
-
}
|
|
9746
|
-
setInternal(internal) {
|
|
9747
|
-
const prev = this._state;
|
|
9748
|
-
this._state = {
|
|
9749
|
-
...prev,
|
|
9750
|
-
internal: { ...prev.internal, ...internal }
|
|
9751
|
-
};
|
|
9752
|
-
this.notify(prev);
|
|
9753
|
-
}
|
|
9754
|
-
setBackend(backend) {
|
|
9755
|
-
const prev = this._state;
|
|
9756
|
-
this._state = {
|
|
9757
|
-
...prev,
|
|
9758
|
-
backend: { ...prev.backend, ...backend }
|
|
9759
|
-
};
|
|
9760
|
-
this.notify(prev);
|
|
9761
|
-
}
|
|
9762
|
-
setUi(ui2) {
|
|
9763
|
-
console.log(`[AppSession:${this.id}] setUi`, ui2);
|
|
9764
|
-
const prev = this._state;
|
|
9765
|
-
this._state = { ...prev, ui: ui2 };
|
|
9766
|
-
this.notify(prev);
|
|
9767
|
-
if (this.hostClient) {
|
|
9768
|
-
this.hostClient.setWidgetState(ui2);
|
|
9769
|
-
}
|
|
9770
|
-
}
|
|
9771
|
-
updateUi(partial) {
|
|
9772
|
-
const prev = this._state;
|
|
9773
|
-
const newUi = prev.ui !== null ? { ...prev.ui, ...partial } : partial;
|
|
9774
|
-
this._state = { ...prev, ui: newUi };
|
|
9775
|
-
this.notify(prev);
|
|
9776
|
-
if (this.hostClient) {
|
|
9777
|
-
this.hostClient.setWidgetState(newUi);
|
|
9778
|
-
}
|
|
9779
|
-
}
|
|
9780
|
-
bindHost(host) {
|
|
9781
|
-
this.hostClient = host;
|
|
9782
|
-
console.log(`[AppSession:${this.id}] Binding to host`);
|
|
9783
|
-
const currentHostState = host.getState();
|
|
9784
|
-
if (currentHostState.widgetState !== null && currentHostState.widgetState !== void 0) {
|
|
9785
|
-
console.log(`[AppSession:${this.id}] Restoring UI state from host`, currentHostState.widgetState);
|
|
9786
|
-
const prev = this._state;
|
|
9787
|
-
this._state = { ...prev, ui: currentHostState.widgetState };
|
|
9788
|
-
this.notify(prev);
|
|
9789
|
-
}
|
|
9790
|
-
this.hostUnsubscribe = host.on("widget-state-change", (widgetState) => {
|
|
9791
|
-
console.log(`[AppSession:${this.id}] Host widget state changed`, widgetState);
|
|
9792
|
-
const prev = this._state;
|
|
9793
|
-
this._state = { ...prev, ui: widgetState };
|
|
9794
|
-
this.notify(prev);
|
|
9795
|
-
});
|
|
9796
|
-
return () => {
|
|
9797
|
-
this.unbindHost();
|
|
9798
|
-
};
|
|
9799
|
-
}
|
|
9800
|
-
unbindHost() {
|
|
9801
|
-
console.log(`[AppSession:${this.id}] Unbinding from host`);
|
|
9802
|
-
if (this.hostUnsubscribe) {
|
|
9803
|
-
this.hostUnsubscribe();
|
|
9804
|
-
this.hostUnsubscribe = null;
|
|
9805
|
-
}
|
|
9806
|
-
this.hostClient = null;
|
|
9807
|
-
}
|
|
9808
|
-
injectAppSessionId(data) {
|
|
9809
|
-
return { ...data, appSessionId: this.id };
|
|
9810
|
-
}
|
|
9811
|
-
};
|
|
9812
|
-
|
|
9813
|
-
// src/core/channel.ts
|
|
9814
|
-
function createChannel(url, config = {}) {
|
|
9782
|
+
// src/core/websocket.ts
|
|
9783
|
+
function createWebSocket(url, config = {}) {
|
|
9815
9784
|
const {
|
|
9816
9785
|
onMessage,
|
|
9817
9786
|
onStatusChange,
|
|
@@ -9831,36 +9800,36 @@ function createChannel(url, config = {}) {
|
|
|
9831
9800
|
};
|
|
9832
9801
|
const connect = () => {
|
|
9833
9802
|
if (ws?.readyState === WebSocket.OPEN || ws?.readyState === WebSocket.CONNECTING) {
|
|
9834
|
-
console.log("[
|
|
9803
|
+
console.log("[WebSocket] Already connected/connecting to:", url);
|
|
9835
9804
|
return;
|
|
9836
9805
|
}
|
|
9837
9806
|
intentionalClose = false;
|
|
9838
9807
|
setStatus("connecting");
|
|
9839
|
-
console.log("[
|
|
9808
|
+
console.log("[WebSocket] Connecting to:", url);
|
|
9840
9809
|
try {
|
|
9841
9810
|
ws = new WebSocket(url);
|
|
9842
9811
|
} catch (e2) {
|
|
9843
|
-
console.error("[
|
|
9812
|
+
console.error("[WebSocket] Failed to create WebSocket:", e2);
|
|
9844
9813
|
setStatus("error", "Failed to create WebSocket");
|
|
9845
9814
|
scheduleReconnect();
|
|
9846
9815
|
return;
|
|
9847
9816
|
}
|
|
9848
9817
|
ws.onopen = () => {
|
|
9849
|
-
console.log("[
|
|
9818
|
+
console.log("[WebSocket] Connected to:", url);
|
|
9850
9819
|
reconnectAttempts = 0;
|
|
9851
9820
|
setStatus("connected");
|
|
9852
9821
|
};
|
|
9853
9822
|
ws.onmessage = (event) => {
|
|
9854
9823
|
try {
|
|
9855
9824
|
const message = JSON.parse(event.data);
|
|
9856
|
-
console.log("[
|
|
9825
|
+
console.log("[WebSocket] Received message:", message);
|
|
9857
9826
|
onMessage?.(message);
|
|
9858
9827
|
} catch (e2) {
|
|
9859
|
-
console.error("[
|
|
9828
|
+
console.error("[WebSocket] Failed to parse message:", e2);
|
|
9860
9829
|
}
|
|
9861
9830
|
};
|
|
9862
9831
|
ws.onerror = (e2) => {
|
|
9863
|
-
console.error("[
|
|
9832
|
+
console.error("[WebSocket] Error:", e2);
|
|
9864
9833
|
setStatus("error", "Connection error");
|
|
9865
9834
|
};
|
|
9866
9835
|
ws.onclose = (event) => {
|
|
@@ -9870,7 +9839,7 @@ function createChannel(url, config = {}) {
|
|
|
9870
9839
|
return;
|
|
9871
9840
|
}
|
|
9872
9841
|
if (event.code === 4004) {
|
|
9873
|
-
setStatus("error", "
|
|
9842
|
+
setStatus("error", "Instance WebSocket not found");
|
|
9874
9843
|
return;
|
|
9875
9844
|
}
|
|
9876
9845
|
scheduleReconnect();
|
|
@@ -9918,19 +9887,18 @@ function createChannel(url, config = {}) {
|
|
|
9918
9887
|
function createHost(config) {
|
|
9919
9888
|
const environment = detectEnvironment();
|
|
9920
9889
|
if (environment === "chatgpt") {
|
|
9921
|
-
return new
|
|
9890
|
+
return new ChatGptAppHostClient(config);
|
|
9922
9891
|
}
|
|
9923
|
-
return new
|
|
9892
|
+
return new McpAppHostClient(config);
|
|
9924
9893
|
}
|
|
9925
9894
|
export {
|
|
9926
|
-
|
|
9927
|
-
|
|
9928
|
-
McpHostClient,
|
|
9895
|
+
ChatGptAppHostClient,
|
|
9896
|
+
McpAppHostClient,
|
|
9929
9897
|
YU as applyDocumentTheme,
|
|
9930
9898
|
qU as applyHostFonts,
|
|
9931
9899
|
QU as applyHostStyleVariables,
|
|
9932
|
-
createChannel,
|
|
9933
9900
|
createHost,
|
|
9901
|
+
createWebSocket,
|
|
9934
9902
|
detectEnvironment,
|
|
9935
9903
|
VU as getDocumentTheme
|
|
9936
9904
|
};
|