@assistant-ui/react 0.10.48 → 0.10.50
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/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -1
- package/dist/cloud/AssistantCloudThreadHistoryAdapter.js +7 -1
- package/dist/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -1
- package/dist/runtimes/adapters/RuntimeAdapterProvider.d.ts +2 -0
- package/dist/runtimes/adapters/RuntimeAdapterProvider.d.ts.map +1 -1
- package/dist/runtimes/adapters/RuntimeAdapterProvider.js.map +1 -1
- package/dist/runtimes/adapters/attachment/CloudFileAttachmentAdapter.d.ts +15 -0
- package/dist/runtimes/adapters/attachment/CloudFileAttachmentAdapter.d.ts.map +1 -0
- package/dist/runtimes/adapters/attachment/CloudFileAttachmentAdapter.js +83 -0
- package/dist/runtimes/adapters/attachment/CloudFileAttachmentAdapter.js.map +1 -0
- package/dist/runtimes/adapters/attachment/index.d.ts +1 -0
- package/dist/runtimes/adapters/attachment/index.d.ts.map +1 -1
- package/dist/runtimes/adapters/attachment/index.js +2 -0
- package/dist/runtimes/adapters/attachment/index.js.map +1 -1
- package/dist/runtimes/remote-thread-list/adapter/cloud.d.ts.map +1 -1
- package/dist/runtimes/remote-thread-list/adapter/cloud.js +14 -1
- package/dist/runtimes/remote-thread-list/adapter/cloud.js.map +1 -1
- package/dist/tests/setup.js +8 -8
- package/dist/tests/setup.js.map +1 -1
- package/package.json +1 -1
- package/src/cloud/AssistantCloudThreadHistoryAdapter.tsx +12 -1
- package/src/runtimes/adapters/RuntimeAdapterProvider.tsx +2 -0
- package/src/runtimes/adapters/attachment/CloudFileAttachmentAdapter.ts +101 -0
- package/src/runtimes/adapters/attachment/index.ts +1 -0
- package/src/runtimes/remote-thread-list/adapter/cloud.tsx +16 -1
package/package.json
CHANGED
@@ -14,6 +14,12 @@ import {
|
|
14
14
|
import { GenericThreadHistoryAdapter } from "../runtimes/adapters/thread-history/ThreadHistoryAdapter";
|
15
15
|
import { ReadonlyJSONObject } from "assistant-stream/utils";
|
16
16
|
|
17
|
+
// Global WeakMap to store message ID mappings across adapter instances
|
18
|
+
const globalMessageIdMapping = new WeakMap<
|
19
|
+
ThreadListItemRuntime,
|
20
|
+
Record<string, string | Promise<string>>
|
21
|
+
>();
|
22
|
+
|
17
23
|
class FormattedThreadHistoryAdapter<TMessage, TStorageFormat>
|
18
24
|
implements GenericThreadHistoryAdapter<TMessage>
|
19
25
|
{
|
@@ -52,7 +58,12 @@ class AssistantCloudThreadHistoryAdapter implements ThreadHistoryAdapter {
|
|
52
58
|
private threadListItemRuntime: ThreadListItemRuntime,
|
53
59
|
) {}
|
54
60
|
|
55
|
-
private _getIdForLocalId: Record<string, string | Promise<string>>
|
61
|
+
private get _getIdForLocalId(): Record<string, string | Promise<string>> {
|
62
|
+
if (!globalMessageIdMapping.has(this.threadListItemRuntime)) {
|
63
|
+
globalMessageIdMapping.set(this.threadListItemRuntime, {});
|
64
|
+
}
|
65
|
+
return globalMessageIdMapping.get(this.threadListItemRuntime)!;
|
66
|
+
}
|
56
67
|
|
57
68
|
withFormat<TMessage, TStorageFormat>(
|
58
69
|
formatAdapter: MessageFormatAdapter<TMessage, TStorageFormat>,
|
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
import { createContext, FC, ReactNode, useContext } from "react";
|
4
4
|
import { ThreadHistoryAdapter } from "./thread-history/ThreadHistoryAdapter";
|
5
|
+
import { AttachmentAdapter } from "./attachment/AttachmentAdapter";
|
5
6
|
import { ModelContextProvider } from "../../model-context";
|
6
7
|
|
7
8
|
export type RuntimeAdapters = {
|
8
9
|
modelContext?: ModelContextProvider;
|
9
10
|
history?: ThreadHistoryAdapter;
|
11
|
+
attachments?: AttachmentAdapter;
|
10
12
|
};
|
11
13
|
|
12
14
|
const RuntimeAdaptersContext = createContext<RuntimeAdapters | null>(null);
|
@@ -0,0 +1,101 @@
|
|
1
|
+
import type { AssistantCloud } from "assistant-cloud";
|
2
|
+
import {
|
3
|
+
Attachment,
|
4
|
+
PendingAttachment,
|
5
|
+
CompleteAttachment,
|
6
|
+
} from "../../../types/AttachmentTypes";
|
7
|
+
import { ThreadUserMessagePart } from "../../../types/MessagePartTypes";
|
8
|
+
import { AttachmentAdapter } from "./AttachmentAdapter";
|
9
|
+
|
10
|
+
const guessAttachmentType = (
|
11
|
+
contentType: string,
|
12
|
+
): "image" | "document" | "file" => {
|
13
|
+
if (contentType.startsWith("image/")) return "image";
|
14
|
+
if (contentType.startsWith("text/")) return "document";
|
15
|
+
return "file";
|
16
|
+
};
|
17
|
+
|
18
|
+
export class CloudFileAttachmentAdapter implements AttachmentAdapter {
|
19
|
+
public accept = "*";
|
20
|
+
|
21
|
+
constructor(private cloud: AssistantCloud) {}
|
22
|
+
|
23
|
+
private uploadedUrls = new Map<string, string>();
|
24
|
+
|
25
|
+
public async *add({
|
26
|
+
file,
|
27
|
+
}: {
|
28
|
+
file: File;
|
29
|
+
}): AsyncGenerator<PendingAttachment, void> {
|
30
|
+
const id = crypto.randomUUID();
|
31
|
+
const type = guessAttachmentType(file.type);
|
32
|
+
let attachment: PendingAttachment = {
|
33
|
+
id,
|
34
|
+
type,
|
35
|
+
name: file.name,
|
36
|
+
contentType: file.type,
|
37
|
+
file,
|
38
|
+
status: { type: "running", reason: "uploading", progress: 0 },
|
39
|
+
};
|
40
|
+
yield attachment;
|
41
|
+
|
42
|
+
try {
|
43
|
+
const { signedUrl, publicUrl } =
|
44
|
+
await this.cloud.files.generatePresignedUploadUrl({
|
45
|
+
filename: file.name,
|
46
|
+
});
|
47
|
+
await fetch(signedUrl, {
|
48
|
+
method: "PUT",
|
49
|
+
body: file,
|
50
|
+
headers: {
|
51
|
+
"Content-Type": file.type,
|
52
|
+
},
|
53
|
+
mode: "cors",
|
54
|
+
});
|
55
|
+
this.uploadedUrls.set(id, publicUrl);
|
56
|
+
attachment = {
|
57
|
+
...attachment,
|
58
|
+
status: { type: "requires-action", reason: "composer-send" },
|
59
|
+
};
|
60
|
+
yield attachment;
|
61
|
+
} catch {
|
62
|
+
attachment = {
|
63
|
+
...attachment,
|
64
|
+
status: { type: "incomplete", reason: "error" },
|
65
|
+
};
|
66
|
+
yield attachment;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
public async remove(attachment: Attachment): Promise<void> {
|
71
|
+
this.uploadedUrls.delete(attachment.id);
|
72
|
+
}
|
73
|
+
|
74
|
+
public async send(
|
75
|
+
attachment: PendingAttachment,
|
76
|
+
): Promise<CompleteAttachment> {
|
77
|
+
const url = this.uploadedUrls.get(attachment.id);
|
78
|
+
if (!url) throw new Error("Attachment not uploaded");
|
79
|
+
this.uploadedUrls.delete(attachment.id);
|
80
|
+
|
81
|
+
let content: ThreadUserMessagePart[];
|
82
|
+
if (attachment.type === "image") {
|
83
|
+
content = [{ type: "image", image: url, filename: attachment.name }];
|
84
|
+
} else {
|
85
|
+
content = [
|
86
|
+
{
|
87
|
+
type: "file",
|
88
|
+
data: url,
|
89
|
+
mimeType: attachment.contentType,
|
90
|
+
filename: attachment.name,
|
91
|
+
},
|
92
|
+
];
|
93
|
+
}
|
94
|
+
|
95
|
+
return {
|
96
|
+
...attachment,
|
97
|
+
status: { type: "complete" },
|
98
|
+
content,
|
99
|
+
};
|
100
|
+
}
|
101
|
+
}
|
@@ -2,3 +2,4 @@ export type { AttachmentAdapter } from "./AttachmentAdapter";
|
|
2
2
|
export { SimpleImageAttachmentAdapter } from "./SimpleImageAttachmentAdapter";
|
3
3
|
export { SimpleTextAttachmentAdapter } from "./SimpleTextAttachmentAdapter";
|
4
4
|
export { CompositeAttachmentAdapter } from "./CompositeAttachmentAdapter";
|
5
|
+
export { CloudFileAttachmentAdapter } from "./CloudFileAttachmentAdapter";
|
@@ -13,6 +13,7 @@ import { RemoteThreadListAdapter } from "../types";
|
|
13
13
|
import { useAssistantCloudThreadHistoryAdapter } from "../../../cloud/AssistantCloudThreadHistoryAdapter";
|
14
14
|
import { RuntimeAdapterProvider } from "../../adapters/RuntimeAdapterProvider";
|
15
15
|
import { InMemoryThreadListAdapter } from "./in-memory";
|
16
|
+
import { CloudFileAttachmentAdapter } from "../../adapters";
|
16
17
|
|
17
18
|
type ThreadData = {
|
18
19
|
externalId: string;
|
@@ -47,7 +48,21 @@ export const useCloudThreadListAdapter = (
|
|
47
48
|
return adapterRef.current.cloud ?? autoCloud!;
|
48
49
|
},
|
49
50
|
});
|
50
|
-
const
|
51
|
+
const attachments = useMemo(
|
52
|
+
() =>
|
53
|
+
new CloudFileAttachmentAdapter(
|
54
|
+
adapterRef.current.cloud ?? autoCloud!,
|
55
|
+
),
|
56
|
+
[adapterRef.current.cloud],
|
57
|
+
);
|
58
|
+
|
59
|
+
const adapters = useMemo(
|
60
|
+
() => ({
|
61
|
+
history,
|
62
|
+
attachments,
|
63
|
+
}),
|
64
|
+
[history],
|
65
|
+
);
|
51
66
|
|
52
67
|
return (
|
53
68
|
<RuntimeAdapterProvider adapters={adapters}>
|