@lincyaw/addon-assistant 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 +202 -0
- package/README.md +27 -2
- package/package.json +45 -3
- package/src/client/AskPanel.css +94 -0
- package/src/client/AskPanel.tsx +195 -0
- package/src/client/AskSettings.css +25 -0
- package/src/client/AskSettings.tsx +122 -0
- package/src/client/AssistantToolbar.css +20 -0
- package/src/client/AssistantToolbar.tsx +33 -0
- package/src/client/chat-client.ts +90 -0
- package/src/client/env.d.ts +7 -0
- package/src/client/index.ts +1 -0
- package/src/client/plugin.ts +49 -0
- package/src/client/settings.ts +22 -0
- package/src/client/state.ts +53 -0
- package/src/client/viewer.ts +19 -0
- package/src/domain/citations.ts +27 -0
- package/src/domain/completion.ts +29 -0
- package/src/domain/index.ts +12 -0
- package/src/domain/messages.ts +81 -0
- package/src/domain/prompt.ts +44 -0
- package/src/domain/settings.ts +36 -0
- package/src/node/presmium.ts +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lincyaw/addon-assistant
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The readers' questions: readers ask about a lecture with their own model service.
|
|
4
|
+
|
|
5
|
+
Part of [Presmium](https://github.com/Lincyaw/presmium), which compiles canonical lecture.json teaching documents into web pages, slides, Jupyter notebooks and narrated playback.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add -D @lincyaw/addon-assistant
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Add this package to `metadata.plugins` in the document's `lecture.json`.
|
|
14
|
+
|
|
15
|
+
It is a Presmium plugin: its components work on the web page and in the slides. It needs `@lincyaw/cli` and the renderers in the same project, and declares the Presmium packages it shares with them as peer dependencies (Presmium 1.x).
|
|
16
|
+
|
|
17
|
+
## What it provides
|
|
18
|
+
|
|
19
|
+
An Ask panel on the web page, the slides and the presenter view, also in static sites: readers ask about a section or a selection, with an OpenAI compatible model service they set up in their browser; the site needs no server and no operator setting. Listed among a workspace's operator plugins, every document of the workspace has it.
|
|
20
|
+
|
|
21
|
+
## Documentation
|
|
22
|
+
|
|
23
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/lecture.json) (Chinese: `site/guide/lecture.json`)
|
|
24
|
+
- [Document contract](https://github.com/Lincyaw/presmium/blob/main/.agents/skills/presmium-author/references/document.md): normalized entities, parameters, states and shared validation
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/addon-assistant",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"description": "Presmium plugin: readers ask an AI about the lecture, with their own model service, in the browser",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Lincyaw/presmium.git",
|
|
9
|
+
"directory": "packages/addon-assistant"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Lincyaw/presmium/tree/main/packages/addon-assistant#readme",
|
|
12
|
+
"bugs": "https://github.com/Lincyaw/presmium/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"presmium"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22.19"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./domain": "./src/domain/index.ts",
|
|
25
|
+
"./client": "./src/client/index.ts",
|
|
26
|
+
"./presmium": "./src/node/presmium.ts",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"eventsource-parser": "4.1.2"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": "^18.3.1",
|
|
37
|
+
"react-dom": "^18.3.1",
|
|
38
|
+
"@lincyaw/kernel": "^1.0.0-rc.1",
|
|
39
|
+
"@lincyaw/host": "^1.0.0-rc.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/react": "18.3.28",
|
|
43
|
+
"@types/react-dom": "^18.3.7",
|
|
44
|
+
"@lincyaw/host": "1.0.0-rc.1",
|
|
45
|
+
"@lincyaw/viewer": "1.0.0-rc.1",
|
|
46
|
+
"@lincyaw/kernel": "1.0.0-rc.1"
|
|
47
|
+
}
|
|
6
48
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
.pm-assistant {
|
|
2
|
+
display: grid;
|
|
3
|
+
gap: var(--pm-space-3);
|
|
4
|
+
color: var(--pm-color-ink);
|
|
5
|
+
font-family: var(--pm-font-sans);
|
|
6
|
+
font-size: var(--pm-text-md);
|
|
7
|
+
line-height: var(--pm-leading-ui);
|
|
8
|
+
text-align: left;
|
|
9
|
+
white-space: normal;
|
|
10
|
+
overflow-wrap: anywhere;
|
|
11
|
+
}
|
|
12
|
+
.pm-assistant p {
|
|
13
|
+
margin: 0;
|
|
14
|
+
}
|
|
15
|
+
.pm-assistant__provider,
|
|
16
|
+
.pm-assistant__where,
|
|
17
|
+
.pm-assistant__status {
|
|
18
|
+
color: var(--pm-color-muted);
|
|
19
|
+
font-size: var(--pm-text-sm);
|
|
20
|
+
}
|
|
21
|
+
.pm-assistant__provider {
|
|
22
|
+
display: flex;
|
|
23
|
+
flex-wrap: wrap;
|
|
24
|
+
align-items: center;
|
|
25
|
+
justify-content: space-between;
|
|
26
|
+
gap: var(--pm-space-2);
|
|
27
|
+
}
|
|
28
|
+
.pm-assistant__form {
|
|
29
|
+
display: grid;
|
|
30
|
+
gap: var(--pm-space-2);
|
|
31
|
+
}
|
|
32
|
+
.pm-assistant__selection {
|
|
33
|
+
display: flex;
|
|
34
|
+
flex-wrap: wrap;
|
|
35
|
+
align-items: center;
|
|
36
|
+
gap: var(--pm-space-2);
|
|
37
|
+
padding: var(--pm-space-2) var(--pm-space-3);
|
|
38
|
+
border-radius: var(--pm-radius-control);
|
|
39
|
+
background: var(--pm-color-selected);
|
|
40
|
+
}
|
|
41
|
+
.pm-assistant__selection code {
|
|
42
|
+
max-width: 100%;
|
|
43
|
+
white-space: pre-wrap;
|
|
44
|
+
}
|
|
45
|
+
.pm-assistant textarea {
|
|
46
|
+
width: 100%;
|
|
47
|
+
}
|
|
48
|
+
.pm-assistant__actions {
|
|
49
|
+
display: flex;
|
|
50
|
+
justify-content: flex-end;
|
|
51
|
+
}
|
|
52
|
+
.pm-assistant__answers {
|
|
53
|
+
display: grid;
|
|
54
|
+
gap: var(--pm-space-3);
|
|
55
|
+
margin: 0;
|
|
56
|
+
padding: 0;
|
|
57
|
+
list-style: none;
|
|
58
|
+
}
|
|
59
|
+
.pm-assistant__answers > li {
|
|
60
|
+
display: grid;
|
|
61
|
+
gap: var(--pm-space-2);
|
|
62
|
+
}
|
|
63
|
+
.pm-assistant__question {
|
|
64
|
+
justify-self: end;
|
|
65
|
+
max-width: 85%;
|
|
66
|
+
padding: var(--pm-space-2) var(--pm-space-3);
|
|
67
|
+
border-radius: var(--pm-radius-card);
|
|
68
|
+
background: var(--pm-color-accent-soft);
|
|
69
|
+
font-weight: 500;
|
|
70
|
+
}
|
|
71
|
+
.pm-assistant__answer {
|
|
72
|
+
font-family: var(--pm-font-serif);
|
|
73
|
+
font-size: var(--pm-text-lg);
|
|
74
|
+
line-height: 1.7;
|
|
75
|
+
white-space: pre-wrap;
|
|
76
|
+
}
|
|
77
|
+
.pm-assistant__cite {
|
|
78
|
+
margin: 0;
|
|
79
|
+
padding: 0;
|
|
80
|
+
border: 0;
|
|
81
|
+
background: none;
|
|
82
|
+
color: var(--pm-color-accent);
|
|
83
|
+
font: inherit;
|
|
84
|
+
text-decoration: underline;
|
|
85
|
+
text-underline-offset: var(--pm-size-stroke);
|
|
86
|
+
cursor: pointer;
|
|
87
|
+
}
|
|
88
|
+
.pm-assistant__cite:focus-visible {
|
|
89
|
+
outline: var(--pm-size-stroke) solid var(--pm-color-accent);
|
|
90
|
+
outline-offset: var(--pm-size-stroke);
|
|
91
|
+
}
|
|
92
|
+
.pm-assistant__error {
|
|
93
|
+
color: var(--pm-color-danger);
|
|
94
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { Fragment, useState, useRef, useEffect, useMemo, useId } from 'react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { PmButton, useMessages, useStore } from '@lincyaw/host'
|
|
4
|
+
import type { AgentContext, Citation } from '@lincyaw/kernel'
|
|
5
|
+
import { answerParts, askProvider, ASSISTANT_MESSAGES } from '../domain/index.ts'
|
|
6
|
+
import AskSettings from './AskSettings.tsx'
|
|
7
|
+
import { askSettings as settingsStore } from './settings.ts'
|
|
8
|
+
import {
|
|
9
|
+
askClient as clientStore,
|
|
10
|
+
exchanges as exchangesStore,
|
|
11
|
+
selected as selectionStore,
|
|
12
|
+
showCitation,
|
|
13
|
+
slideNavigation as navigationStore,
|
|
14
|
+
startExchange,
|
|
15
|
+
useWebViewerAccess,
|
|
16
|
+
} from './state.ts'
|
|
17
|
+
import './AskPanel.css'
|
|
18
|
+
export default function AskPanel(props: {
|
|
19
|
+
children?: ReactNode
|
|
20
|
+
className?: string
|
|
21
|
+
onDone?(): void
|
|
22
|
+
onCited?(): void
|
|
23
|
+
}) {
|
|
24
|
+
const t = useMessages(ASSISTANT_MESSAGES)
|
|
25
|
+
const askSettings = useStore(settingsStore)
|
|
26
|
+
const askClient = useStore(clientStore)
|
|
27
|
+
const exchanges = useStore(exchangesStore)
|
|
28
|
+
const selected = useStore(selectionStore)
|
|
29
|
+
const slideNavigation = useStore(navigationStore)
|
|
30
|
+
const webViewer = useWebViewerAccess()
|
|
31
|
+
const [text, setText] = useState('')
|
|
32
|
+
const [changing, setChanging] = useState(false)
|
|
33
|
+
const running = exchanges.some((e) => e.state === 'running')
|
|
34
|
+
const service = askSettings && { provider: askProvider(askSettings.url), model: askSettings.model }
|
|
35
|
+
const canShowCitations = Boolean(webViewer ?? slideNavigation)
|
|
36
|
+
|
|
37
|
+
const place = (() => {
|
|
38
|
+
const position = webViewer?.position() ?? slideNavigation?.position.getSnapshot()
|
|
39
|
+
const section = selected?.section ?? position?.section
|
|
40
|
+
return section
|
|
41
|
+
? {
|
|
42
|
+
section,
|
|
43
|
+
step: position?.section === section ? position.step : undefined,
|
|
44
|
+
title: webViewer?.title(section) ?? section,
|
|
45
|
+
}
|
|
46
|
+
: undefined
|
|
47
|
+
})()
|
|
48
|
+
const excerpt = (() => {
|
|
49
|
+
const value = selected?.text ?? ''
|
|
50
|
+
return value.length > 120 ? `${value.slice(0, 120)}…` : value
|
|
51
|
+
})()
|
|
52
|
+
|
|
53
|
+
function submit(): void {
|
|
54
|
+
const question = text.trim()
|
|
55
|
+
const client = askClient
|
|
56
|
+
if (!question || !client) return
|
|
57
|
+
const context: AgentContext = {
|
|
58
|
+
document: '',
|
|
59
|
+
...(place ? { section: place.section } : {}),
|
|
60
|
+
...(place?.step !== undefined ? { step: place.step } : {}),
|
|
61
|
+
...(selected?.block ? { block: selected.block } : {}),
|
|
62
|
+
...(selected ? { selection: selected.text } : {}),
|
|
63
|
+
}
|
|
64
|
+
startExchange(client, question, context)
|
|
65
|
+
setText('')
|
|
66
|
+
selectionStore.set(undefined)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function drop(): void {
|
|
70
|
+
selectionStore.set(undefined)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function go(citation: Citation): void {
|
|
74
|
+
showCitation(citation)
|
|
75
|
+
props.onCited?.()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<>
|
|
80
|
+
<div className="pm-assistant">
|
|
81
|
+
{!service || changing ? (
|
|
82
|
+
<AskSettings
|
|
83
|
+
onDone={() => {
|
|
84
|
+
setChanging(false)
|
|
85
|
+
}}
|
|
86
|
+
></AskSettings>
|
|
87
|
+
) : (
|
|
88
|
+
<>
|
|
89
|
+
<p className="pm-assistant__provider">
|
|
90
|
+
<span>{t.panel.provider(service.provider, service.model)}</span>
|
|
91
|
+
<PmButton
|
|
92
|
+
className="pm-assistant__service"
|
|
93
|
+
variant="ghost"
|
|
94
|
+
small
|
|
95
|
+
icon="sliders"
|
|
96
|
+
onClick={(event) => {
|
|
97
|
+
setChanging(true)
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
{t.panel.service}
|
|
101
|
+
</PmButton>
|
|
102
|
+
</p>
|
|
103
|
+
<form
|
|
104
|
+
className="pm-assistant__form"
|
|
105
|
+
onSubmit={(event) => {
|
|
106
|
+
event.preventDefault()
|
|
107
|
+
submit()
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
{selected ? (
|
|
111
|
+
<p className="pm-assistant__selection">
|
|
112
|
+
<span>{t.panel.selection}</span>
|
|
113
|
+
<code className="pm-ui-code">{excerpt}</code>
|
|
114
|
+
<PmButton variant="ghost" small onClick={drop}>
|
|
115
|
+
{t.panel.dropSelection}
|
|
116
|
+
</PmButton>
|
|
117
|
+
</p>
|
|
118
|
+
) : null}
|
|
119
|
+
{place ? <p className="pm-assistant__where">{t.panel.at(place.title)}</p> : null}
|
|
120
|
+
<textarea
|
|
121
|
+
value={text}
|
|
122
|
+
onChange={(event) => setText(event.target.value)}
|
|
123
|
+
className="pm-ui-textarea"
|
|
124
|
+
aria-label={t.panel.question}
|
|
125
|
+
placeholder={t.panel.placeholder}
|
|
126
|
+
rows={3}
|
|
127
|
+
maxLength={2000}
|
|
128
|
+
onKeyDown={(event) => {
|
|
129
|
+
event.stopPropagation()
|
|
130
|
+
}}
|
|
131
|
+
></textarea>
|
|
132
|
+
<p className="pm-assistant__actions">
|
|
133
|
+
<PmButton type="submit" variant="primary" small disabled={!text.trim() || running}>
|
|
134
|
+
{t.panel.ask}
|
|
135
|
+
</PmButton>
|
|
136
|
+
</p>
|
|
137
|
+
</form>
|
|
138
|
+
</>
|
|
139
|
+
)}
|
|
140
|
+
{exchanges.length ? (
|
|
141
|
+
<ol className="pm-assistant__answers" aria-label={t.panel.answers}>
|
|
142
|
+
{exchanges.map((exchange) => (
|
|
143
|
+
<li key={exchange.id} data-state={exchange.state}>
|
|
144
|
+
<p className="pm-assistant__question">{exchange.question}</p>
|
|
145
|
+
{exchange.answer ? (
|
|
146
|
+
<p className="pm-assistant__answer">
|
|
147
|
+
{answerParts(exchange.answer).map((part, j) => (
|
|
148
|
+
<>
|
|
149
|
+
{part.citation && canShowCitations ? (
|
|
150
|
+
<button
|
|
151
|
+
type="button"
|
|
152
|
+
className="pm-assistant__cite"
|
|
153
|
+
data-section={part.citation.section}
|
|
154
|
+
title={t.panel.cite(part.text)}
|
|
155
|
+
onClick={(event) => {
|
|
156
|
+
go(part.citation!)
|
|
157
|
+
}}
|
|
158
|
+
>
|
|
159
|
+
{part.text}
|
|
160
|
+
</button>
|
|
161
|
+
) : (
|
|
162
|
+
<span>{part.text}</span>
|
|
163
|
+
)}
|
|
164
|
+
</>
|
|
165
|
+
))}
|
|
166
|
+
</p>
|
|
167
|
+
) : null}
|
|
168
|
+
{exchange.state === 'running' ? (
|
|
169
|
+
<p className="pm-assistant__status" role="status">
|
|
170
|
+
{t.panel.answering}
|
|
171
|
+
<PmButton
|
|
172
|
+
variant="ghost"
|
|
173
|
+
small
|
|
174
|
+
onClick={(event) => {
|
|
175
|
+
exchange.cancel()
|
|
176
|
+
}}
|
|
177
|
+
>
|
|
178
|
+
{t.panel.cancel}
|
|
179
|
+
</PmButton>
|
|
180
|
+
</p>
|
|
181
|
+
) : exchange.cancelled ? (
|
|
182
|
+
<p className="pm-assistant__status">{t.panel.cancelled}</p>
|
|
183
|
+
) : exchange.state === 'failed' ? (
|
|
184
|
+
<p className="pm-assistant__error" role="alert">
|
|
185
|
+
{t.panel.failed(exchange.error ?? '')}
|
|
186
|
+
</p>
|
|
187
|
+
) : null}
|
|
188
|
+
</li>
|
|
189
|
+
))}
|
|
190
|
+
</ol>
|
|
191
|
+
) : null}
|
|
192
|
+
</div>
|
|
193
|
+
</>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
.pm-assistant-settings {
|
|
2
|
+
display: grid;
|
|
3
|
+
gap: var(--pm-space-3);
|
|
4
|
+
}
|
|
5
|
+
.pm-assistant-settings p {
|
|
6
|
+
margin: 0;
|
|
7
|
+
}
|
|
8
|
+
.pm-assistant-settings__title {
|
|
9
|
+
margin: 0;
|
|
10
|
+
font-family: var(--pm-font-serif);
|
|
11
|
+
font-size: var(--pm-text-title);
|
|
12
|
+
font-weight: 600;
|
|
13
|
+
line-height: var(--pm-leading-tight);
|
|
14
|
+
}
|
|
15
|
+
.pm-assistant-settings__intro {
|
|
16
|
+
color: var(--pm-color-muted);
|
|
17
|
+
}
|
|
18
|
+
.pm-assistant-settings__actions {
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-wrap: wrap;
|
|
21
|
+
gap: var(--pm-space-2);
|
|
22
|
+
}
|
|
23
|
+
.pm-assistant-settings__problem {
|
|
24
|
+
color: var(--pm-color-danger);
|
|
25
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Fragment, useState, useRef, useEffect, useMemo, useId } from 'react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { PmButton, PmField, PmNotice, useMessages, useStore } from '@lincyaw/host'
|
|
4
|
+
import { ASSISTANT_MESSAGES, isAskUrlValid } from '../domain/index.ts'
|
|
5
|
+
import { askSettings as settingsStore, saveAskSettings } from './settings.ts'
|
|
6
|
+
import './AskSettings.css'
|
|
7
|
+
export default function AskSettings(props: {
|
|
8
|
+
children?: ReactNode
|
|
9
|
+
className?: string
|
|
10
|
+
onDone?(): void
|
|
11
|
+
onCited?(): void
|
|
12
|
+
}) {
|
|
13
|
+
const t = useMessages(ASSISTANT_MESSAGES)
|
|
14
|
+
const askSettings = useStore(settingsStore)
|
|
15
|
+
const [url, setUrl] = useState(askSettings?.url ?? '')
|
|
16
|
+
const [key, setKey] = useState(askSettings?.key ?? '')
|
|
17
|
+
const [model, setModel] = useState(askSettings?.model ?? '')
|
|
18
|
+
const [problem, setProblem] = useState('')
|
|
19
|
+
|
|
20
|
+
function save(): void {
|
|
21
|
+
if (!isAskUrlValid(url.trim())) {
|
|
22
|
+
setProblem(t.ask.invalidUrl)
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
saveAskSettings({ url: url.trim(), key: key.trim(), model: model.trim() })
|
|
26
|
+
props.onDone?.()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function clear(): void {
|
|
30
|
+
saveAskSettings(undefined)
|
|
31
|
+
setUrl('')
|
|
32
|
+
setKey('')
|
|
33
|
+
setModel('')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<>
|
|
38
|
+
<form
|
|
39
|
+
className="pm-assistant-settings"
|
|
40
|
+
aria-label={t.ask.title}
|
|
41
|
+
onSubmit={(event) => {
|
|
42
|
+
event.preventDefault()
|
|
43
|
+
save()
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
<h3 className="pm-assistant-settings__title">{t.ask.title}</h3>
|
|
47
|
+
<p className="pm-assistant-settings__intro">{t.ask.intro}</p>
|
|
48
|
+
<PmNotice className="pm-assistant-settings__storage" tone="warn">
|
|
49
|
+
{t.ask.storage}
|
|
50
|
+
</PmNotice>
|
|
51
|
+
<PmField label={t.ask.url}>
|
|
52
|
+
<input
|
|
53
|
+
value={url}
|
|
54
|
+
onChange={(event) => setUrl(event.target.value)}
|
|
55
|
+
className="pm-ui-input"
|
|
56
|
+
name="url"
|
|
57
|
+
type="url"
|
|
58
|
+
required
|
|
59
|
+
placeholder={t.ask.urlPlaceholder}
|
|
60
|
+
autoComplete="off"
|
|
61
|
+
spellCheck={false}
|
|
62
|
+
onKeyDown={(event) => {
|
|
63
|
+
event.stopPropagation()
|
|
64
|
+
}}
|
|
65
|
+
></input>
|
|
66
|
+
</PmField>
|
|
67
|
+
<PmField label={t.ask.key}>
|
|
68
|
+
<input
|
|
69
|
+
value={key}
|
|
70
|
+
onChange={(event) => setKey(event.target.value)}
|
|
71
|
+
className="pm-ui-input"
|
|
72
|
+
name="key"
|
|
73
|
+
type="password"
|
|
74
|
+
required
|
|
75
|
+
autoComplete="off"
|
|
76
|
+
spellCheck={false}
|
|
77
|
+
onKeyDown={(event) => {
|
|
78
|
+
event.stopPropagation()
|
|
79
|
+
}}
|
|
80
|
+
></input>
|
|
81
|
+
</PmField>
|
|
82
|
+
<PmField label={t.ask.model}>
|
|
83
|
+
<input
|
|
84
|
+
value={model}
|
|
85
|
+
onChange={(event) => setModel(event.target.value)}
|
|
86
|
+
className="pm-ui-input"
|
|
87
|
+
name="model"
|
|
88
|
+
type="text"
|
|
89
|
+
required
|
|
90
|
+
placeholder={t.ask.modelPlaceholder}
|
|
91
|
+
autoComplete="off"
|
|
92
|
+
spellCheck={false}
|
|
93
|
+
onKeyDown={(event) => {
|
|
94
|
+
event.stopPropagation()
|
|
95
|
+
}}
|
|
96
|
+
></input>
|
|
97
|
+
</PmField>
|
|
98
|
+
{problem ? <p className="pm-assistant-settings__problem">{problem}</p> : null}
|
|
99
|
+
<p className="pm-assistant-settings__actions">
|
|
100
|
+
<PmButton type="submit" variant="primary" small>
|
|
101
|
+
{t.ask.save}
|
|
102
|
+
</PmButton>
|
|
103
|
+
{askSettings ? (
|
|
104
|
+
<PmButton
|
|
105
|
+
small
|
|
106
|
+
onClick={(event) => {
|
|
107
|
+
props.onDone?.()
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
{t.ask.cancel}
|
|
111
|
+
</PmButton>
|
|
112
|
+
) : null}
|
|
113
|
+
{askSettings ? (
|
|
114
|
+
<PmButton variant="danger" small onClick={clear}>
|
|
115
|
+
{t.ask.clear}
|
|
116
|
+
</PmButton>
|
|
117
|
+
) : null}
|
|
118
|
+
</p>
|
|
119
|
+
</form>
|
|
120
|
+
</>
|
|
121
|
+
)
|
|
122
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
.pm-assistant-toolbar {
|
|
2
|
+
display: inline-flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
}
|
|
5
|
+
.pm-assistant-popover__close {
|
|
6
|
+
margin-top: var(--pm-space-3);
|
|
7
|
+
}
|
|
8
|
+
.pm-assistant-popover {
|
|
9
|
+
box-sizing: border-box;
|
|
10
|
+
width: min(28rem, calc(100vw - 2rem));
|
|
11
|
+
max-height: 80vh;
|
|
12
|
+
margin: 0;
|
|
13
|
+
overflow: auto;
|
|
14
|
+
padding: var(--pm-space-4);
|
|
15
|
+
border: var(--pm-size-hairline) solid var(--pm-color-line);
|
|
16
|
+
border-radius: var(--pm-radius-card);
|
|
17
|
+
background: var(--pm-color-surface);
|
|
18
|
+
box-shadow: var(--pm-shadow-float);
|
|
19
|
+
white-space: normal;
|
|
20
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import { PmButton, PmPopover, useMessages } from '@lincyaw/host'
|
|
3
|
+
import { ASSISTANT_MESSAGES as MESSAGES } from '../domain/index.ts'
|
|
4
|
+
import { useWebViewerAccess } from './state.ts'
|
|
5
|
+
import AskPanel from './AskPanel.tsx'
|
|
6
|
+
import './AssistantToolbar.css'
|
|
7
|
+
export default function AssistantToolbar() {
|
|
8
|
+
const t = useMessages(MESSAGES)
|
|
9
|
+
const [open, setOpen] = useState(false)
|
|
10
|
+
useWebViewerAccess()
|
|
11
|
+
return (
|
|
12
|
+
<span className="pm-assistant-toolbar">
|
|
13
|
+
{true && (
|
|
14
|
+
<PmPopover
|
|
15
|
+
open={open}
|
|
16
|
+
onOpenChange={setOpen}
|
|
17
|
+
label={t.panel.title}
|
|
18
|
+
name="pm-assistant-popover"
|
|
19
|
+
trigger={
|
|
20
|
+
<PmButton variant="ghost" small title={t.toolbar.title} aria-expanded={open}>
|
|
21
|
+
{t.toolbar.button}
|
|
22
|
+
</PmButton>
|
|
23
|
+
}
|
|
24
|
+
>
|
|
25
|
+
<AskPanel onCited={() => setOpen(false)} />
|
|
26
|
+
<PmButton small className="pm-assistant-popover__close" onClick={() => setOpen(false)}>
|
|
27
|
+
{t.panel.close}
|
|
28
|
+
</PmButton>
|
|
29
|
+
</PmPopover>
|
|
30
|
+
)}
|
|
31
|
+
</span>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { EventSourceParserStream } from 'eventsource-parser/stream'
|
|
2
|
+
import { currentLocale } from '@lincyaw/host'
|
|
3
|
+
import type { Navigation } from '@lincyaw/host'
|
|
4
|
+
import type { AgentContext, AskClient } from '@lincyaw/kernel'
|
|
5
|
+
import { askMessages, askProvider, completionsUrl, completionText, sectionText, serviceError, streamDelta } from '../domain/index.ts'
|
|
6
|
+
import type { AskPlace } from '../domain/index.ts'
|
|
7
|
+
import { askSettings } from './settings.ts'
|
|
8
|
+
import type { WebViewerAccess } from './state.ts'
|
|
9
|
+
|
|
10
|
+
function placeOf(context: AgentContext, webViewer: WebViewerAccess | undefined, slideNavigation: Navigation | undefined): AskPlace {
|
|
11
|
+
const selection = context.selection ? { selection: context.selection } : {}
|
|
12
|
+
const doc = webViewer?.doc()
|
|
13
|
+
if (doc) {
|
|
14
|
+
const section = context.section === undefined ? undefined : doc.sections.find(s => s.id === context.section)
|
|
15
|
+
return {
|
|
16
|
+
title: doc.meta.title,
|
|
17
|
+
lang: doc.meta.lang,
|
|
18
|
+
sections: doc.sections.map(s => ({ id: s.id, title: s.title })),
|
|
19
|
+
...section ? { section: { id: section.id, title: section.title, ...context.step !== undefined ? { step: context.step } : {}, text: sectionText(section) } } : {},
|
|
20
|
+
...selection,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const position = slideNavigation?.position.getSnapshot()
|
|
24
|
+
const page = position && typeof document !== 'undefined' ? document.querySelector<HTMLElement>(`.pm-slide[data-slide="${position.slide}"]`) : undefined
|
|
25
|
+
const text = page?.innerText.trim() ?? ''
|
|
26
|
+
const id = position?.section ?? context.section
|
|
27
|
+
const title = page?.querySelector('h1, h2, h3')?.textContent?.trim() || document.title
|
|
28
|
+
return {
|
|
29
|
+
lang: currentLocale(),
|
|
30
|
+
sections: id ? [{ id, title }] : [],
|
|
31
|
+
...id && text ? { section: { id, title, text } } : {},
|
|
32
|
+
...selection,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function* streamed(body: ReadableStream<Uint8Array<ArrayBuffer>>): AsyncIterable<string> {
|
|
37
|
+
const reader = body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).getReader()
|
|
38
|
+
try {
|
|
39
|
+
for (;;) {
|
|
40
|
+
const { value, done } = await reader.read()
|
|
41
|
+
if (done) break
|
|
42
|
+
const delta = streamDelta(value.data)
|
|
43
|
+
if (delta.done) return
|
|
44
|
+
if (delta.text) yield delta.text
|
|
45
|
+
}
|
|
46
|
+
} finally {
|
|
47
|
+
await reader.cancel().catch(() => {})
|
|
48
|
+
reader.releaseLock()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ChatClientOptions {
|
|
53
|
+
webViewer(): WebViewerAccess | undefined
|
|
54
|
+
slideNavigation(): Navigation | undefined
|
|
55
|
+
missing(): string
|
|
56
|
+
signal?: AbortSignal
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function createAskClient(options: ChatClientOptions): AskClient {
|
|
60
|
+
return {
|
|
61
|
+
provider: () => (askSettings.getSnapshot() ? askProvider(askSettings.getSnapshot()!.url) : undefined),
|
|
62
|
+
async* ask(question, context, signal) {
|
|
63
|
+
const settings = askSettings.getSnapshot()
|
|
64
|
+
if (!settings) throw new Error(options.missing())
|
|
65
|
+
const requestSignal = signal && options.signal ? AbortSignal.any([signal, options.signal]) : signal ?? options.signal
|
|
66
|
+
const response = await fetch(completionsUrl(settings.url), {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: { 'Content-Type': 'application/json', 'Accept': 'text/event-stream', 'Authorization': `Bearer ${settings.key}` },
|
|
69
|
+
body: JSON.stringify({ model: settings.model, stream: true, messages: askMessages(question, placeOf(context, options.webViewer(), options.slideNavigation())) }),
|
|
70
|
+
credentials: 'omit',
|
|
71
|
+
referrerPolicy: 'no-referrer',
|
|
72
|
+
...requestSignal ? { signal: requestSignal } : {},
|
|
73
|
+
})
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
const body = await response.text().catch(() => '')
|
|
76
|
+
requestSignal?.throwIfAborted()
|
|
77
|
+
throw new Error(serviceError(response.status, body))
|
|
78
|
+
}
|
|
79
|
+
if (!response.headers.get('content-type')?.includes('text/event-stream') || !response.body) {
|
|
80
|
+
const body = await response.json().catch(() => undefined)
|
|
81
|
+
requestSignal?.throwIfAborted()
|
|
82
|
+
const text = completionText(body)
|
|
83
|
+
if (text === undefined) throw new Error(serviceError(response.status, ''))
|
|
84
|
+
yield text
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
yield* streamed(response.body)
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { assistantPlugin } from './plugin.ts'
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { currentLocale, NAVIGATION, TOOLBAR } from '@lincyaw/host'
|
|
2
|
+
import { ASK, PRESENTER_PANELS } from '@lincyaw/kernel'
|
|
3
|
+
import type { PluginDefinition, Unsubscribe } from '@lincyaw/kernel'
|
|
4
|
+
import { version } from '../../package.json'
|
|
5
|
+
import { ASSISTANT_ID, ASSISTANT_MESSAGES } from '../domain/index.ts'
|
|
6
|
+
import AskPanel from './AskPanel.tsx'
|
|
7
|
+
import AssistantToolbar from './AssistantToolbar.tsx'
|
|
8
|
+
import { createAskClient } from './chat-client.ts'
|
|
9
|
+
import { askClient, keptWebViewerAccess, slideNavigation, trackSelection } from './state.ts'
|
|
10
|
+
|
|
11
|
+
let cleanup: Unsubscribe[] = []
|
|
12
|
+
|
|
13
|
+
export const assistantPlugin: PluginDefinition = {
|
|
14
|
+
manifest: {
|
|
15
|
+
id: ASSISTANT_ID,
|
|
16
|
+
name: { 'zh-CN': '读者问答', en: 'Reader Q&A' },
|
|
17
|
+
version,
|
|
18
|
+
kernel: '^1.0.0',
|
|
19
|
+
provides: [ASK],
|
|
20
|
+
requires: [{ token: TOOLBAR, optional: true }, { token: PRESENTER_PANELS, optional: true }, { token: NAVIGATION, optional: true }],
|
|
21
|
+
},
|
|
22
|
+
setup(ctx) {
|
|
23
|
+
const lifetime = new AbortController()
|
|
24
|
+
slideNavigation.set(ctx.tryGet(NAVIGATION))
|
|
25
|
+
const ask = createAskClient({
|
|
26
|
+
webViewer: keptWebViewerAccess,
|
|
27
|
+
slideNavigation: () => slideNavigation.getSnapshot(),
|
|
28
|
+
missing: () => ASSISTANT_MESSAGES[currentLocale()].ask.missing,
|
|
29
|
+
signal: lifetime.signal,
|
|
30
|
+
})
|
|
31
|
+
ctx.provide(ASK, ask)
|
|
32
|
+
askClient.set(ask)
|
|
33
|
+
const t = ASSISTANT_MESSAGES[currentLocale()]
|
|
34
|
+
cleanup = [
|
|
35
|
+
() => lifetime.abort(),
|
|
36
|
+
ctx.tryGet(TOOLBAR)?.register({ id: ASSISTANT_ID, title: t.toolbar.title, component: AssistantToolbar, order: 15 }),
|
|
37
|
+
ctx.tryGet(PRESENTER_PANELS)?.register({ id: ASSISTANT_ID, title: t.panel.title, component: AskPanel, order: 15 }),
|
|
38
|
+
typeof document === 'undefined' ? undefined : trackSelection(),
|
|
39
|
+
].filter(stop => stop !== undefined)
|
|
40
|
+
},
|
|
41
|
+
dispose() {
|
|
42
|
+
cleanup.forEach(stop => stop())
|
|
43
|
+
cleanup = []
|
|
44
|
+
askClient.set(undefined)
|
|
45
|
+
slideNavigation.set(undefined)
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default assistantPlugin
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createStore } from '@lincyaw/host'
|
|
2
|
+
import { ASK_STORAGE_KEY, toAskSettings } from '../domain/index.ts'
|
|
3
|
+
import type { AskSettings } from '../domain/index.ts'
|
|
4
|
+
|
|
5
|
+
function storedSettings(): AskSettings | undefined {
|
|
6
|
+
try {
|
|
7
|
+
return toAskSettings(JSON.parse(localStorage.getItem(ASK_STORAGE_KEY) ?? 'null'))
|
|
8
|
+
} catch {
|
|
9
|
+
return undefined
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const askSettings = createStore<AskSettings | undefined>(typeof window === 'undefined' ? undefined : storedSettings())
|
|
14
|
+
|
|
15
|
+
export function saveAskSettings(settings: AskSettings | undefined): void {
|
|
16
|
+
askSettings.set(settings)
|
|
17
|
+
try {
|
|
18
|
+
if (settings) localStorage.setItem(ASK_STORAGE_KEY, JSON.stringify(settings))
|
|
19
|
+
else localStorage.removeItem(ASK_STORAGE_KEY)
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createStore } from '@lincyaw/host'
|
|
2
|
+
import type { Navigation } from '@lincyaw/host'
|
|
3
|
+
import type { AgentContext, AskClient, Citation, IRDocument } from '@lincyaw/kernel'
|
|
4
|
+
import { errorMessage } from '@lincyaw/kernel'
|
|
5
|
+
export const askClient = createStore<AskClient | undefined>(undefined)
|
|
6
|
+
export const slideNavigation = createStore<Navigation | undefined>(undefined)
|
|
7
|
+
export interface WebViewerAccess { doc(): IRDocument, position(): { section: string, step: number } | undefined, title(section: string): string | undefined, show(citation: Citation): void }
|
|
8
|
+
let resolveWebViewer: (() => WebViewerAccess | undefined) | undefined
|
|
9
|
+
let keptWebViewer: WebViewerAccess | undefined
|
|
10
|
+
export function setWebViewerAccess(resolve: () => WebViewerAccess | undefined) { resolveWebViewer = resolve; keptWebViewer = undefined }
|
|
11
|
+
export function useWebViewerAccess(): WebViewerAccess | undefined { try { return keptWebViewer = resolveWebViewer?.() } catch { return undefined } }
|
|
12
|
+
export function keptWebViewerAccess() { return keptWebViewer }
|
|
13
|
+
export function showCitation(citation: Citation) {
|
|
14
|
+
if (keptWebViewer) return keptWebViewer.show(citation)
|
|
15
|
+
const go = slideNavigation.getSnapshot()
|
|
16
|
+
if (go) go.go({ slide: go.position.getSnapshot().slide, step: citation.step ?? 0, section: citation.section })
|
|
17
|
+
}
|
|
18
|
+
export interface Selected { text: string, section?: string, block?: string }
|
|
19
|
+
export const selected = createStore<Selected | undefined>(undefined)
|
|
20
|
+
export function trackSelection(): () => void {
|
|
21
|
+
const update = () => {
|
|
22
|
+
const selection = document.getSelection()
|
|
23
|
+
if (!selection || selection.isCollapsed || selection.rangeCount === 0) return
|
|
24
|
+
const text = selection.toString().trim()
|
|
25
|
+
const node = selection.getRangeAt(0).commonAncestorContainer
|
|
26
|
+
const element = node instanceof Element ? node : node.parentElement
|
|
27
|
+
if (!text || element?.closest('.pm-assistant')) return
|
|
28
|
+
const section = element?.closest<HTMLElement>('[data-section]')?.dataset.section
|
|
29
|
+
if (!section) return
|
|
30
|
+
const block = element?.closest<HTMLElement>('[data-block]')
|
|
31
|
+
selected.set({ text: text.slice(0,4000), section, ...block?.dataset.block ? { block: block.dataset.block } : {} })
|
|
32
|
+
}
|
|
33
|
+
document.addEventListener('selectionchange', update)
|
|
34
|
+
return () => document.removeEventListener('selectionchange', update)
|
|
35
|
+
}
|
|
36
|
+
export interface Exchange { id: number, question: string, answer: string, state: 'running' | 'done' | 'failed', error?: string, cancelled?: boolean, cancel(): void }
|
|
37
|
+
export const exchanges = createStore<Exchange[]>([])
|
|
38
|
+
let asked = 0
|
|
39
|
+
export function startExchange(client: AskClient, question: string, context: AgentContext) {
|
|
40
|
+
const stop = new AbortController()
|
|
41
|
+
const id = ++asked
|
|
42
|
+
const update = (patch: Partial<Exchange>) => exchanges.set(exchanges.getSnapshot().map(e => e.id === id ? { ...e, ...patch } : e))
|
|
43
|
+
const exchange: Exchange = { id, question, answer: '', state: 'running', cancel: () => { update({ cancelled: true }); stop.abort() } }
|
|
44
|
+
exchanges.set([exchange, ...exchanges.getSnapshot()])
|
|
45
|
+
void (async () => {
|
|
46
|
+
let answer = ''
|
|
47
|
+
try { for await (const piece of client.ask(question, context, stop.signal)) { answer += piece; update({ answer }) }; update({ state: 'done' }) }
|
|
48
|
+
catch (error) {
|
|
49
|
+
const cancelled = stop.signal.aborted || (error instanceof Error && error.name === 'AbortError')
|
|
50
|
+
update(cancelled ? { state: 'done', cancelled: true } : { state: 'failed', error: errorMessage(error) })
|
|
51
|
+
}
|
|
52
|
+
})()
|
|
53
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useStore } from '@lincyaw/host'
|
|
2
|
+
import type { ViewerPlugin } from '@lincyaw/viewer'
|
|
3
|
+
import { setWebViewerAccess } from './state.ts'
|
|
4
|
+
|
|
5
|
+
const plugin: ViewerPlugin = (ctx) => {
|
|
6
|
+
setWebViewerAccess(() => {
|
|
7
|
+
const viewer = ctx.useViewer()
|
|
8
|
+
useStore(viewer.doc)
|
|
9
|
+
useStore(viewer.position)
|
|
10
|
+
return {
|
|
11
|
+
doc: () => viewer.doc.getSnapshot(),
|
|
12
|
+
position: () => viewer.position.getSnapshot(),
|
|
13
|
+
title: section => viewer.section(section)?.title,
|
|
14
|
+
show: citation => viewer.show({ section: citation.section, step: citation.step ?? 0 }),
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default plugin
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Citation } from '@lincyaw/kernel'
|
|
2
|
+
|
|
3
|
+
const LINK = /\[([^\]\n]+)\]\(presmium:\/\/sections\/([^)\s/#?]+)(?:\/steps\/(\d+))?(?:#([^)\s]+))?\)/g
|
|
4
|
+
|
|
5
|
+
function decoded(value: string): string {
|
|
6
|
+
try {
|
|
7
|
+
return decodeURIComponent(value)
|
|
8
|
+
} catch {
|
|
9
|
+
return value
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type AnswerPart = { text: string, citation?: Citation }
|
|
14
|
+
|
|
15
|
+
export function answerParts(text: string): AnswerPart[] {
|
|
16
|
+
const parts: AnswerPart[] = []
|
|
17
|
+
let at = 0
|
|
18
|
+
for (const match of text.matchAll(LINK)) {
|
|
19
|
+
if (match.index > at) parts.push({ text: text.slice(at, match.index) })
|
|
20
|
+
const [, label, section, step, block] = match
|
|
21
|
+
const citation: Citation = { label: label!, section: decoded(section!), ...step === undefined ? {} : { step: Number(step) }, ...block ? { block: decoded(block) } : {} }
|
|
22
|
+
parts.push({ text: label!, citation })
|
|
23
|
+
at = match.index + match[0].length
|
|
24
|
+
}
|
|
25
|
+
if (at < text.length) parts.push({ text: text.slice(at) })
|
|
26
|
+
return parts
|
|
27
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type StreamDelta = { done: true } | { done?: false, text?: string }
|
|
2
|
+
|
|
3
|
+
export function streamDelta(data: string): StreamDelta {
|
|
4
|
+
const value = data.trim()
|
|
5
|
+
if (value === '[DONE]') return { done: true }
|
|
6
|
+
try {
|
|
7
|
+
const parsed = JSON.parse(value) as { choices?: { delta?: { content?: unknown } }[], error?: { message?: unknown } }
|
|
8
|
+
if (parsed.error) throw new Error(typeof parsed.error.message === 'string' ? parsed.error.message : JSON.stringify(parsed.error))
|
|
9
|
+
const content = parsed.choices?.[0]?.delta?.content
|
|
10
|
+
return typeof content === 'string' && content ? { text: content } : {}
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (error instanceof SyntaxError) return {}
|
|
13
|
+
throw error
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function completionText(body: unknown): string | undefined {
|
|
18
|
+
const content = (body as { choices?: { message?: { content?: unknown } }[] } | undefined)?.choices?.[0]?.message?.content
|
|
19
|
+
return typeof content === 'string' ? content : undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function serviceError(status: number, text: string): string {
|
|
23
|
+
try {
|
|
24
|
+
const message = (JSON.parse(text) as { error?: { message?: unknown } }).error?.message
|
|
25
|
+
if (typeof message === 'string' && message) return `${status}: ${message}`
|
|
26
|
+
} catch {
|
|
27
|
+
}
|
|
28
|
+
return `${status}${text.trim() ? `: ${text.trim().slice(0, 300)}` : ''}`
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { answerParts } from './citations.ts'
|
|
2
|
+
export type { AnswerPart } from './citations.ts'
|
|
3
|
+
export { completionText, serviceError, streamDelta } from './completion.ts'
|
|
4
|
+
export type { StreamDelta } from './completion.ts'
|
|
5
|
+
export { ASSISTANT_MESSAGES } from './messages.ts'
|
|
6
|
+
export type { AssistantMessages } from './messages.ts'
|
|
7
|
+
export { askMessages, promptLanguage, sectionText } from './prompt.ts'
|
|
8
|
+
export type { AskPlace } from './prompt.ts'
|
|
9
|
+
export { ASK_STORAGE_KEY, askProvider, completionsUrl, isAskUrlValid, toAskSettings } from './settings.ts'
|
|
10
|
+
export type { AskSettings } from './settings.ts'
|
|
11
|
+
|
|
12
|
+
export const ASSISTANT_ID = 'presmium.assistant'
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Locale } from '@lincyaw/kernel'
|
|
2
|
+
|
|
3
|
+
const zh = {
|
|
4
|
+
toolbar: { button: '提问', title: '向 AI 提问(关于当前页面或所选内容),使用你自己的模型服务' },
|
|
5
|
+
panel: {
|
|
6
|
+
title: '向 AI 提问',
|
|
7
|
+
close: '关闭',
|
|
8
|
+
provider: (provider: string, model: string) => `回答由 ${provider} 的模型 ${model} 生成`,
|
|
9
|
+
service: '模型服务',
|
|
10
|
+
question: '问题',
|
|
11
|
+
placeholder: '想问什么?',
|
|
12
|
+
selection: '附上所选内容',
|
|
13
|
+
dropSelection: '不附上',
|
|
14
|
+
at: (where: string) => `关于:${where}`,
|
|
15
|
+
ask: '提问',
|
|
16
|
+
cancel: '取消',
|
|
17
|
+
cancelled: '已取消',
|
|
18
|
+
answering: '正在回答…',
|
|
19
|
+
failed: (message: string) => `无法获得回答:${message}`,
|
|
20
|
+
answers: '回答',
|
|
21
|
+
cite: (label: string) => `跳到“${label}”`,
|
|
22
|
+
},
|
|
23
|
+
ask: {
|
|
24
|
+
title: '你的模型服务',
|
|
25
|
+
intro: '提问由你自己填写的模型服务回答:OpenAI 兼容的 Chat Completions 接口的地址、密钥与模型名。问题与当前页面的内容从这个浏览器直接发给该服务,不经过本站;费用由该服务向你收取。',
|
|
26
|
+
storage: '设置只保存在这个浏览器中(本站点的存储)。同一站点上的脚本能读到密钥,请只在你信任的站点上填写。',
|
|
27
|
+
url: '地址',
|
|
28
|
+
urlPlaceholder: 'https://api.deepseek.com/v1',
|
|
29
|
+
key: '密钥',
|
|
30
|
+
model: '模型名',
|
|
31
|
+
modelPlaceholder: 'deepseek-chat',
|
|
32
|
+
save: '保存',
|
|
33
|
+
cancel: '取消',
|
|
34
|
+
clear: '清除设置',
|
|
35
|
+
invalidUrl: '地址须以 https:// 或 http:// 开头。',
|
|
36
|
+
missing: '尚未设置模型服务。',
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Messages = typeof zh
|
|
41
|
+
|
|
42
|
+
const en: Messages = {
|
|
43
|
+
toolbar: { button: 'Ask', title: 'Ask the AI about this page or the selection, using your own model service' },
|
|
44
|
+
panel: {
|
|
45
|
+
title: 'Ask the AI',
|
|
46
|
+
close: 'Close',
|
|
47
|
+
provider: (provider, model) => `Answers come from ${provider}’s model ${model}`,
|
|
48
|
+
service: 'Model service',
|
|
49
|
+
question: 'Question',
|
|
50
|
+
placeholder: 'What would you like to know?',
|
|
51
|
+
selection: 'Include the selection',
|
|
52
|
+
dropSelection: 'Don’t Include',
|
|
53
|
+
at: where => `About: ${where}`,
|
|
54
|
+
ask: 'Ask',
|
|
55
|
+
cancel: 'Cancel',
|
|
56
|
+
cancelled: 'Canceled',
|
|
57
|
+
answering: 'Answering…',
|
|
58
|
+
failed: message => `Couldn’t get an answer: ${message}`,
|
|
59
|
+
answers: 'Answers',
|
|
60
|
+
cite: label => `Go to “${label}”`,
|
|
61
|
+
},
|
|
62
|
+
ask: {
|
|
63
|
+
title: 'Your Model Service',
|
|
64
|
+
intro: 'Your own model service answers: the address, key, and model of an OpenAI-compatible Chat Completions API. The question and this page’s content go from this browser to that service directly, not through this site; the service bills you.',
|
|
65
|
+
storage: 'The settings stay in this browser (this site’s storage). Scripts on the same site can read the key, so fill it in only on sites you trust.',
|
|
66
|
+
url: 'Address',
|
|
67
|
+
urlPlaceholder: 'https://api.deepseek.com/v1',
|
|
68
|
+
key: 'API key',
|
|
69
|
+
model: 'Model',
|
|
70
|
+
modelPlaceholder: 'deepseek-chat',
|
|
71
|
+
save: 'Save',
|
|
72
|
+
cancel: 'Cancel',
|
|
73
|
+
clear: 'Clear Settings',
|
|
74
|
+
invalidUrl: 'The address must start with https:// or http://.',
|
|
75
|
+
missing: 'No model service is set up yet.',
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type AssistantMessages = Messages
|
|
80
|
+
|
|
81
|
+
export const ASSISTANT_MESSAGES: Record<Locale, AssistantMessages> = { 'zh-CN': zh, 'en': en }
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { blockText } from '@lincyaw/kernel'
|
|
2
|
+
import type { IRSection } from '@lincyaw/kernel'
|
|
3
|
+
|
|
4
|
+
const LANGUAGES: Record<string, string> = { zh: 'Simplified Chinese (zh-CN)', en: 'English' }
|
|
5
|
+
|
|
6
|
+
export function promptLanguage(lang: string | undefined): string {
|
|
7
|
+
const tag = lang?.trim() || 'zh-CN'
|
|
8
|
+
return LANGUAGES[tag.toLowerCase().split('-')[0]!] ?? tag
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface AskPlace {
|
|
12
|
+
title?: string
|
|
13
|
+
lang?: string
|
|
14
|
+
sections: { id: string, title: string }[]
|
|
15
|
+
section?: { id: string, title: string, step?: number, text: string }
|
|
16
|
+
selection?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const SECTION_CHARS = 12_000
|
|
20
|
+
const SELECTION_CHARS = 4000
|
|
21
|
+
const TOC_SECTIONS = 400
|
|
22
|
+
|
|
23
|
+
export function sectionText(section: IRSection): string {
|
|
24
|
+
return section.blocks.map(blockText).filter(Boolean).join('\n\n').slice(0, SECTION_CHARS)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const cut = (text: string, max: number) => (text.length > max ? `${text.slice(0, max)}…` : text)
|
|
28
|
+
|
|
29
|
+
export function askMessages(question: string, place: AskPlace): { role: 'system' | 'user', content: string }[] {
|
|
30
|
+
const lines = [
|
|
31
|
+
`A reader of a course document${place.title ? ` ("${place.title}")` : ''} asks a question. Answer from the content below, which is what the reader's page shows; say so when it does not tell.`,
|
|
32
|
+
'When your answer relies on a section, link it as [its title](presmium://sections/<section id>), with an id of the table of contents; the reader can follow these links.',
|
|
33
|
+
`Answer in ${promptLanguage(place.lang)}, briefly.`,
|
|
34
|
+
]
|
|
35
|
+
if (place.sections.length) {
|
|
36
|
+
lines.push('', 'Table of contents (section id: title):', ...place.sections.slice(0, TOC_SECTIONS).map(s => `- ${s.id}: ${s.title}`))
|
|
37
|
+
}
|
|
38
|
+
if (place.section) {
|
|
39
|
+
const { id, title, step, text } = place.section
|
|
40
|
+
lines.push('', `The section the reader is at: ${title} (id ${id}${step !== undefined ? `, step ${step}` : ''}). Its content:`, '"""', cut(text, SECTION_CHARS), '"""')
|
|
41
|
+
}
|
|
42
|
+
if (place.selection) lines.push('', 'The reader selected this on the page:', '"""', cut(place.selection, SELECTION_CHARS), '"""')
|
|
43
|
+
return [{ role: 'system', content: lines.join('\n') }, { role: 'user', content: question }]
|
|
44
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface AskSettings {
|
|
2
|
+
url: string
|
|
3
|
+
key: string
|
|
4
|
+
model: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const ASK_STORAGE_KEY = 'presmium.assistant.ask'
|
|
8
|
+
|
|
9
|
+
export function toAskSettings(value: unknown): AskSettings | undefined {
|
|
10
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
11
|
+
const { url, key, model } = value as Record<string, unknown>
|
|
12
|
+
const text = (v: unknown) => (typeof v === 'string' && v.trim() ? v.trim() : undefined)
|
|
13
|
+
const settings = { url: text(url), key: text(key), model: text(model) }
|
|
14
|
+
return settings.url && settings.key && settings.model && isAskUrlValid(settings.url) ? settings as AskSettings : undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isAskUrlValid(url: string): boolean {
|
|
18
|
+
try {
|
|
19
|
+
return ['https:', 'http:'].includes(new URL(url).protocol)
|
|
20
|
+
} catch {
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function completionsUrl(url: string): string {
|
|
26
|
+
const base = url.trim().replace(/\/+$/, '')
|
|
27
|
+
return /\/chat\/completions$/.test(base) ? base : `${base}/chat/completions`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function askProvider(url: string): string {
|
|
31
|
+
try {
|
|
32
|
+
return new URL(url).host
|
|
33
|
+
} catch {
|
|
34
|
+
return url
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import type { PresmiumNodePlugin } from '@lincyaw/kernel'
|
|
3
|
+
import { ASSISTANT_ID } from '../domain/index.ts'
|
|
4
|
+
|
|
5
|
+
const { version } = createRequire(import.meta.url)('../../package.json') as { version: string }
|
|
6
|
+
|
|
7
|
+
const plugin: PresmiumNodePlugin = {
|
|
8
|
+
id: ASSISTANT_ID,
|
|
9
|
+
version,
|
|
10
|
+
kernel: '^1.0.0',
|
|
11
|
+
operator: true,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default plugin
|