@dsh-brain-core/plugin-console-chat 0.0.0-stage → 1.0.0-rc.1
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/LICENSE +133 -0
- package/package.json +60 -4
- package/src/index.d.ts +32 -0
- package/src/index.js +250 -0
- package/src/ui.d.ts +20 -0
- package/src/ui.js +230 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncommercial Purposes
|
|
53
|
+
|
|
54
|
+
Any noncommercial purpose is a permitted purpose.
|
|
55
|
+
|
|
56
|
+
## Personal Uses
|
|
57
|
+
|
|
58
|
+
Personal use for research, experiment, and testing for
|
|
59
|
+
the benefit of public knowledge, personal study, private
|
|
60
|
+
entertainment, hobby projects, amateur pursuits, or religious
|
|
61
|
+
observance, without any anticipated commercial application,
|
|
62
|
+
is use for a permitted purpose.
|
|
63
|
+
|
|
64
|
+
## Noncommercial Organizations
|
|
65
|
+
|
|
66
|
+
Use by any charitable organization, educational institution,
|
|
67
|
+
public research organization, public safety or health
|
|
68
|
+
organization, environmental protection organization,
|
|
69
|
+
or government institution is use for a permitted purpose
|
|
70
|
+
regardless of the source of funding or obligations resulting
|
|
71
|
+
from the funding.
|
|
72
|
+
|
|
73
|
+
## Fair Use
|
|
74
|
+
|
|
75
|
+
You may have "fair use" rights for the software under the
|
|
76
|
+
law. These terms do not limit them.
|
|
77
|
+
|
|
78
|
+
## No Other Rights
|
|
79
|
+
|
|
80
|
+
These terms do not allow you to sublicense or transfer any of
|
|
81
|
+
your licenses to anyone else, or prevent the licensor from
|
|
82
|
+
granting licenses to anyone else. These terms do not imply
|
|
83
|
+
any other licenses.
|
|
84
|
+
|
|
85
|
+
## Patent Defense
|
|
86
|
+
|
|
87
|
+
If you make any written claim that the software infringes or
|
|
88
|
+
contributes to infringement of any patent, your patent license
|
|
89
|
+
for the software granted under these terms ends immediately. If
|
|
90
|
+
your company makes such a claim, your patent license ends
|
|
91
|
+
immediately for work on behalf of your company.
|
|
92
|
+
|
|
93
|
+
## Violations
|
|
94
|
+
|
|
95
|
+
The first time you are notified in writing that you have
|
|
96
|
+
violated any of these terms, or done anything with the software
|
|
97
|
+
not covered by your licenses, your licenses can nonetheless
|
|
98
|
+
continue if you come into full compliance with these terms,
|
|
99
|
+
and take practical steps to correct past violations, within
|
|
100
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
101
|
+
end immediately.
|
|
102
|
+
|
|
103
|
+
## No Liability
|
|
104
|
+
|
|
105
|
+
***As far as the law allows, the software comes as is, without
|
|
106
|
+
any warranty or condition, and the licensor will not be liable
|
|
107
|
+
to you for any damages arising out of these terms or the use
|
|
108
|
+
or nature of the software, under any kind of legal claim.***
|
|
109
|
+
|
|
110
|
+
## Definitions
|
|
111
|
+
|
|
112
|
+
The **licensor** is the individual or entity offering these
|
|
113
|
+
terms, and the **software** is the software the licensor makes
|
|
114
|
+
available under these terms.
|
|
115
|
+
|
|
116
|
+
**You** refers to the individual or entity agreeing to these
|
|
117
|
+
terms.
|
|
118
|
+
|
|
119
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
120
|
+
or other kind of organization that you work for, plus all
|
|
121
|
+
organizations that have control over, are under the control of,
|
|
122
|
+
or are under common control with that organization. **Control**
|
|
123
|
+
means ownership of substantially all the assets of an entity,
|
|
124
|
+
or the power to direct its management and policies by vote,
|
|
125
|
+
contract, or otherwise. Control can be direct or indirect.
|
|
126
|
+
|
|
127
|
+
**Your licenses** are all the licenses granted to you for the
|
|
128
|
+
software under these terms.
|
|
129
|
+
|
|
130
|
+
**Use** means anything you do with the software requiring one
|
|
131
|
+
of your licenses.
|
|
132
|
+
|
|
133
|
+
Required Notice: Copyright (c) 2026 xiaoyuan
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsh-brain-core/plugin-console-chat",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"brain": {
|
|
11
|
+
"id": "console-chat",
|
|
12
|
+
"kinds": [
|
|
13
|
+
"channel"
|
|
14
|
+
],
|
|
15
|
+
"api": "^1.0.0",
|
|
16
|
+
"runtime": "node",
|
|
17
|
+
"permissions": [
|
|
18
|
+
"channel.provide",
|
|
19
|
+
"channel.send",
|
|
20
|
+
"agent.session",
|
|
21
|
+
"memory.ingest",
|
|
22
|
+
"identity.link",
|
|
23
|
+
"tools.register"
|
|
24
|
+
],
|
|
25
|
+
"source": {
|
|
26
|
+
"channels": [
|
|
27
|
+
"console-chat:private:*",
|
|
28
|
+
"console-chat:group:*"
|
|
29
|
+
],
|
|
30
|
+
"eventKinds": [
|
|
31
|
+
"console-chat.message"
|
|
32
|
+
],
|
|
33
|
+
"defaultVisibility": "subject",
|
|
34
|
+
"trust": "owner"
|
|
35
|
+
},
|
|
36
|
+
"tools": [
|
|
37
|
+
{
|
|
38
|
+
"name": "send_message",
|
|
39
|
+
"effect": "send",
|
|
40
|
+
"load": "core",
|
|
41
|
+
"summary": "在控制台对话里发送一条消息",
|
|
42
|
+
"textArg": "text"
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@dsh-brain-core/chat-kit": "1.0.0-rc.1",
|
|
48
|
+
"@dsh-brain-core/sdk": "1.0.0-rc.1"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=24.12.0"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"registry": "https://registry.npmjs.org/"
|
|
56
|
+
},
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/y083245/dsh-brain-core-sdk.git",
|
|
60
|
+
"directory": "plugins/console-chat"
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ChatClock, type ChatTimers, type MessageDebouncer } from '@dsh-brain-core/chat-kit';
|
|
2
|
+
import { type PluginApply, type WakeMessage } from '@dsh-brain-core/sdk';
|
|
3
|
+
export interface ChatHistoryEntry {
|
|
4
|
+
readonly messageId: string;
|
|
5
|
+
readonly channel: string;
|
|
6
|
+
readonly direction: 'inbound' | 'outbound';
|
|
7
|
+
readonly actor: string;
|
|
8
|
+
readonly displayName: string;
|
|
9
|
+
readonly text: string;
|
|
10
|
+
readonly replyTo?: string;
|
|
11
|
+
readonly recordId: string;
|
|
12
|
+
readonly tEvent: number;
|
|
13
|
+
}
|
|
14
|
+
export interface InboundMessageRequest {
|
|
15
|
+
readonly channel?: string;
|
|
16
|
+
readonly account?: string;
|
|
17
|
+
readonly displayName?: string;
|
|
18
|
+
readonly text: string;
|
|
19
|
+
readonly tEvent?: number;
|
|
20
|
+
readonly messageId?: string;
|
|
21
|
+
readonly debounceMs?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface ChannelWakeBatchOptions {
|
|
24
|
+
readonly clock?: ChatClock;
|
|
25
|
+
readonly timers?: ChatTimers;
|
|
26
|
+
readonly minQuietMs?: number;
|
|
27
|
+
readonly maxWaitMs?: number;
|
|
28
|
+
readonly onFlush: (channel: string, messages: readonly WakeMessage[]) => Promise<void> | void;
|
|
29
|
+
}
|
|
30
|
+
/** Optional chat-kit debouncer helper for batching inbound wake messages per channel. */
|
|
31
|
+
export declare function createConsoleChatDebouncer(options: ChannelWakeBatchOptions): MessageDebouncer<WakeMessage>;
|
|
32
|
+
export declare const apply: PluginApply;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { buildMessageSnapshot, computeHumanPauseMs, createMessageDebouncer, createReadCursor, } from '@dsh-brain-core/chat-kit';
|
|
2
|
+
import { BrainError } from '@dsh-brain-core/sdk';
|
|
3
|
+
import { renderChatUiHtml } from "./ui.js";
|
|
4
|
+
const DEFAULT_CHANNEL = 'console-chat:private:owner';
|
|
5
|
+
const DEFAULT_ACCOUNT = 'owner';
|
|
6
|
+
const DEFAULT_DISPLAY_NAME = '主人';
|
|
7
|
+
const VALID_CHANNEL = /^console-chat:(?:private|group):[^\s]+$/;
|
|
8
|
+
function assertValidChannel(channel) {
|
|
9
|
+
if (typeof channel !== 'string' || !VALID_CHANNEL.test(channel)) {
|
|
10
|
+
throw new BrainError('INVALID_ARGUMENT', 'channel must match console-chat:private:<id> or console-chat:group:<id> without whitespace');
|
|
11
|
+
}
|
|
12
|
+
return channel;
|
|
13
|
+
}
|
|
14
|
+
function assertNonEmptyString(value, field) {
|
|
15
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
16
|
+
throw new BrainError('INVALID_ARGUMENT', `${field} must be a non-empty string`);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function isPlainObject(value) {
|
|
21
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
/** Optional chat-kit debouncer helper for batching inbound wake messages per channel. */
|
|
24
|
+
export function createConsoleChatDebouncer(options) {
|
|
25
|
+
return createMessageDebouncer({
|
|
26
|
+
clock: options.clock ?? { now: () => Date.now() },
|
|
27
|
+
timers: options.timers ?? {
|
|
28
|
+
setTimeout: (fn, ms) => setTimeout(fn, ms),
|
|
29
|
+
clearTimeout: (handle) => clearTimeout(handle),
|
|
30
|
+
},
|
|
31
|
+
minQuietMs: options.minQuietMs ?? 0,
|
|
32
|
+
maxWaitMs: options.maxWaitMs ?? 0,
|
|
33
|
+
onFlush: options.onFlush,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export const apply = (ctx) => {
|
|
37
|
+
const profiles = new Map([['console-chat:owner', DEFAULT_DISPLAY_NAME]]);
|
|
38
|
+
const channelMembers = new Map();
|
|
39
|
+
const history = new Map();
|
|
40
|
+
const inboundDedupe = new Map();
|
|
41
|
+
const latestWakeByChannel = new Map();
|
|
42
|
+
const readCursor = createReadCursor();
|
|
43
|
+
let nextMessageSeq = 0;
|
|
44
|
+
const nextMessageId = () => {
|
|
45
|
+
nextMessageSeq += 1;
|
|
46
|
+
return `msg_${nextMessageSeq}`;
|
|
47
|
+
};
|
|
48
|
+
const appendHistory = (channel, entry) => {
|
|
49
|
+
const list = history.get(channel) ?? [];
|
|
50
|
+
list.push(entry);
|
|
51
|
+
history.set(channel, list);
|
|
52
|
+
};
|
|
53
|
+
ctx.channel.provide({
|
|
54
|
+
send: async (channel) => {
|
|
55
|
+
assertValidChannel(channel);
|
|
56
|
+
return { messageId: nextMessageId() };
|
|
57
|
+
},
|
|
58
|
+
members: async (channel) => [...(channelMembers.get(channel) ?? [])].sort(),
|
|
59
|
+
resolveIdentity: async (account) => {
|
|
60
|
+
if (!account.startsWith('console-chat:'))
|
|
61
|
+
return null;
|
|
62
|
+
const displayName = profiles.get(account);
|
|
63
|
+
return displayName === undefined ? null : { displayName };
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
ctx.tools.register({
|
|
67
|
+
name: 'send_message',
|
|
68
|
+
description: '在控制台对话里发送一条消息。',
|
|
69
|
+
parameters: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
properties: {
|
|
72
|
+
text: { type: 'string', description: '要发送的消息内容' },
|
|
73
|
+
replyTo: { type: 'string', description: '回复的消息 ID' },
|
|
74
|
+
},
|
|
75
|
+
required: ['text'],
|
|
76
|
+
},
|
|
77
|
+
handler: async (args, call) => {
|
|
78
|
+
const text = assertNonEmptyString(args.text, 'text');
|
|
79
|
+
if (args.replyTo !== undefined && typeof args.replyTo !== 'string') {
|
|
80
|
+
throw new BrainError('INVALID_ARGUMENT', 'replyTo must be a string');
|
|
81
|
+
}
|
|
82
|
+
const replyTo = args.replyTo;
|
|
83
|
+
const channel = assertValidChannel(call.channel);
|
|
84
|
+
const sent = await ctx.channel.send(channel, {
|
|
85
|
+
text,
|
|
86
|
+
...(replyTo === undefined ? {} : { replyTo }),
|
|
87
|
+
});
|
|
88
|
+
const messageId = sent.messageId ?? nextMessageId();
|
|
89
|
+
const tEvent = Date.now();
|
|
90
|
+
const ingested = await ctx.memory.ingest({
|
|
91
|
+
kind: 'console-chat.message',
|
|
92
|
+
channel,
|
|
93
|
+
subjects: ['self'],
|
|
94
|
+
actor: 'self',
|
|
95
|
+
content: {
|
|
96
|
+
text,
|
|
97
|
+
direction: 'outbound',
|
|
98
|
+
messageId,
|
|
99
|
+
...(replyTo === undefined ? {} : { replyTo }),
|
|
100
|
+
},
|
|
101
|
+
tEvent,
|
|
102
|
+
idempotencyKey: `console-chat:out:${call.wakeId}:${messageId}`,
|
|
103
|
+
});
|
|
104
|
+
appendHistory(channel, {
|
|
105
|
+
messageId,
|
|
106
|
+
channel,
|
|
107
|
+
direction: 'outbound',
|
|
108
|
+
actor: 'self',
|
|
109
|
+
displayName: '她',
|
|
110
|
+
text,
|
|
111
|
+
...(replyTo === undefined ? {} : { replyTo }),
|
|
112
|
+
recordId: ingested.id,
|
|
113
|
+
tEvent,
|
|
114
|
+
});
|
|
115
|
+
const suggestedPauseMs = computeHumanPauseMs(text);
|
|
116
|
+
ctx.logger.info('console_chat.sent', { channel, recordId: ingested.id, suggestedPauseMs });
|
|
117
|
+
return 'sent';
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
ctx.http.route('POST', '/messages', async (request) => {
|
|
121
|
+
if (!isPlainObject(request.body)) {
|
|
122
|
+
throw new BrainError('INVALID_ARGUMENT', 'request body must be an object');
|
|
123
|
+
}
|
|
124
|
+
const body = request.body;
|
|
125
|
+
const channel = assertValidChannel(body.channel === undefined ? DEFAULT_CHANNEL : body.channel);
|
|
126
|
+
const text = assertNonEmptyString(body.text, 'text');
|
|
127
|
+
const rawAccount = body.account === undefined ? DEFAULT_ACCOUNT : body.account;
|
|
128
|
+
const account = assertNonEmptyString(rawAccount, 'account');
|
|
129
|
+
if (/\s/.test(account)) {
|
|
130
|
+
throw new BrainError('INVALID_ARGUMENT', 'account must not contain whitespace');
|
|
131
|
+
}
|
|
132
|
+
const fullAccount = account.startsWith('console-chat:') ? account : `console-chat:${account}`;
|
|
133
|
+
if (body.displayName !== undefined && typeof body.displayName !== 'string') {
|
|
134
|
+
throw new BrainError('INVALID_ARGUMENT', 'displayName must be a string');
|
|
135
|
+
}
|
|
136
|
+
const displayName = body.displayName !== undefined && body.displayName.trim().length > 0
|
|
137
|
+
? body.displayName
|
|
138
|
+
: (profiles.get(fullAccount) ?? DEFAULT_DISPLAY_NAME);
|
|
139
|
+
const tEvent = body.tEvent === undefined ? Date.now() : body.tEvent;
|
|
140
|
+
if (typeof tEvent !== 'number' || !Number.isFinite(tEvent)) {
|
|
141
|
+
throw new BrainError('INVALID_ARGUMENT', 'tEvent must be a finite number');
|
|
142
|
+
}
|
|
143
|
+
if (body.debounceMs !== undefined && (typeof body.debounceMs !== 'number' || !Number.isFinite(body.debounceMs) || body.debounceMs < 0)) {
|
|
144
|
+
throw new BrainError('INVALID_ARGUMENT', 'debounceMs must be a non-negative finite number');
|
|
145
|
+
}
|
|
146
|
+
const debounceMs = body.debounceMs;
|
|
147
|
+
const messageId = body.messageId === undefined
|
|
148
|
+
? nextMessageId()
|
|
149
|
+
: assertNonEmptyString(body.messageId, 'messageId');
|
|
150
|
+
profiles.set(fullAccount, displayName);
|
|
151
|
+
const personId = await ctx.identity.resolve(fullAccount);
|
|
152
|
+
const members = channelMembers.get(channel) ?? new Set();
|
|
153
|
+
members.add(personId);
|
|
154
|
+
channelMembers.set(channel, members);
|
|
155
|
+
const dedupeKey = `${channel}\u0000${messageId}`;
|
|
156
|
+
const cached = inboundDedupe.get(dedupeKey);
|
|
157
|
+
if (cached !== undefined) {
|
|
158
|
+
return cached;
|
|
159
|
+
}
|
|
160
|
+
const ingested = await ctx.memory.ingest({
|
|
161
|
+
kind: 'console-chat.message',
|
|
162
|
+
channel,
|
|
163
|
+
subjects: [personId],
|
|
164
|
+
actor: personId,
|
|
165
|
+
content: {
|
|
166
|
+
text,
|
|
167
|
+
direction: 'inbound',
|
|
168
|
+
account: fullAccount,
|
|
169
|
+
displayName,
|
|
170
|
+
messageId,
|
|
171
|
+
},
|
|
172
|
+
tEvent,
|
|
173
|
+
idempotencyKey: `console-chat:in:${channel}:${messageId}`,
|
|
174
|
+
});
|
|
175
|
+
const existingInHistory = (history.get(channel) ?? []).some((entry) => entry.direction === 'inbound' && entry.messageId === messageId);
|
|
176
|
+
if (!existingInHistory) {
|
|
177
|
+
appendHistory(channel, {
|
|
178
|
+
messageId,
|
|
179
|
+
channel,
|
|
180
|
+
direction: 'inbound',
|
|
181
|
+
actor: personId,
|
|
182
|
+
displayName,
|
|
183
|
+
text,
|
|
184
|
+
recordId: ingested.id,
|
|
185
|
+
tEvent,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
readCursor.onInbound(channel, messageId, tEvent);
|
|
189
|
+
const wake = await ctx.agent.wake(channel, {
|
|
190
|
+
reason: 'message',
|
|
191
|
+
...(debounceMs === undefined ? {} : { debounceMs }),
|
|
192
|
+
messages: [{
|
|
193
|
+
id: ingested.id,
|
|
194
|
+
actor: personId,
|
|
195
|
+
name: displayName,
|
|
196
|
+
text,
|
|
197
|
+
tEvent,
|
|
198
|
+
}],
|
|
199
|
+
});
|
|
200
|
+
readCursor.markRead(channel, messageId, Date.now());
|
|
201
|
+
latestWakeByChannel.set(channel, wake.wakeId);
|
|
202
|
+
const result = {
|
|
203
|
+
recordId: ingested.id,
|
|
204
|
+
personId,
|
|
205
|
+
messageId,
|
|
206
|
+
wake,
|
|
207
|
+
};
|
|
208
|
+
inboundDedupe.set(dedupeKey, result);
|
|
209
|
+
return result;
|
|
210
|
+
});
|
|
211
|
+
ctx.http.route('GET', '/messages', async (request) => {
|
|
212
|
+
const channel = assertValidChannel(request.query.channel ?? DEFAULT_CHANNEL);
|
|
213
|
+
const entries = history.get(channel) ?? [];
|
|
214
|
+
const now = Date.now();
|
|
215
|
+
const cursor = readCursor.get(channel);
|
|
216
|
+
const snapshot = buildMessageSnapshot(entries.map((entry) => ({
|
|
217
|
+
id: entry.messageId,
|
|
218
|
+
who: entry.displayName,
|
|
219
|
+
text: entry.text,
|
|
220
|
+
tEvent: entry.tEvent,
|
|
221
|
+
direction: entry.direction,
|
|
222
|
+
})), now);
|
|
223
|
+
return {
|
|
224
|
+
channel,
|
|
225
|
+
items: [...entries],
|
|
226
|
+
unreadCount: cursor.unreadCount,
|
|
227
|
+
...(cursor.lastReadMessageId === undefined ? {} : { lastReadMessageId: cursor.lastReadMessageId }),
|
|
228
|
+
...(cursor.lastReadAt === undefined ? {} : { lastReadAt: cursor.lastReadAt }),
|
|
229
|
+
snapshot,
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
ctx.http.route('GET', '/ui', async (request) => {
|
|
233
|
+
const channel = assertValidChannel(request.query.channel ?? DEFAULT_CHANNEL);
|
|
234
|
+
const messages = history.get(channel) ?? [];
|
|
235
|
+
const latestWakeId = latestWakeByChannel.get(channel);
|
|
236
|
+
const cursor = readCursor.get(channel);
|
|
237
|
+
return renderChatUiHtml({
|
|
238
|
+
channel,
|
|
239
|
+
messages,
|
|
240
|
+
unreadCount: cursor.unreadCount,
|
|
241
|
+
now: Date.now(),
|
|
242
|
+
...(latestWakeId === undefined ? {} : { latestWakeId }),
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
ctx.console.page({
|
|
246
|
+
id: 'chat',
|
|
247
|
+
title: '与她对话',
|
|
248
|
+
entry: '/api/v1/plugins/console-chat/ui',
|
|
249
|
+
});
|
|
250
|
+
};
|
package/src/ui.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ChatUiMessage {
|
|
2
|
+
readonly messageId: string;
|
|
3
|
+
readonly direction: 'inbound' | 'outbound';
|
|
4
|
+
readonly displayName: string;
|
|
5
|
+
readonly text: string;
|
|
6
|
+
readonly replyTo?: string;
|
|
7
|
+
readonly recordId: string;
|
|
8
|
+
readonly tEvent: number;
|
|
9
|
+
}
|
|
10
|
+
export interface ChatUiInput {
|
|
11
|
+
readonly channel: string;
|
|
12
|
+
readonly messages: readonly ChatUiMessage[];
|
|
13
|
+
readonly latestWakeId?: string;
|
|
14
|
+
readonly unreadCount?: number;
|
|
15
|
+
readonly now?: number;
|
|
16
|
+
}
|
|
17
|
+
/** Escape untrusted text before inserting into HTML text nodes or attributes. */
|
|
18
|
+
export declare function escapeHtml(value: string): string;
|
|
19
|
+
/** Render the Apple iMessage frosted-glass conversation page for `GET /ui`. */
|
|
20
|
+
export declare function renderChatUiHtml(input: ChatUiInput): string;
|
package/src/ui.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { formatRelativeTime } from '@dsh-brain-core/chat-kit';
|
|
2
|
+
/** Escape untrusted text before inserting into HTML text nodes or attributes. */
|
|
3
|
+
export function escapeHtml(value) {
|
|
4
|
+
return value
|
|
5
|
+
.replaceAll('&', '&')
|
|
6
|
+
.replaceAll('<', '<')
|
|
7
|
+
.replaceAll('>', '>')
|
|
8
|
+
.replaceAll('"', '"')
|
|
9
|
+
.replaceAll("'", ''');
|
|
10
|
+
}
|
|
11
|
+
/** Render the Apple iMessage frosted-glass conversation page for `GET /ui`. */
|
|
12
|
+
export function renderChatUiHtml(input) {
|
|
13
|
+
const now = input.now ?? Date.now();
|
|
14
|
+
const safeChannel = escapeHtml(input.channel);
|
|
15
|
+
const latestEvidence = input.messages.length > 0 ? input.messages[input.messages.length - 1].recordId : 'none';
|
|
16
|
+
const safeWakeId = escapeHtml(input.latestWakeId ?? 'idle');
|
|
17
|
+
const safeEvidence = escapeHtml(latestEvidence);
|
|
18
|
+
const unreadCount = input.unreadCount ?? 0;
|
|
19
|
+
const bubblesHtml = input.messages.length === 0
|
|
20
|
+
? '<div class="empty-state glass-card">还没有消息,发一句试试吧。</div>'
|
|
21
|
+
: input.messages.map((item) => {
|
|
22
|
+
const roleClass = item.direction === 'outbound' ? 'bubble-outbound' : 'bubble-inbound';
|
|
23
|
+
const ago = formatRelativeTime(now - item.tEvent);
|
|
24
|
+
const replyHtml = item.replyTo === undefined
|
|
25
|
+
? ''
|
|
26
|
+
: `<div class="bubble-reply">回复 ${escapeHtml(item.replyTo)}</div>`;
|
|
27
|
+
return `<article class="message-row ${roleClass}" data-message-id="${escapeHtml(item.messageId)}">
|
|
28
|
+
<div class="message-meta">
|
|
29
|
+
<span class="message-author">${escapeHtml(item.displayName)}</span>
|
|
30
|
+
<span class="message-ago">${escapeHtml(ago)}</span>
|
|
31
|
+
<span class="mono evidence-chip" title="L0 记忆证据">${escapeHtml(item.recordId)}</span>
|
|
32
|
+
</div>
|
|
33
|
+
<div class="bubble glass-card">
|
|
34
|
+
${replyHtml}
|
|
35
|
+
<div class="bubble-text">${escapeHtml(item.text)}</div>
|
|
36
|
+
</div>
|
|
37
|
+
</article>`;
|
|
38
|
+
}).join('\n');
|
|
39
|
+
return `<!doctype html>
|
|
40
|
+
<html lang="zh-CN" data-theme="auto">
|
|
41
|
+
<head>
|
|
42
|
+
<meta charset="utf-8" />
|
|
43
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
44
|
+
<title>与她对话 · ${safeChannel}</title>
|
|
45
|
+
<style>
|
|
46
|
+
:root {
|
|
47
|
+
--font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "PingFang SC", sans-serif;
|
|
48
|
+
--font-mono: "SF Mono", Menlo, monospace;
|
|
49
|
+
--blue: #0A84FF;
|
|
50
|
+
--green: #30D158;
|
|
51
|
+
--bg: #05070F;
|
|
52
|
+
--window: rgba(22, 24, 34, 0.58);
|
|
53
|
+
--card: rgba(255, 255, 255, 0.065);
|
|
54
|
+
--card-edge: 1px solid rgba(255, 255, 255, 0.13);
|
|
55
|
+
--card-mirror: inset 0 1px 0 rgba(255, 255, 255, 0.22);
|
|
56
|
+
--text: rgba(255, 255, 255, 0.94);
|
|
57
|
+
--text-dim: rgba(255, 255, 255, 0.62);
|
|
58
|
+
--inbound-bg: rgba(255, 255, 255, 0.12);
|
|
59
|
+
--outbound-bg: rgba(10, 132, 255, 0.86);
|
|
60
|
+
}
|
|
61
|
+
@media (prefers-color-scheme: light) {
|
|
62
|
+
[data-theme="auto"] {
|
|
63
|
+
--bg: #DCE5F2;
|
|
64
|
+
--window: rgba(246, 248, 252, 0.65);
|
|
65
|
+
--card: rgba(255, 255, 255, 0.72);
|
|
66
|
+
--card-edge: 1px solid rgba(255, 255, 255, 0.92);
|
|
67
|
+
--card-mirror: inset 0 1px 0 #FFFFFF;
|
|
68
|
+
--text: rgba(0, 0, 0, 0.88);
|
|
69
|
+
--text-dim: rgba(0, 0, 0, 0.55);
|
|
70
|
+
--inbound-bg: rgba(255, 255, 255, 0.84);
|
|
71
|
+
--outbound-bg: #0A84FF;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
* { box-sizing: border-box; }
|
|
75
|
+
body {
|
|
76
|
+
margin: 0;
|
|
77
|
+
min-height: 100vh;
|
|
78
|
+
font-family: var(--font-sans);
|
|
79
|
+
color: var(--text);
|
|
80
|
+
background:
|
|
81
|
+
radial-gradient(60% 50% at 20% 15%, rgba(10, 132, 255, 0.28), transparent 70%),
|
|
82
|
+
radial-gradient(50% 45% at 85% 25%, rgba(94, 92, 230, 0.24), transparent 72%),
|
|
83
|
+
var(--bg);
|
|
84
|
+
display: grid;
|
|
85
|
+
place-items: center;
|
|
86
|
+
padding: 20px;
|
|
87
|
+
}
|
|
88
|
+
.chat-window {
|
|
89
|
+
width: min(860px, 100%);
|
|
90
|
+
height: min(720px, calc(100vh - 40px));
|
|
91
|
+
display: flex;
|
|
92
|
+
flex-direction: column;
|
|
93
|
+
border-radius: 22px;
|
|
94
|
+
background: var(--window);
|
|
95
|
+
border: var(--card-edge);
|
|
96
|
+
box-shadow: 0 28px 72px rgba(0, 0, 0, 0.35), var(--card-mirror);
|
|
97
|
+
backdrop-filter: blur(48px) saturate(190%);
|
|
98
|
+
-webkit-backdrop-filter: blur(48px) saturate(190%);
|
|
99
|
+
overflow: hidden;
|
|
100
|
+
}
|
|
101
|
+
.glass-card {
|
|
102
|
+
background: var(--card);
|
|
103
|
+
border: var(--card-edge);
|
|
104
|
+
box-shadow: var(--card-mirror);
|
|
105
|
+
border-radius: 18px;
|
|
106
|
+
backdrop-filter: blur(48px) saturate(180%);
|
|
107
|
+
-webkit-backdrop-filter: blur(48px) saturate(180%);
|
|
108
|
+
}
|
|
109
|
+
.status-bar {
|
|
110
|
+
display: flex;
|
|
111
|
+
align-items: center;
|
|
112
|
+
justify-content: space-between;
|
|
113
|
+
gap: 12px;
|
|
114
|
+
padding: 14px 18px;
|
|
115
|
+
border-bottom: 1px solid rgba(127, 127, 127, 0.18);
|
|
116
|
+
}
|
|
117
|
+
.mono {
|
|
118
|
+
font-family: var(--font-mono);
|
|
119
|
+
font-variant-numeric: tabular-nums;
|
|
120
|
+
font-size: 12px;
|
|
121
|
+
}
|
|
122
|
+
.status-pills {
|
|
123
|
+
display: flex;
|
|
124
|
+
gap: 8px;
|
|
125
|
+
flex-wrap: wrap;
|
|
126
|
+
color: var(--text-dim);
|
|
127
|
+
}
|
|
128
|
+
.messages {
|
|
129
|
+
flex: 1;
|
|
130
|
+
overflow-y: auto;
|
|
131
|
+
padding: 18px;
|
|
132
|
+
display: flex;
|
|
133
|
+
flex-direction: column;
|
|
134
|
+
gap: 14px;
|
|
135
|
+
}
|
|
136
|
+
.message-row {
|
|
137
|
+
display: flex;
|
|
138
|
+
flex-direction: column;
|
|
139
|
+
max-width: 78%;
|
|
140
|
+
gap: 4px;
|
|
141
|
+
}
|
|
142
|
+
.bubble-inbound { align-self: flex-start; }
|
|
143
|
+
.bubble-outbound { align-self: flex-end; }
|
|
144
|
+
.message-meta {
|
|
145
|
+
display: flex;
|
|
146
|
+
align-items: center;
|
|
147
|
+
gap: 8px;
|
|
148
|
+
font-size: 12px;
|
|
149
|
+
color: var(--text-dim);
|
|
150
|
+
padding: 0 6px;
|
|
151
|
+
}
|
|
152
|
+
.bubble {
|
|
153
|
+
padding: 11px 15px;
|
|
154
|
+
line-height: 1.5;
|
|
155
|
+
}
|
|
156
|
+
.bubble-inbound .bubble { background: var(--inbound-bg); }
|
|
157
|
+
.bubble-outbound .bubble {
|
|
158
|
+
background: var(--outbound-bg);
|
|
159
|
+
color: #FFFFFF;
|
|
160
|
+
border-color: rgba(255, 255, 255, 0.24);
|
|
161
|
+
}
|
|
162
|
+
.bubble-reply {
|
|
163
|
+
font-size: 12px;
|
|
164
|
+
opacity: 0.78;
|
|
165
|
+
margin-bottom: 4px;
|
|
166
|
+
padding-left: 8px;
|
|
167
|
+
border-left: 2px solid currentColor;
|
|
168
|
+
}
|
|
169
|
+
.evidence-chip {
|
|
170
|
+
padding: 2px 7px;
|
|
171
|
+
border-radius: 999px;
|
|
172
|
+
background: rgba(127, 127, 127, 0.18);
|
|
173
|
+
}
|
|
174
|
+
.composer {
|
|
175
|
+
display: flex;
|
|
176
|
+
gap: 10px;
|
|
177
|
+
padding: 14px 18px;
|
|
178
|
+
border-top: 1px solid rgba(127, 127, 127, 0.18);
|
|
179
|
+
}
|
|
180
|
+
.composer input {
|
|
181
|
+
flex: 1;
|
|
182
|
+
padding: 10px 14px;
|
|
183
|
+
border-radius: 14px;
|
|
184
|
+
border: var(--card-edge);
|
|
185
|
+
background: var(--card);
|
|
186
|
+
color: var(--text);
|
|
187
|
+
font: inherit;
|
|
188
|
+
outline: none;
|
|
189
|
+
}
|
|
190
|
+
.composer button {
|
|
191
|
+
padding: 10px 18px;
|
|
192
|
+
border: none;
|
|
193
|
+
border-radius: 14px;
|
|
194
|
+
background: var(--blue);
|
|
195
|
+
color: #FFFFFF;
|
|
196
|
+
font: inherit;
|
|
197
|
+
font-weight: 600;
|
|
198
|
+
cursor: pointer;
|
|
199
|
+
}
|
|
200
|
+
.empty-state {
|
|
201
|
+
margin: auto;
|
|
202
|
+
padding: 18px 24px;
|
|
203
|
+
color: var(--text-dim);
|
|
204
|
+
}
|
|
205
|
+
</style>
|
|
206
|
+
</head>
|
|
207
|
+
<body>
|
|
208
|
+
<main class="chat-window" data-channel="${safeChannel}">
|
|
209
|
+
<header class="status-bar">
|
|
210
|
+
<div>
|
|
211
|
+
<strong>与她对话</strong>
|
|
212
|
+
<span class="mono">${safeChannel}</span>
|
|
213
|
+
</div>
|
|
214
|
+
<div class="status-pills mono" data-role="evidence-wake-bar">
|
|
215
|
+
<span data-unread-count="${unreadCount}">unread: ${unreadCount}</span>
|
|
216
|
+
<span data-wake-id="${safeWakeId}">wakeId: ${safeWakeId}</span>
|
|
217
|
+
<span data-evidence-id="${safeEvidence}">memoryEvidence: ${safeEvidence}</span>
|
|
218
|
+
</div>
|
|
219
|
+
</header>
|
|
220
|
+
<section class="messages" aria-label="对话消息">
|
|
221
|
+
${bubblesHtml}
|
|
222
|
+
</section>
|
|
223
|
+
<form class="composer" method="post" action="/api/v1/plugins/console-chat/messages">
|
|
224
|
+
<input type="text" name="text" placeholder="发一条消息…" autocomplete="off" />
|
|
225
|
+
<button type="submit">发送</button>
|
|
226
|
+
</form>
|
|
227
|
+
</main>
|
|
228
|
+
</body>
|
|
229
|
+
</html>`;
|
|
230
|
+
}
|
package/README.md
DELETED