@assistant-ui/react 0.7.46 → 0.7.47
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/api/AssistantRuntime.d.ts +1 -0
- package/dist/api/AssistantRuntime.d.ts.map +1 -1
- package/dist/api/AssistantRuntime.js +5 -0
- package/dist/api/AssistantRuntime.js.map +1 -1
- package/dist/api/AssistantRuntime.mjs +5 -0
- package/dist/api/AssistantRuntime.mjs.map +1 -1
- package/dist/api/AttachmentRuntime.d.ts +1 -0
- package/dist/api/AttachmentRuntime.d.ts.map +1 -1
- package/dist/api/AttachmentRuntime.js +5 -0
- package/dist/api/AttachmentRuntime.js.map +1 -1
- package/dist/api/AttachmentRuntime.mjs +5 -0
- package/dist/api/AttachmentRuntime.mjs.map +1 -1
- package/dist/api/ComposerRuntime.d.ts +2 -0
- package/dist/api/ComposerRuntime.d.ts.map +1 -1
- package/dist/api/ComposerRuntime.js +19 -0
- package/dist/api/ComposerRuntime.js.map +1 -1
- package/dist/api/ComposerRuntime.mjs +19 -0
- package/dist/api/ComposerRuntime.mjs.map +1 -1
- package/dist/api/ContentPartRuntime.d.ts +1 -0
- package/dist/api/ContentPartRuntime.d.ts.map +1 -1
- package/dist/api/ContentPartRuntime.js +5 -0
- package/dist/api/ContentPartRuntime.js.map +1 -1
- package/dist/api/ContentPartRuntime.mjs +5 -0
- package/dist/api/ContentPartRuntime.mjs.map +1 -1
- package/dist/api/MessageRuntime.d.ts +1 -0
- package/dist/api/MessageRuntime.d.ts.map +1 -1
- package/dist/api/MessageRuntime.js +13 -0
- package/dist/api/MessageRuntime.js.map +1 -1
- package/dist/api/MessageRuntime.mjs +13 -0
- package/dist/api/MessageRuntime.mjs.map +1 -1
- package/dist/api/ThreadListItemRuntime.d.ts +1 -0
- package/dist/api/ThreadListItemRuntime.d.ts.map +1 -1
- package/dist/api/ThreadListItemRuntime.js +11 -0
- package/dist/api/ThreadListItemRuntime.js.map +1 -1
- package/dist/api/ThreadListItemRuntime.mjs +11 -0
- package/dist/api/ThreadListItemRuntime.mjs.map +1 -1
- package/dist/api/ThreadListRuntime.d.ts +1 -0
- package/dist/api/ThreadListRuntime.d.ts.map +1 -1
- package/dist/api/ThreadListRuntime.js +10 -0
- package/dist/api/ThreadListRuntime.js.map +1 -1
- package/dist/api/ThreadListRuntime.mjs +10 -0
- package/dist/api/ThreadListRuntime.mjs.map +1 -1
- package/dist/api/ThreadRuntime.d.ts +1 -1
- package/dist/api/ThreadRuntime.d.ts.map +1 -1
- package/dist/api/ThreadRuntime.js +14 -3
- package/dist/api/ThreadRuntime.js.map +1 -1
- package/dist/api/ThreadRuntime.mjs +14 -3
- package/dist/api/ThreadRuntime.mjs.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.js +5 -14
- package/dist/context/providers/AssistantRuntimeProvider.js.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.mjs +5 -14
- package/dist/context/providers/AssistantRuntimeProvider.mjs.map +1 -1
- package/dist/context/providers/AttachmentRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/AttachmentRuntimeProvider.js +5 -16
- package/dist/context/providers/AttachmentRuntimeProvider.js.map +1 -1
- package/dist/context/providers/AttachmentRuntimeProvider.mjs +6 -21
- package/dist/context/providers/AttachmentRuntimeProvider.mjs.map +1 -1
- package/dist/context/providers/ContentPartRuntimeProvider.d.ts +0 -1
- package/dist/context/providers/ContentPartRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/ContentPartRuntimeProvider.js +5 -15
- package/dist/context/providers/ContentPartRuntimeProvider.js.map +1 -1
- package/dist/context/providers/ContentPartRuntimeProvider.mjs +4 -13
- package/dist/context/providers/ContentPartRuntimeProvider.mjs.map +1 -1
- package/dist/context/providers/MessageRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/MessageRuntimeProvider.js +3 -22
- package/dist/context/providers/MessageRuntimeProvider.js.map +1 -1
- package/dist/context/providers/MessageRuntimeProvider.mjs +3 -22
- package/dist/context/providers/MessageRuntimeProvider.mjs.map +1 -1
- package/dist/context/providers/TextContentPartProvider.d.ts.map +1 -1
- package/dist/context/providers/TextContentPartProvider.js +13 -12
- package/dist/context/providers/TextContentPartProvider.js.map +1 -1
- package/dist/context/providers/TextContentPartProvider.mjs +13 -12
- package/dist/context/providers/TextContentPartProvider.mjs.map +1 -1
- package/dist/context/providers/ThreadListItemRuntimeProvider.d.ts +0 -1
- package/dist/context/providers/ThreadListItemRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/ThreadListItemRuntimeProvider.js +5 -15
- package/dist/context/providers/ThreadListItemRuntimeProvider.js.map +1 -1
- package/dist/context/providers/ThreadListItemRuntimeProvider.mjs +4 -13
- package/dist/context/providers/ThreadListItemRuntimeProvider.mjs.map +1 -1
- package/dist/context/providers/ThreadRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/ThreadRuntimeProvider.js +5 -24
- package/dist/context/providers/ThreadRuntimeProvider.js.map +1 -1
- package/dist/context/providers/ThreadRuntimeProvider.mjs +6 -25
- package/dist/context/providers/ThreadRuntimeProvider.mjs.map +1 -1
- package/dist/context/react/AssistantContext.d.ts +26 -17
- package/dist/context/react/AssistantContext.d.ts.map +1 -1
- package/dist/context/react/AssistantContext.js +3 -4
- package/dist/context/react/AssistantContext.js.map +1 -1
- package/dist/context/react/AssistantContext.mjs +3 -4
- package/dist/context/react/AssistantContext.mjs.map +1 -1
- package/dist/context/react/AttachmentContext.d.ts +558 -30
- package/dist/context/react/AttachmentContext.d.ts.map +1 -1
- package/dist/context/react/AttachmentContext.js +32 -52
- package/dist/context/react/AttachmentContext.js.map +1 -1
- package/dist/context/react/AttachmentContext.mjs +33 -52
- package/dist/context/react/AttachmentContext.mjs.map +1 -1
- package/dist/context/react/ComposerContext.d.ts +28 -19
- package/dist/context/react/ComposerContext.d.ts.map +1 -1
- package/dist/context/react/ComposerContext.js +2 -19
- package/dist/context/react/ComposerContext.js.map +1 -1
- package/dist/context/react/ComposerContext.mjs +4 -20
- package/dist/context/react/ComposerContext.mjs.map +1 -1
- package/dist/context/react/ContentPartContext.d.ts +22 -16
- package/dist/context/react/ContentPartContext.d.ts.map +1 -1
- package/dist/context/react/ContentPartContext.js +2 -7
- package/dist/context/react/ContentPartContext.js.map +1 -1
- package/dist/context/react/ContentPartContext.mjs +2 -6
- package/dist/context/react/ContentPartContext.mjs.map +1 -1
- package/dist/context/react/MessageContext.d.ts +49 -29
- package/dist/context/react/MessageContext.d.ts.map +1 -1
- package/dist/context/react/MessageContext.js +5 -9
- package/dist/context/react/MessageContext.js.map +1 -1
- package/dist/context/react/MessageContext.mjs +5 -8
- package/dist/context/react/MessageContext.mjs.map +1 -1
- package/dist/context/react/ThreadContext.d.ts +44 -24
- package/dist/context/react/ThreadContext.d.ts.map +1 -1
- package/dist/context/react/ThreadContext.js +5 -9
- package/dist/context/react/ThreadContext.js.map +1 -1
- package/dist/context/react/ThreadContext.mjs +5 -8
- package/dist/context/react/ThreadContext.mjs.map +1 -1
- package/dist/context/react/ThreadListItemContext.d.ts +22 -16
- package/dist/context/react/ThreadListItemContext.d.ts.map +1 -1
- package/dist/context/react/ThreadListItemContext.js +3 -6
- package/dist/context/react/ThreadListItemContext.js.map +1 -1
- package/dist/context/react/ThreadListItemContext.mjs +3 -5
- package/dist/context/react/ThreadListItemContext.mjs.map +1 -1
- package/dist/context/react/index.d.ts +1 -0
- package/dist/context/react/index.d.ts.map +1 -1
- package/dist/context/react/index.js +3 -0
- package/dist/context/react/index.js.map +1 -1
- package/dist/context/react/index.mjs +2 -0
- package/dist/context/react/index.mjs.map +1 -1
- package/dist/context/react/utils/createContextHook.d.ts +3 -8
- package/dist/context/react/utils/createContextHook.d.ts.map +1 -1
- package/dist/context/react/utils/createContextHook.js.map +1 -1
- package/dist/context/react/utils/createContextHook.mjs.map +1 -1
- package/dist/context/react/utils/createStateHookForRuntime.d.ts +31 -0
- package/dist/context/react/utils/createStateHookForRuntime.d.ts.map +1 -0
- package/dist/context/react/utils/createStateHookForRuntime.js +47 -0
- package/dist/context/react/utils/createStateHookForRuntime.js.map +1 -0
- package/dist/context/react/utils/createStateHookForRuntime.mjs +24 -0
- package/dist/context/react/utils/createStateHookForRuntime.mjs.map +1 -0
- package/dist/context/react/utils/ensureBinding.d.ts +2 -0
- package/dist/context/react/utils/ensureBinding.d.ts.map +1 -0
- package/dist/context/react/utils/ensureBinding.js +36 -0
- package/dist/context/react/utils/ensureBinding.js.map +1 -0
- package/dist/context/react/utils/ensureBinding.mjs +11 -0
- package/dist/context/react/utils/ensureBinding.mjs.map +1 -0
- package/dist/context/react/utils/useRuntimeState.d.ts +10 -0
- package/dist/context/react/utils/useRuntimeState.d.ts.map +1 -0
- package/dist/context/react/utils/useRuntimeState.js +48 -0
- package/dist/context/react/utils/useRuntimeState.js.map +1 -0
- package/dist/context/react/utils/useRuntimeState.mjs +22 -0
- package/dist/context/react/utils/useRuntimeState.mjs.map +1 -0
- package/dist/runtimes/adapters/attachment/AttachmentAdapter.d.ts +1 -1
- package/dist/runtimes/adapters/attachment/AttachmentAdapter.d.ts.map +1 -1
- package/dist/runtimes/adapters/attachment/AttachmentAdapter.js.map +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.d.ts +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.d.ts.map +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.js +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.js.map +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.mjs +1 -1
- package/dist/runtimes/adapters/attachment/CompositeAttachmentAdapter.mjs.map +1 -1
- package/dist/runtimes/adapters/thread-history/ThreadHistoryAdapter.d.ts.map +1 -1
- package/dist/runtimes/adapters/thread-history/ThreadHistoryAdapter.js.map +1 -1
- package/dist/runtimes/composer/BaseComposerRuntimeCore.d.ts.map +1 -1
- package/dist/runtimes/composer/BaseComposerRuntimeCore.js +22 -2
- package/dist/runtimes/composer/BaseComposerRuntimeCore.js.map +1 -1
- package/dist/runtimes/composer/BaseComposerRuntimeCore.mjs +22 -2
- package/dist/runtimes/composer/BaseComposerRuntimeCore.mjs.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.d.ts.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.js +4 -0
- package/dist/runtimes/local/useLocalRuntime.js.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.mjs +4 -0
- package/dist/runtimes/local/useLocalRuntime.mjs.map +1 -1
- package/package.json +3 -3
- package/src/api/AssistantRuntime.ts +7 -0
- package/src/api/AttachmentRuntime.ts +6 -0
- package/src/api/ComposerRuntime.ts +21 -0
- package/src/api/ContentPartRuntime.ts +6 -0
- package/src/api/MessageRuntime.ts +15 -0
- package/src/api/ThreadListItemRuntime.ts +12 -0
- package/src/api/ThreadListRuntime.ts +11 -0
- package/src/api/ThreadRuntime.ts +15 -4
- package/src/context/providers/AssistantRuntimeProvider.tsx +5 -17
- package/src/context/providers/AttachmentRuntimeProvider.tsx +6 -24
- package/src/context/providers/ContentPartRuntimeProvider.tsx +4 -14
- package/src/context/providers/MessageRuntimeProvider.tsx +5 -31
- package/src/context/providers/TextContentPartProvider.tsx +26 -22
- package/src/context/providers/ThreadListItemRuntimeProvider.tsx +3 -14
- package/src/context/providers/ThreadRuntimeProvider.tsx +7 -33
- package/src/context/react/AssistantContext.ts +7 -7
- package/src/context/react/AttachmentContext.ts +45 -136
- package/src/context/react/ComposerContext.ts +6 -29
- package/src/context/react/ContentPartContext.ts +3 -8
- package/src/context/react/MessageContext.ts +10 -13
- package/src/context/react/ThreadContext.ts +9 -12
- package/src/context/react/ThreadListItemContext.ts +4 -7
- package/src/context/react/index.ts +2 -0
- package/src/context/react/utils/createContextHook.ts +1 -3
- package/src/context/react/utils/createStateHookForRuntime.ts +80 -0
- package/src/context/react/utils/ensureBinding.ts +45 -0
- package/src/context/react/utils/useRuntimeState.ts +43 -0
- package/src/runtimes/adapters/attachment/AttachmentAdapter.ts +3 -1
- package/src/runtimes/adapters/attachment/CompositeAttachmentAdapter.ts +1 -1
- package/src/runtimes/adapters/thread-history/ThreadHistoryAdapter.ts +1 -0
- package/src/runtimes/composer/BaseComposerRuntimeCore.tsx +24 -2
- package/src/runtimes/local/useLocalRuntime.tsx +5 -0
@@ -12,11 +12,9 @@ export function createContextHook<T>(
|
|
12
12
|
context: Context<T | null>,
|
13
13
|
providerName: string,
|
14
14
|
) {
|
15
|
-
function useContextHook(options?: { optional?: false | undefined }): T;
|
16
15
|
function useContextHook(options?: {
|
17
16
|
optional?: boolean | undefined;
|
18
|
-
}): T | null
|
19
|
-
function useContextHook(options?: { optional?: boolean | undefined }) {
|
17
|
+
}): T | null {
|
20
18
|
const contextValue = useContext(context);
|
21
19
|
if (!options?.optional && !contextValue) {
|
22
20
|
throw new Error(`This component must be used within ${providerName}.`);
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import {
|
2
|
+
SubscribableRuntime,
|
3
|
+
useRuntimeStateInternal,
|
4
|
+
} from "./useRuntimeState";
|
5
|
+
|
6
|
+
export function createStateHookForRuntime<TState>(
|
7
|
+
useRuntime: (options: {
|
8
|
+
optional: boolean | undefined;
|
9
|
+
}) => SubscribableRuntime<TState> | null,
|
10
|
+
) {
|
11
|
+
// empty
|
12
|
+
function useStoreHook(): TState;
|
13
|
+
|
14
|
+
// selector
|
15
|
+
function useStoreHook<TSelected>(
|
16
|
+
selector: (state: TState) => TSelected,
|
17
|
+
): TSelected;
|
18
|
+
|
19
|
+
// selector?
|
20
|
+
function useStoreHook<TSelected>(
|
21
|
+
selector: ((state: TState) => TSelected) | undefined,
|
22
|
+
): TSelected | TState;
|
23
|
+
|
24
|
+
// optional=false
|
25
|
+
function useStoreHook(options: { optional?: false | undefined }): TState;
|
26
|
+
|
27
|
+
// optional?
|
28
|
+
function useStoreHook(options: {
|
29
|
+
optional?: boolean | undefined;
|
30
|
+
}): TState | null;
|
31
|
+
|
32
|
+
// optional=false, selector
|
33
|
+
function useStoreHook<TSelected>(options: {
|
34
|
+
optional?: false | undefined;
|
35
|
+
selector: (state: TState) => TSelected;
|
36
|
+
}): TSelected;
|
37
|
+
|
38
|
+
// optional=false, selector?
|
39
|
+
function useStoreHook<TSelected>(options: {
|
40
|
+
optional?: false | undefined;
|
41
|
+
selector: ((state: TState) => TSelected) | undefined;
|
42
|
+
}): TSelected | TState;
|
43
|
+
|
44
|
+
// optional?, selector
|
45
|
+
function useStoreHook<TSelected>(options: {
|
46
|
+
optional?: boolean | undefined;
|
47
|
+
selector: (state: TState) => TSelected;
|
48
|
+
}): TSelected | null;
|
49
|
+
|
50
|
+
// optional?, selector?
|
51
|
+
function useStoreHook<TSelected>(options: {
|
52
|
+
optional?: boolean | undefined;
|
53
|
+
selector: ((state: TState) => TSelected) | undefined;
|
54
|
+
}): TSelected | TState | null;
|
55
|
+
|
56
|
+
function useStoreHook<TSelected>(
|
57
|
+
param?:
|
58
|
+
| ((state: TState) => TSelected)
|
59
|
+
| {
|
60
|
+
optional?: boolean | undefined;
|
61
|
+
selector?: ((state: TState) => TSelected) | undefined;
|
62
|
+
},
|
63
|
+
): TSelected | TState | null {
|
64
|
+
let optional = false;
|
65
|
+
let selector: ((state: TState) => TSelected) | undefined;
|
66
|
+
|
67
|
+
if (typeof param === "function") {
|
68
|
+
selector = param;
|
69
|
+
} else if (param) {
|
70
|
+
optional = !!param.optional;
|
71
|
+
selector = param.selector;
|
72
|
+
}
|
73
|
+
|
74
|
+
const store = useRuntime({ optional });
|
75
|
+
if (!store) return null;
|
76
|
+
return useRuntimeStateInternal(store, selector);
|
77
|
+
}
|
78
|
+
|
79
|
+
return useStoreHook;
|
80
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
type Bindable = {
|
2
|
+
__internal_bindMethods?: () => void;
|
3
|
+
__isBound?: boolean;
|
4
|
+
};
|
5
|
+
const debugVerifyPrototype = (
|
6
|
+
runtime: Record<string, unknown>,
|
7
|
+
prototype: any,
|
8
|
+
) => {
|
9
|
+
const unboundMethods = Object.getOwnPropertyNames(prototype).filter(
|
10
|
+
(methodStr) => {
|
11
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, methodStr);
|
12
|
+
const isMethod = descriptor && typeof descriptor.value === "function";
|
13
|
+
if (!isMethod) return false;
|
14
|
+
|
15
|
+
const methodName = methodStr as keyof typeof runtime | "constructor";
|
16
|
+
return (
|
17
|
+
isMethod &&
|
18
|
+
!methodName.startsWith("_") &&
|
19
|
+
methodName !== "constructor" &&
|
20
|
+
prototype[methodName] === runtime[methodName]
|
21
|
+
);
|
22
|
+
},
|
23
|
+
);
|
24
|
+
|
25
|
+
if (unboundMethods.length > 0) {
|
26
|
+
throw new Error(
|
27
|
+
"The following methods are not bound: " + JSON.stringify(unboundMethods),
|
28
|
+
);
|
29
|
+
}
|
30
|
+
|
31
|
+
const prototypePrototype = Object.getPrototypeOf(prototype);
|
32
|
+
if (prototypePrototype && prototypePrototype !== Object.prototype) {
|
33
|
+
debugVerifyPrototype(runtime, prototypePrototype);
|
34
|
+
}
|
35
|
+
};
|
36
|
+
export const ensureBinding = (r: unknown) => {
|
37
|
+
const runtime = r as Bindable;
|
38
|
+
if (runtime.__isBound) return;
|
39
|
+
|
40
|
+
runtime.__internal_bindMethods?.();
|
41
|
+
runtime.__isBound = true;
|
42
|
+
|
43
|
+
// @ts-ignore - strip this out in production build
|
44
|
+
DEV: debugVerifyPrototype(runtime, Object.getPrototypeOf(runtime));
|
45
|
+
};
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import { useDebugValue, useSyncExternalStore } from "react";
|
2
|
+
import { Unsubscribe } from "../../../types";
|
3
|
+
import { ensureBinding } from "./ensureBinding";
|
4
|
+
|
5
|
+
export type SubscribableRuntime<TState> = {
|
6
|
+
getState: () => TState;
|
7
|
+
subscribe: (callback: () => void) => Unsubscribe;
|
8
|
+
};
|
9
|
+
|
10
|
+
export function useRuntimeStateInternal<TState, TSelected>(
|
11
|
+
runtime: SubscribableRuntime<TState>,
|
12
|
+
selector: ((state: TState) => TSelected) | undefined = identity as any,
|
13
|
+
): TSelected | TState {
|
14
|
+
const slice = useSyncExternalStore(
|
15
|
+
runtime.subscribe,
|
16
|
+
() => selector(runtime.getState()),
|
17
|
+
() => selector(runtime.getState()),
|
18
|
+
);
|
19
|
+
useDebugValue(slice);
|
20
|
+
return slice;
|
21
|
+
}
|
22
|
+
|
23
|
+
const identity = <T>(arg: T): T => arg;
|
24
|
+
export function useRuntimeState<TState>(
|
25
|
+
runtime: SubscribableRuntime<TState>,
|
26
|
+
): TState;
|
27
|
+
export function useRuntimeState<TState, TSelected>(
|
28
|
+
runtime: SubscribableRuntime<TState>,
|
29
|
+
selector: (state: TState) => TSelected,
|
30
|
+
): TSelected;
|
31
|
+
export function useRuntimeState<TState, TSelected>(
|
32
|
+
runtime: SubscribableRuntime<TState>,
|
33
|
+
selector: ((state: TState) => TSelected) | undefined,
|
34
|
+
): TSelected | TState;
|
35
|
+
export function useRuntimeState<TState, TSelected>(
|
36
|
+
runtime: SubscribableRuntime<TState>,
|
37
|
+
selector?: ((state: TState) => TSelected) | undefined,
|
38
|
+
): TSelected | TState {
|
39
|
+
// ensure that the runtime is bound
|
40
|
+
ensureBinding(runtime);
|
41
|
+
|
42
|
+
return useRuntimeStateInternal(runtime, selector);
|
43
|
+
}
|
@@ -7,7 +7,9 @@ import {
|
|
7
7
|
export type AttachmentAdapter = {
|
8
8
|
accept: string;
|
9
9
|
|
10
|
-
add(state: {
|
10
|
+
add(state: {
|
11
|
+
file: File;
|
12
|
+
}): Promise<PendingAttachment> | AsyncGenerator<PendingAttachment, void>;
|
11
13
|
remove(attachment: Attachment): Promise<void>;
|
12
14
|
send(attachment: PendingAttachment): Promise<CompleteAttachment>;
|
13
15
|
};
|
@@ -65,7 +65,7 @@ export class CompositeAttachmentAdapter implements AttachmentAdapter {
|
|
65
65
|
}
|
66
66
|
}
|
67
67
|
|
68
|
-
public
|
68
|
+
public add(state: { file: File }) {
|
69
69
|
for (const adapter of this._adapters) {
|
70
70
|
if (fileMatchesAccept(state.file, adapter.accept)) {
|
71
71
|
return adapter.add(state);
|
@@ -3,6 +3,7 @@ import {
|
|
3
3
|
ExportedMessageRepositoryItem,
|
4
4
|
} from "../../utils/MessageRepository";
|
5
5
|
|
6
|
+
// TODO external store - which version to save? how to restore?
|
6
7
|
export type ThreadHistoryAdapter = {
|
7
8
|
load(): Promise<ExportedMessageRepository>;
|
8
9
|
append(item: ExportedMessageRepositoryItem): Promise<void>;
|
@@ -158,9 +158,31 @@ export abstract class BaseComposerRuntimeCore
|
|
158
158
|
const adapter = this.getAttachmentAdapter();
|
159
159
|
if (!adapter) throw new Error("Attachments are not supported");
|
160
160
|
|
161
|
-
const
|
161
|
+
const upsertAttachment = (a: PendingAttachment) => {
|
162
|
+
const idx = this._attachments.findIndex((attachment) => attachment.id === a.id);
|
163
|
+
if (idx !== -1)
|
164
|
+
this._attachments = [
|
165
|
+
...this._attachments.slice(0, idx),
|
166
|
+
a,
|
167
|
+
...this._attachments.slice(idx + 1),
|
168
|
+
];
|
169
|
+
else {
|
170
|
+
this._attachments = [...this._attachments, a];
|
171
|
+
this._notifyEventSubscribers("attachment_add");
|
172
|
+
}
|
173
|
+
|
174
|
+
this._notifySubscribers();
|
175
|
+
};
|
176
|
+
|
177
|
+
const promiseOrGenerator = adapter.add({ file });
|
178
|
+
if (Symbol.asyncIterator in promiseOrGenerator) {
|
179
|
+
for await (const r of promiseOrGenerator) {
|
180
|
+
upsertAttachment(r);
|
181
|
+
}
|
182
|
+
} else {
|
183
|
+
upsertAttachment(await promiseOrGenerator);
|
184
|
+
}
|
162
185
|
|
163
|
-
this._attachments = [...this._attachments, attachment as PendingAttachment];
|
164
186
|
this._notifyEventSubscribers("attachment_add");
|
165
187
|
this._notifySubscribers();
|
166
188
|
}
|
@@ -19,6 +19,11 @@ class LocalRuntimeImpl extends AssistantRuntimeImpl implements LocalRuntime {
|
|
19
19
|
super(core, ThreadRuntimeImpl);
|
20
20
|
}
|
21
21
|
|
22
|
+
public override __internal_bindMethods() {
|
23
|
+
super.__internal_bindMethods();
|
24
|
+
this.reset = this.reset.bind(this);
|
25
|
+
}
|
26
|
+
|
22
27
|
public reset(options?: Parameters<LocalRuntimeCore["reset"]>[0]) {
|
23
28
|
this.core.reset(options);
|
24
29
|
}
|