@assistant-ui/react 0.5.2 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/edge.js +17 -1
- package/dist/edge.js.map +1 -1
- package/dist/edge.mjs +17 -1
- package/dist/edge.mjs.map +1 -1
- package/dist/index.d.mts +127 -64
- package/dist/index.d.ts +127 -64
- package/dist/index.js +246 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +211 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
@@ -3330,9 +3330,7 @@ var EdgeChatAdapter = class {
|
|
3330
3330
|
signal: abortSignal
|
3331
3331
|
});
|
3332
3332
|
if (result.status !== 200) {
|
3333
|
-
throw new Error(
|
3334
|
-
`Edge runtime returned status ${result.status}: ${await result.text()}`
|
3335
|
-
);
|
3333
|
+
throw new Error(`Status ${result.status}: ${await result.text()}`);
|
3336
3334
|
}
|
3337
3335
|
const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream());
|
3338
3336
|
let update;
|
@@ -3563,6 +3561,213 @@ var useLocalRuntime = (adapter, options) => {
|
|
3563
3561
|
return runtime;
|
3564
3562
|
};
|
3565
3563
|
|
3564
|
+
// src/runtimes/external-store/ExternalStoreThreadRuntime.tsx
|
3565
|
+
import { create as create14 } from "zustand";
|
3566
|
+
|
3567
|
+
// src/runtimes/external-store/getExternalStoreMessage.tsx
|
3568
|
+
var symbolInnerMessage = Symbol("innerMessage");
|
3569
|
+
var getExternalStoreMessage = (message) => {
|
3570
|
+
return message[symbolInnerMessage];
|
3571
|
+
};
|
3572
|
+
|
3573
|
+
// src/runtimes/external-store/useExternalStoreSync.tsx
|
3574
|
+
import { useEffect as useEffect11, useMemo as useMemo3 } from "react";
|
3575
|
+
|
3576
|
+
// src/runtimes/external-store/ThreadMessageConverter.ts
|
3577
|
+
var ThreadMessageConverter = class {
|
3578
|
+
cache = /* @__PURE__ */ new WeakMap();
|
3579
|
+
convertMessages(messages, converter, keyMapper = (key) => key) {
|
3580
|
+
return messages.map((m, idx) => {
|
3581
|
+
const key = keyMapper(m);
|
3582
|
+
const cached = this.cache.get(key);
|
3583
|
+
const newMessage = converter(cached, m, idx);
|
3584
|
+
this.cache.set(key, newMessage);
|
3585
|
+
return newMessage;
|
3586
|
+
});
|
3587
|
+
}
|
3588
|
+
};
|
3589
|
+
|
3590
|
+
// src/runtimes/external-store/useExternalStoreSync.tsx
|
3591
|
+
var useExternalStoreSync = (adapter, updateData) => {
|
3592
|
+
const [converter, convertCallback] = useMemo3(() => {
|
3593
|
+
const converter2 = adapter.convertMessage ?? ((m) => m);
|
3594
|
+
const convertCallback2 = (cache, m, idx) => {
|
3595
|
+
if (cache) return cache;
|
3596
|
+
const newMessage = converter2(m, idx);
|
3597
|
+
newMessage[symbolInnerMessage] = m;
|
3598
|
+
return newMessage;
|
3599
|
+
};
|
3600
|
+
return [new ThreadMessageConverter(), convertCallback2];
|
3601
|
+
}, [adapter.convertMessage]);
|
3602
|
+
useEffect11(() => {
|
3603
|
+
updateData(
|
3604
|
+
adapter.isRunning ?? false,
|
3605
|
+
converter.convertMessages(adapter.messages, convertCallback)
|
3606
|
+
);
|
3607
|
+
}, [
|
3608
|
+
updateData,
|
3609
|
+
converter,
|
3610
|
+
convertCallback,
|
3611
|
+
adapter.messages,
|
3612
|
+
adapter.isRunning
|
3613
|
+
]);
|
3614
|
+
};
|
3615
|
+
|
3616
|
+
// src/runtimes/external-store/ExternalStoreThreadRuntime.tsx
|
3617
|
+
var hasUpcomingMessage = (isRunning, messages) => {
|
3618
|
+
return isRunning && messages[messages.length - 1]?.role !== "assistant";
|
3619
|
+
};
|
3620
|
+
var ExternalStoreThreadRuntime = class {
|
3621
|
+
constructor(store) {
|
3622
|
+
this.store = store;
|
3623
|
+
this.useStore = create14(() => ({
|
3624
|
+
store
|
3625
|
+
}));
|
3626
|
+
}
|
3627
|
+
_subscriptions = /* @__PURE__ */ new Set();
|
3628
|
+
repository = new MessageRepository();
|
3629
|
+
assistantOptimisticId = null;
|
3630
|
+
useStore;
|
3631
|
+
get capabilities() {
|
3632
|
+
return {
|
3633
|
+
edit: this.store.onEdit !== void 0,
|
3634
|
+
reload: this.store.onReload !== void 0,
|
3635
|
+
cancel: this.store.onCancel !== void 0,
|
3636
|
+
copy: this.store.onCopy !== null
|
3637
|
+
};
|
3638
|
+
}
|
3639
|
+
messages = [];
|
3640
|
+
isRunning = false;
|
3641
|
+
getBranches(messageId) {
|
3642
|
+
return this.repository.getBranches(messageId);
|
3643
|
+
}
|
3644
|
+
switchToBranch(branchId) {
|
3645
|
+
this.repository.switchToBranch(branchId);
|
3646
|
+
this.updateMessages(this.repository.getMessages());
|
3647
|
+
}
|
3648
|
+
async append(message) {
|
3649
|
+
if (message.parentId !== (this.messages.at(-1)?.id ?? null)) {
|
3650
|
+
if (!this.store.onEdit)
|
3651
|
+
throw new Error("Runtime does not support editing messages.");
|
3652
|
+
await this.store.onEdit(message);
|
3653
|
+
} else {
|
3654
|
+
await this.store.onNew(message);
|
3655
|
+
}
|
3656
|
+
}
|
3657
|
+
async startRun(parentId) {
|
3658
|
+
if (!this.store.onReload)
|
3659
|
+
throw new Error("Runtime does not support reloading messages.");
|
3660
|
+
await this.store.onReload(parentId);
|
3661
|
+
}
|
3662
|
+
cancelRun() {
|
3663
|
+
if (!this.store.onCancel)
|
3664
|
+
throw new Error("Runtime does not support cancelling runs.");
|
3665
|
+
this.store.onCancel();
|
3666
|
+
if (this.assistantOptimisticId) {
|
3667
|
+
this.repository.deleteMessage(this.assistantOptimisticId);
|
3668
|
+
this.assistantOptimisticId = null;
|
3669
|
+
}
|
3670
|
+
let messages = this.repository.getMessages();
|
3671
|
+
setTimeout(() => {
|
3672
|
+
this.updateMessages(messages);
|
3673
|
+
}, 0);
|
3674
|
+
}
|
3675
|
+
subscribe(callback) {
|
3676
|
+
this._subscriptions.add(callback);
|
3677
|
+
return () => this._subscriptions.delete(callback);
|
3678
|
+
}
|
3679
|
+
updateMessages = (messages) => {
|
3680
|
+
this.store.setMessages?.(
|
3681
|
+
messages.flatMap(getExternalStoreMessage).filter((m) => m != null)
|
3682
|
+
);
|
3683
|
+
};
|
3684
|
+
onStoreUpdated() {
|
3685
|
+
if (this.useStore.getState().store !== this.store) {
|
3686
|
+
this.useStore.setState({ store: this.store });
|
3687
|
+
}
|
3688
|
+
}
|
3689
|
+
updateData = (isRunning, vm) => {
|
3690
|
+
for (let i = 0; i < vm.length; i++) {
|
3691
|
+
const message = vm[i];
|
3692
|
+
const parent = vm[i - 1];
|
3693
|
+
this.repository.addOrUpdateMessage(parent?.id ?? null, message);
|
3694
|
+
}
|
3695
|
+
if (this.assistantOptimisticId) {
|
3696
|
+
this.repository.deleteMessage(this.assistantOptimisticId);
|
3697
|
+
this.assistantOptimisticId = null;
|
3698
|
+
}
|
3699
|
+
if (hasUpcomingMessage(isRunning, vm)) {
|
3700
|
+
this.assistantOptimisticId = this.repository.appendOptimisticMessage(
|
3701
|
+
vm.at(-1)?.id ?? null,
|
3702
|
+
{
|
3703
|
+
role: "assistant",
|
3704
|
+
content: []
|
3705
|
+
}
|
3706
|
+
);
|
3707
|
+
}
|
3708
|
+
this.repository.resetHead(
|
3709
|
+
this.assistantOptimisticId ?? vm.at(-1)?.id ?? null
|
3710
|
+
);
|
3711
|
+
this.messages = this.repository.getMessages();
|
3712
|
+
this.isRunning = isRunning;
|
3713
|
+
for (const callback of this._subscriptions) callback();
|
3714
|
+
};
|
3715
|
+
unstable_synchronizer = () => {
|
3716
|
+
const { store } = this.useStore();
|
3717
|
+
useExternalStoreSync(store, this.updateData);
|
3718
|
+
return null;
|
3719
|
+
};
|
3720
|
+
addToolResult(options) {
|
3721
|
+
if (!this.store.onAddToolResult)
|
3722
|
+
throw new Error("Runtime does not support tool results.");
|
3723
|
+
this.store.onAddToolResult(options);
|
3724
|
+
}
|
3725
|
+
};
|
3726
|
+
|
3727
|
+
// src/runtimes/external-store/ExternalStoreRuntime.tsx
|
3728
|
+
var ExternalStoreRuntime = class extends BaseAssistantRuntime {
|
3729
|
+
_proxyConfigProvider = new ProxyConfigProvider();
|
3730
|
+
constructor(store) {
|
3731
|
+
super(new ExternalStoreThreadRuntime(store));
|
3732
|
+
}
|
3733
|
+
set store(store) {
|
3734
|
+
this.thread.store = store;
|
3735
|
+
}
|
3736
|
+
onStoreUpdated() {
|
3737
|
+
return this.thread.onStoreUpdated();
|
3738
|
+
}
|
3739
|
+
getModelConfig() {
|
3740
|
+
return this._proxyConfigProvider.getModelConfig();
|
3741
|
+
}
|
3742
|
+
registerModelConfigProvider(provider) {
|
3743
|
+
return this._proxyConfigProvider.registerModelConfigProvider(provider);
|
3744
|
+
}
|
3745
|
+
switchToThread(threadId) {
|
3746
|
+
if (threadId) {
|
3747
|
+
if (!this.store.onSwitchThread)
|
3748
|
+
throw new Error("Runtime does not support switching threads.");
|
3749
|
+
return this.store.onSwitchThread(threadId);
|
3750
|
+
} else {
|
3751
|
+
if (!this.store.onNewThread)
|
3752
|
+
throw new Error("Runtime does not support switching to new threads.");
|
3753
|
+
return this.store.onNewThread();
|
3754
|
+
}
|
3755
|
+
}
|
3756
|
+
};
|
3757
|
+
|
3758
|
+
// src/runtimes/external-store/useExternalStoreRuntime.tsx
|
3759
|
+
import { useEffect as useEffect12, useInsertionEffect as useInsertionEffect4, useState as useState9 } from "react";
|
3760
|
+
var useExternalStoreRuntime = (store) => {
|
3761
|
+
const [runtime] = useState9(() => new ExternalStoreRuntime(store));
|
3762
|
+
useInsertionEffect4(() => {
|
3763
|
+
runtime.store = store;
|
3764
|
+
});
|
3765
|
+
useEffect12(() => {
|
3766
|
+
runtime.onStoreUpdated();
|
3767
|
+
});
|
3768
|
+
return runtime;
|
3769
|
+
};
|
3770
|
+
|
3566
3771
|
// src/ui/thread-config.tsx
|
3567
3772
|
import { createContext as createContext5, useContext as useContext5 } from "react";
|
3568
3773
|
import { Fragment as Fragment3, jsx as jsx29 } from "react/jsx-runtime";
|
@@ -4263,6 +4468,7 @@ export {
|
|
4263
4468
|
contentPart_exports as ContentPartPrimitive,
|
4264
4469
|
EdgeChatAdapter,
|
4265
4470
|
edit_composer_default as EditComposer,
|
4471
|
+
ExternalStoreRuntime,
|
4266
4472
|
internal_exports as INTERNAL,
|
4267
4473
|
message_exports as MessagePrimitive,
|
4268
4474
|
thread_default as Thread,
|
@@ -4275,6 +4481,7 @@ export {
|
|
4275
4481
|
fromCoreMessages,
|
4276
4482
|
fromLanguageModelMessages,
|
4277
4483
|
fromLanguageModelTools,
|
4484
|
+
getExternalStoreMessage,
|
4278
4485
|
makeAssistantTool,
|
4279
4486
|
makeAssistantToolUI,
|
4280
4487
|
toCoreMessage,
|
@@ -4302,6 +4509,7 @@ export {
|
|
4302
4509
|
useContentPartImage,
|
4303
4510
|
useContentPartText,
|
4304
4511
|
useEdgeRuntime,
|
4512
|
+
useExternalStoreRuntime,
|
4305
4513
|
useLocalRuntime,
|
4306
4514
|
useMessageContext,
|
4307
4515
|
useMessageIf,
|