@shaxpir/duiduidui-models 1.17.3 → 1.19.0
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/models/Chat.d.ts +87 -0
- package/dist/models/Chat.js +122 -0
- package/dist/models/Content.d.ts +2 -1
- package/dist/models/ContentKind.d.ts +1 -0
- package/dist/models/ContentKind.js +1 -0
- package/dist/models/Manifest.d.ts +1 -0
- package/dist/models/Manifest.js +1 -0
- package/dist/models/Workspace.d.ts +3 -0
- package/dist/models/Workspace.js +8 -0
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.js +1 -0
- package/dist/util/index.d.ts +0 -1
- package/dist/util/index.js +0 -1
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Doc } from '@shaxpir/sharedb/lib/client';
|
|
2
|
+
import { CompactDateTime, MultiTime } from '@shaxpir/shaxpir-common';
|
|
3
|
+
import { ShareSync } from '../repo';
|
|
4
|
+
import { ArrayView } from './ArrayView';
|
|
5
|
+
import { Content, ContentBody, ContentId, ContentMeta } from './Content';
|
|
6
|
+
export interface TextBlock {
|
|
7
|
+
type: 'text';
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ImageBlock {
|
|
11
|
+
type: 'image';
|
|
12
|
+
media_id: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ToolUseBlock {
|
|
15
|
+
type: 'tool_use';
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
input: any;
|
|
19
|
+
}
|
|
20
|
+
export interface ToolResultBlock {
|
|
21
|
+
type: 'tool_result';
|
|
22
|
+
tool_use_id: string;
|
|
23
|
+
content: string;
|
|
24
|
+
is_error?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface ThinkingBlock {
|
|
27
|
+
type: 'thinking';
|
|
28
|
+
thinking: string;
|
|
29
|
+
}
|
|
30
|
+
export type ContentBlock = TextBlock | ImageBlock | ToolUseBlock | ToolResultBlock | ThinkingBlock;
|
|
31
|
+
export interface CacheBreakpoint {
|
|
32
|
+
ttl: 'ephemeral' | 'standard';
|
|
33
|
+
created_at: CompactDateTime;
|
|
34
|
+
expires_at: CompactDateTime;
|
|
35
|
+
}
|
|
36
|
+
export interface ChatUsage {
|
|
37
|
+
input_tokens?: number;
|
|
38
|
+
output_tokens?: number;
|
|
39
|
+
cache_creation_input_tokens?: number;
|
|
40
|
+
cache_read_input_tokens?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface ChatMessage {
|
|
43
|
+
role: 'user' | 'assistant';
|
|
44
|
+
content: ContentBlock[];
|
|
45
|
+
timestamp: CompactDateTime;
|
|
46
|
+
system_prompt_hash?: string;
|
|
47
|
+
model?: string;
|
|
48
|
+
usage?: ChatUsage;
|
|
49
|
+
duration_ms?: number;
|
|
50
|
+
stop_reason?: string;
|
|
51
|
+
cache_breakpoint?: CacheBreakpoint;
|
|
52
|
+
}
|
|
53
|
+
export interface ChatScratchpad {
|
|
54
|
+
text?: string;
|
|
55
|
+
media_ids?: string[];
|
|
56
|
+
model?: string;
|
|
57
|
+
extended_thinking?: boolean;
|
|
58
|
+
[key: string]: any;
|
|
59
|
+
}
|
|
60
|
+
export interface ChatPayload {
|
|
61
|
+
title?: string;
|
|
62
|
+
messages: ChatMessage[];
|
|
63
|
+
scratchpad: ChatScratchpad;
|
|
64
|
+
}
|
|
65
|
+
export interface ChatBody extends ContentBody {
|
|
66
|
+
meta: ContentMeta;
|
|
67
|
+
payload: ChatPayload;
|
|
68
|
+
}
|
|
69
|
+
export declare class Chat extends Content {
|
|
70
|
+
private _messagesView;
|
|
71
|
+
constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
|
|
72
|
+
static makeChatId(userId: ContentId, chatKey: string): ContentId;
|
|
73
|
+
static createDefaultPayload(): ChatPayload;
|
|
74
|
+
static create(userId: ContentId, chatKey: string, payload?: ChatPayload, createdAt?: MultiTime): Chat;
|
|
75
|
+
get payload(): ChatPayload;
|
|
76
|
+
get title(): string | undefined;
|
|
77
|
+
get messages(): ArrayView<ChatMessage>;
|
|
78
|
+
get scratchpad(): ChatScratchpad;
|
|
79
|
+
setTitle(title: string): void;
|
|
80
|
+
setScratchpad(scratchpad: ChatScratchpad): void;
|
|
81
|
+
updateScratchpadField(key: string, value: any): void;
|
|
82
|
+
clearScratchpad(): void;
|
|
83
|
+
appendMessage(message: ChatMessage): void;
|
|
84
|
+
setBreakpoint(messageIndex: number, ttl: 'ephemeral' | 'standard'): void;
|
|
85
|
+
removeBreakpoint(messageIndex: number): void;
|
|
86
|
+
clearExpiredBreakpoints(): void;
|
|
87
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Chat = void 0;
|
|
4
|
+
const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
|
|
5
|
+
const repo_1 = require("../repo");
|
|
6
|
+
const ArrayView_1 = require("./ArrayView");
|
|
7
|
+
const Content_1 = require("./Content");
|
|
8
|
+
const ContentKind_1 = require("./ContentKind");
|
|
9
|
+
const Operation_1 = require("./Operation");
|
|
10
|
+
// ---- Chat model ----
|
|
11
|
+
class Chat extends Content_1.Content {
|
|
12
|
+
constructor(doc, shouldAcquire, shareSync) {
|
|
13
|
+
super(doc, shouldAcquire, shareSync);
|
|
14
|
+
this._messagesView = new ArrayView_1.ArrayView(this, ['payload', 'messages']);
|
|
15
|
+
}
|
|
16
|
+
// ---- ID generation ----
|
|
17
|
+
static makeChatId(userId, chatKey) {
|
|
18
|
+
return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(`${ContentKind_1.ContentKind.CHAT}-${userId}-${chatKey}`);
|
|
19
|
+
}
|
|
20
|
+
// ---- Factory ----
|
|
21
|
+
static createDefaultPayload() {
|
|
22
|
+
return {
|
|
23
|
+
messages: [],
|
|
24
|
+
scratchpad: {},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
static create(userId, chatKey, payload, createdAt) {
|
|
28
|
+
const now = shaxpir_common_1.ClockService.getClock().now();
|
|
29
|
+
createdAt ??= now;
|
|
30
|
+
payload ??= Chat.createDefaultPayload();
|
|
31
|
+
const chatId = Chat.makeChatId(userId, chatKey);
|
|
32
|
+
return repo_1.ShareSyncFactory.get().createContent({
|
|
33
|
+
meta: {
|
|
34
|
+
ref: chatId,
|
|
35
|
+
kind: ContentKind_1.ContentKind.CHAT,
|
|
36
|
+
id: chatId,
|
|
37
|
+
owner: userId,
|
|
38
|
+
created_at: createdAt,
|
|
39
|
+
updated_at: createdAt
|
|
40
|
+
},
|
|
41
|
+
payload
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
// ---- Getters ----
|
|
45
|
+
get payload() {
|
|
46
|
+
return this.doc.data.payload;
|
|
47
|
+
}
|
|
48
|
+
get title() {
|
|
49
|
+
this.checkDisposed('Chat.title');
|
|
50
|
+
return this.payload.title;
|
|
51
|
+
}
|
|
52
|
+
get messages() {
|
|
53
|
+
this.checkDisposed('Chat.messages');
|
|
54
|
+
return this._messagesView;
|
|
55
|
+
}
|
|
56
|
+
get scratchpad() {
|
|
57
|
+
this.checkDisposed('Chat.scratchpad');
|
|
58
|
+
return this.payload.scratchpad;
|
|
59
|
+
}
|
|
60
|
+
// ---- Setters ----
|
|
61
|
+
setTitle(title) {
|
|
62
|
+
this.checkDisposed('Chat.setTitle');
|
|
63
|
+
const batch = new Operation_1.BatchOperation(this);
|
|
64
|
+
batch.setPathValue(['payload', 'title'], title);
|
|
65
|
+
batch.commit();
|
|
66
|
+
}
|
|
67
|
+
setScratchpad(scratchpad) {
|
|
68
|
+
this.checkDisposed('Chat.setScratchpad');
|
|
69
|
+
const batch = new Operation_1.BatchOperation(this);
|
|
70
|
+
batch.setPathValue(['payload', 'scratchpad'], scratchpad);
|
|
71
|
+
batch.commit();
|
|
72
|
+
}
|
|
73
|
+
updateScratchpadField(key, value) {
|
|
74
|
+
this.checkDisposed('Chat.updateScratchpadField');
|
|
75
|
+
const batch = new Operation_1.BatchOperation(this);
|
|
76
|
+
batch.setPathValue(['payload', 'scratchpad', key], value);
|
|
77
|
+
batch.commit();
|
|
78
|
+
}
|
|
79
|
+
clearScratchpad() {
|
|
80
|
+
this.checkDisposed('Chat.clearScratchpad');
|
|
81
|
+
const batch = new Operation_1.BatchOperation(this);
|
|
82
|
+
batch.setPathValue(['payload', 'scratchpad'], {});
|
|
83
|
+
batch.commit();
|
|
84
|
+
}
|
|
85
|
+
// ---- Message operations ----
|
|
86
|
+
appendMessage(message) {
|
|
87
|
+
this.checkDisposed('Chat.appendMessage');
|
|
88
|
+
this._messagesView.push(message);
|
|
89
|
+
}
|
|
90
|
+
// ---- Cache breakpoint operations ----
|
|
91
|
+
setBreakpoint(messageIndex, ttl) {
|
|
92
|
+
this.checkDisposed('Chat.setBreakpoint');
|
|
93
|
+
if (messageIndex < 0 || messageIndex >= this._messagesView.length)
|
|
94
|
+
return;
|
|
95
|
+
const now = shaxpir_common_1.ClockService.getClock().utc();
|
|
96
|
+
const durationMinutes = ttl === 'ephemeral' ? 5 : 60;
|
|
97
|
+
const expiresAt = shaxpir_common_1.Time.plus(now, durationMinutes, 'minutes');
|
|
98
|
+
const breakpoint = {
|
|
99
|
+
ttl,
|
|
100
|
+
created_at: now,
|
|
101
|
+
expires_at: expiresAt,
|
|
102
|
+
};
|
|
103
|
+
this._messagesView.setObjectValueAtIndex(messageIndex, 'cache_breakpoint', breakpoint);
|
|
104
|
+
}
|
|
105
|
+
removeBreakpoint(messageIndex) {
|
|
106
|
+
this.checkDisposed('Chat.removeBreakpoint');
|
|
107
|
+
if (messageIndex < 0 || messageIndex >= this._messagesView.length)
|
|
108
|
+
return;
|
|
109
|
+
this._messagesView.removeObjectPropertyAtIndex(messageIndex, 'cache_breakpoint');
|
|
110
|
+
}
|
|
111
|
+
clearExpiredBreakpoints() {
|
|
112
|
+
this.checkDisposed('Chat.clearExpiredBreakpoints');
|
|
113
|
+
const now = shaxpir_common_1.ClockService.getClock().utc();
|
|
114
|
+
for (let i = 0; i < this._messagesView.length; i++) {
|
|
115
|
+
const message = this._messagesView.get(i);
|
|
116
|
+
if (message.cache_breakpoint && shaxpir_common_1.Time.isDateTimeBefore(message.cache_breakpoint.expires_at, now)) {
|
|
117
|
+
this._messagesView.removeObjectPropertyAtIndex(i, 'cache_breakpoint');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.Chat = Chat;
|
package/dist/models/Content.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Doc } from '@shaxpir/sharedb/lib/client';
|
|
|
2
2
|
import { CompactDateTime, MultiTime } from '@shaxpir/shaxpir-common';
|
|
3
3
|
import { ShareSync } from '../repo';
|
|
4
4
|
import { BillingPayload } from './Billing';
|
|
5
|
+
import { ChatPayload } from './Chat';
|
|
5
6
|
import { CollectionPayload } from './Collection';
|
|
6
7
|
import { ContentKind } from './ContentKind';
|
|
7
8
|
import { DevicePayload } from './Device';
|
|
@@ -38,7 +39,7 @@ export interface ContentMeta {
|
|
|
38
39
|
created_at: MultiTime;
|
|
39
40
|
updated_at: MultiTime;
|
|
40
41
|
}
|
|
41
|
-
export type ContentPayload = BillingPayload | CollectionPayload | DevicePayload | ImagePayload | MetricPayload | ProfilePayload | ProgressPayload | SessionPayload | SocialPayload | TermPayload | UserPayload | WorkspacePayload;
|
|
42
|
+
export type ContentPayload = BillingPayload | ChatPayload | CollectionPayload | DevicePayload | ImagePayload | MetricPayload | ProfilePayload | ProgressPayload | SessionPayload | SocialPayload | TermPayload | UserPayload | WorkspacePayload;
|
|
42
43
|
export declare abstract class Content extends Model {
|
|
43
44
|
static ID_LENGTH: number;
|
|
44
45
|
constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
|
|
@@ -6,6 +6,7 @@ var ContentKind;
|
|
|
6
6
|
// No models yet for these, but they're definitely coming
|
|
7
7
|
ContentKind["BILLING"] = "billing";
|
|
8
8
|
// These are all the current Content subclasses
|
|
9
|
+
ContentKind["CHAT"] = "chat";
|
|
9
10
|
ContentKind["COLLECTION"] = "collection";
|
|
10
11
|
ContentKind["DEVICE"] = "device";
|
|
11
12
|
ContentKind["METRIC"] = "metric";
|
package/dist/models/Manifest.js
CHANGED
|
@@ -14,6 +14,7 @@ export interface WorkspacePayload {
|
|
|
14
14
|
devices: ContentId[];
|
|
15
15
|
sessions: MultiTime[];
|
|
16
16
|
collections: string[];
|
|
17
|
+
chats: ContentId[];
|
|
17
18
|
journey: {
|
|
18
19
|
[key: string]: CompactDateTime;
|
|
19
20
|
};
|
|
@@ -29,6 +30,7 @@ export declare class Workspace extends Content {
|
|
|
29
30
|
private _devicesView;
|
|
30
31
|
private _sessionsView;
|
|
31
32
|
private _collectionsView;
|
|
33
|
+
private _chatsView;
|
|
32
34
|
private _uploadedAvatarsView;
|
|
33
35
|
constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
|
|
34
36
|
get payload(): WorkspacePayload;
|
|
@@ -42,6 +44,7 @@ export declare class Workspace extends Content {
|
|
|
42
44
|
static create(userId: ContentId, payload: WorkspacePayload): Workspace;
|
|
43
45
|
get sessions(): ArrayView<MultiTime>;
|
|
44
46
|
get collections(): ArrayView<string>;
|
|
47
|
+
get chats(): ArrayView<ContentId>;
|
|
45
48
|
get devices(): ArrayView<ContentId>;
|
|
46
49
|
get uploadedAvatars(): ArrayView<ContentId>;
|
|
47
50
|
acquireSessionForUtcTime(utcTime: CompactDateTime): Promise<Session | null>;
|
package/dist/models/Workspace.js
CHANGED
|
@@ -14,6 +14,7 @@ class Workspace extends Content_1.Content {
|
|
|
14
14
|
this._devicesView = new ArrayView_1.ArrayView(this, ['payload', 'devices']);
|
|
15
15
|
this._sessionsView = new ArrayView_1.ArrayView(this, ['payload', 'sessions']);
|
|
16
16
|
this._collectionsView = new ArrayView_1.ArrayView(this, ['payload', 'collections']);
|
|
17
|
+
this._chatsView = new ArrayView_1.ArrayView(this, ['payload', 'chats']);
|
|
17
18
|
this._uploadedAvatarsView = new ArrayView_1.ArrayView(this, ['payload', 'uploaded_avatars']);
|
|
18
19
|
}
|
|
19
20
|
get payload() {
|
|
@@ -43,6 +44,9 @@ class Workspace extends Content_1.Content {
|
|
|
43
44
|
if (!Array.isArray(payload.collections)) {
|
|
44
45
|
throw new Error('Workspace.create: payload.collections must be an array');
|
|
45
46
|
}
|
|
47
|
+
if (!Array.isArray(payload.chats)) {
|
|
48
|
+
throw new Error('Workspace.create: payload.chats must be an array');
|
|
49
|
+
}
|
|
46
50
|
if (!payload.journey || typeof payload.journey !== 'object' || Array.isArray(payload.journey)) {
|
|
47
51
|
throw new Error('Workspace.create: payload.journey must be an object');
|
|
48
52
|
}
|
|
@@ -77,6 +81,10 @@ class Workspace extends Content_1.Content {
|
|
|
77
81
|
this.checkDisposed("Workspace.collections");
|
|
78
82
|
return this._collectionsView;
|
|
79
83
|
}
|
|
84
|
+
get chats() {
|
|
85
|
+
this.checkDisposed("Workspace.chats");
|
|
86
|
+
return this._chatsView;
|
|
87
|
+
}
|
|
80
88
|
// NOTE: As the user adds devices, we will always add new devices to the end of the array.
|
|
81
89
|
// So the most-recently-added device will always be at the end.
|
|
82
90
|
get devices() {
|
package/dist/models/index.d.ts
CHANGED
package/dist/models/index.js
CHANGED
|
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
__exportStar(require("./ArrayView"), exports);
|
|
19
19
|
__exportStar(require("./BayesianScore"), exports);
|
|
20
20
|
__exportStar(require("./Billing"), exports);
|
|
21
|
+
__exportStar(require("./Chat"), exports);
|
|
21
22
|
__exportStar(require("./CognitiveLoad"), exports);
|
|
22
23
|
__exportStar(require("./ChangeModel"), exports);
|
|
23
24
|
__exportStar(require("./Collection"), exports);
|
package/dist/util/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from './ConditionMatcher';
|
|
|
3
3
|
export * from './Database';
|
|
4
4
|
export * from './DifficultyRange';
|
|
5
5
|
export * from './Encryption';
|
|
6
|
-
export * from './Logging';
|
|
7
6
|
export * from './PinyinParser';
|
|
8
7
|
export * from './PinyinValidator';
|
|
9
8
|
export * from './SearchPreprocessor';
|
package/dist/util/index.js
CHANGED
|
@@ -20,7 +20,6 @@ __exportStar(require("./ConditionMatcher"), exports);
|
|
|
20
20
|
__exportStar(require("./Database"), exports);
|
|
21
21
|
__exportStar(require("./DifficultyRange"), exports);
|
|
22
22
|
__exportStar(require("./Encryption"), exports);
|
|
23
|
-
__exportStar(require("./Logging"), exports);
|
|
24
23
|
__exportStar(require("./PinyinParser"), exports);
|
|
25
24
|
__exportStar(require("./PinyinValidator"), exports);
|
|
26
25
|
__exportStar(require("./SearchPreprocessor"), exports);
|