@molecule/api-ai-summarization-llm 1.1.0 → 1.1.2
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 +111 -24
- package/README.md +86 -50
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/provider.d.ts +26 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +61 -25
- package/package.json +3 -3
package/LICENSE
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
Apache License
|
|
2
3
|
Version 2.0, January 2004
|
|
3
4
|
http://www.apache.org/licenses/
|
|
@@ -34,21 +35,33 @@
|
|
|
34
35
|
|
|
35
36
|
"Work" shall mean the work of authorship, whether in Source or
|
|
36
37
|
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
38
40
|
|
|
39
41
|
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
42
|
form, that is based on (or derived from) the Work and for which the
|
|
41
43
|
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
-
represent, as a whole, an original work of authorship.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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."
|
|
49
62
|
|
|
50
63
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
-
on behalf of whom a Contribution has been received by
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
52
65
|
subsequently incorporated within the Work.
|
|
53
66
|
|
|
54
67
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
@@ -61,8 +74,18 @@
|
|
|
61
74
|
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
75
|
this License, each Contributor hereby grants to You a perpetual,
|
|
63
76
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
-
|
|
65
|
-
and otherwise transfer the Work
|
|
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.
|
|
66
89
|
|
|
67
90
|
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
91
|
Work or Derivative Works thereof in any medium, with or without
|
|
@@ -84,23 +107,87 @@
|
|
|
84
107
|
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
108
|
distribution, then any Derivative Works that You distribute must
|
|
86
109
|
include a readable copy of the attribution notices contained
|
|
87
|
-
within such NOTICE file
|
|
88
|
-
|
|
89
|
-
|
|
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.
|
|
90
138
|
|
|
91
139
|
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
-
names, trademarks, service marks, or product names of the Licensor
|
|
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.
|
|
93
143
|
|
|
94
144
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
-
agreed to in writing, Licensor provides the Work
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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]
|
|
104
191
|
|
|
105
192
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
193
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -3,15 +3,11 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-23T02:59:53.479Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-summarization-llm
|
|
10
10
|
|
|
11
|
-
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
-
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
-
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
-
|
|
15
11
|
Default (`llm`) AI summarization provider for molecule.dev.
|
|
16
12
|
|
|
17
13
|
Turns a block of text into a concise summary over whatever LLM the app has
|
|
@@ -19,6 +15,10 @@ bonded. It has no vendor of its own — it composes the swappable `ai` chat
|
|
|
19
15
|
bond (`@molecule/api-ai`) with a summarizer system prompt built from the
|
|
20
16
|
requested format / length / focus, so swapping the LLM swaps the summarizer.
|
|
21
17
|
|
|
18
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
19
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
20
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
21
|
+
|
|
22
22
|
## Quick Start
|
|
23
23
|
|
|
24
24
|
```typescript
|
|
@@ -109,20 +109,86 @@ interface SummaryCache {
|
|
|
109
109
|
}
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
+
#### `LlmSummarizationConfig`
|
|
113
|
+
|
|
114
|
+
Configuration for {@link createProvider}.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
interface LlmSummarizationConfig {
|
|
118
|
+
/**
|
|
119
|
+
* The `ai` provider this provider composes: an already-resolved provider
|
|
120
|
+
* instance, or the name of one to resolve at call time. Defaults to the
|
|
121
|
+
* bonded singleton of `@molecule/api-ai`.
|
|
122
|
+
*/
|
|
123
|
+
aiProvider?: AIProvider | string
|
|
124
|
+
/** Default model for every summarize call. A call's own `model` wins. */
|
|
125
|
+
model?: string
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
112
129
|
### Functions
|
|
113
130
|
|
|
114
|
-
#### `
|
|
131
|
+
#### `summaryKey(input)`
|
|
115
132
|
|
|
116
|
-
|
|
117
|
-
|
|
133
|
+
The cache key for a request: a SHA-256 of the text (whitespace-normalized)
|
|
134
|
+
and every option that changes the answer.
|
|
118
135
|
|
|
119
136
|
```typescript
|
|
120
|
-
function
|
|
137
|
+
function summaryKey(input: SummarizeInput): string
|
|
121
138
|
```
|
|
122
139
|
|
|
123
|
-
- `
|
|
140
|
+
- `input` — The summarize request.
|
|
124
141
|
|
|
125
|
-
**Returns:**
|
|
142
|
+
**Returns:** A 64-char hex key.
|
|
143
|
+
|
|
144
|
+
#### `createFileSummaryCache(file)`
|
|
145
|
+
|
|
146
|
+
Opens (or starts) a summary cache stored at `file`.
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
function createFileSummaryCache(file: string): SummaryCache
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- `file` — Path of the JSON file, e.g. `.cache/summaries.json`.
|
|
153
|
+
|
|
154
|
+
**Returns:** The cache.
|
|
155
|
+
|
|
156
|
+
#### `summarizeCached(input, options)`
|
|
157
|
+
|
|
158
|
+
Summarize through the cache: a hit returns without a model call; a miss
|
|
159
|
+
summarizes and stores the result. A summary that did not meet its caps
|
|
160
|
+
(`withinCap: false`) is returned but not stored, so the next run tries again.
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
function summarizeCached(
|
|
164
|
+
input: SummarizeInput,
|
|
165
|
+
options: { cache: SummaryCache; summarizer?: AISummarizationProvider },
|
|
166
|
+
): Promise<SummarizeResult & { cached: boolean; key: string }>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- `input` — The summarize request.
|
|
170
|
+
- `options` — The cache, and optionally a provider (defaults to the bonded one).
|
|
171
|
+
- `options.cache` — The cache to read and fill.
|
|
172
|
+
- `options.summarizer` — A provider to use instead of the bonded one.
|
|
173
|
+
|
|
174
|
+
**Returns:** The result, with `cached: true` on a hit and `key` for publishing.
|
|
175
|
+
|
|
176
|
+
#### `createProvider(config)`
|
|
177
|
+
|
|
178
|
+
Creates an LLM summarization provider.
|
|
179
|
+
|
|
180
|
+
Composes the `ai` chat provider (`@molecule/api-ai`) — an AI provider MUST be
|
|
181
|
+
bonded first (`bond('ai', <provider>)`), or `summarize()` throws, unless
|
|
182
|
+
`config.aiProvider` supplies one directly (how a hosted handler injects its
|
|
183
|
+
own provider). The module-level `provider` is this factory with no config.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
function createProvider(config?: LlmSummarizationConfig): AISummarizationProvider
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
- `config` — Optional `ai` provider and default model.
|
|
190
|
+
|
|
191
|
+
**Returns:** An `AISummarizationProvider` composed over the `ai` bond.
|
|
126
192
|
|
|
127
193
|
#### `countWords(s)`
|
|
128
194
|
|
|
@@ -136,17 +202,18 @@ function countWords(s: string): number
|
|
|
136
202
|
|
|
137
203
|
**Returns:** The number of whitespace-separated words.
|
|
138
204
|
|
|
139
|
-
#### `
|
|
205
|
+
#### `cleanSummary(raw)`
|
|
140
206
|
|
|
141
|
-
|
|
207
|
+
Strips what models wrap a summary in: reasoning blocks, a "TL;DR:" or
|
|
208
|
+
"Summary:" label, surrounding quotes or bold, and line breaks.
|
|
142
209
|
|
|
143
210
|
```typescript
|
|
144
|
-
function
|
|
211
|
+
function cleanSummary(raw: string): string
|
|
145
212
|
```
|
|
146
213
|
|
|
147
|
-
- `
|
|
214
|
+
- `raw` — The model's answer.
|
|
148
215
|
|
|
149
|
-
**Returns:** The
|
|
216
|
+
**Returns:** The bare summary text.
|
|
150
217
|
|
|
151
218
|
#### `firstSentences(s, n)`
|
|
152
219
|
|
|
@@ -176,39 +243,6 @@ function meetsCap(s: string, maxWords?: number): boolean
|
|
|
176
243
|
|
|
177
244
|
**Returns:** `true` when the summary can be published as is.
|
|
178
245
|
|
|
179
|
-
#### `summarizeCached(input, options)`
|
|
180
|
-
|
|
181
|
-
Summarize through the cache: a hit returns without a model call; a miss
|
|
182
|
-
summarizes and stores the result. A summary that did not meet its caps
|
|
183
|
-
(`withinCap: false`) is returned but not stored, so the next run tries again.
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
function summarizeCached(
|
|
187
|
-
input: SummarizeInput,
|
|
188
|
-
options: { cache: SummaryCache; summarizer?: AISummarizationProvider },
|
|
189
|
-
): Promise<SummarizeResult & { cached: boolean; key: string }>
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
- `input` — The summarize request.
|
|
193
|
-
- `options` — The cache, and optionally a provider (defaults to the bonded one).
|
|
194
|
-
- `options.cache` — The cache to read and fill.
|
|
195
|
-
- `options.summarizer` — A provider to use instead of the bonded one.
|
|
196
|
-
|
|
197
|
-
**Returns:** The result, with `cached: true` on a hit and `key` for publishing.
|
|
198
|
-
|
|
199
|
-
#### `summaryKey(input)`
|
|
200
|
-
|
|
201
|
-
The cache key for a request: a SHA-256 of the text (whitespace-normalized)
|
|
202
|
-
and every option that changes the answer.
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
function summaryKey(input: SummarizeInput): string
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
- `input` — The summarize request.
|
|
209
|
-
|
|
210
|
-
**Returns:** A 64-char hex key.
|
|
211
|
-
|
|
212
246
|
### Constants
|
|
213
247
|
|
|
214
248
|
#### `provider`
|
|
@@ -288,7 +322,9 @@ Composes the swappable `ai` bond: an `ai` provider MUST be bonded first
|
|
|
288
322
|
throws at `summarize()` time, not at import). Pass `provider` on the input to
|
|
289
323
|
target a specific named `ai` provider. Because it is just prompt orchestration
|
|
290
324
|
over the bonded LLM, swapping the `ai` provider swaps the model behind every
|
|
291
|
-
summary without touching call sites.
|
|
325
|
+
summary without touching call sites. `createProvider({ aiProvider, model })`
|
|
326
|
+
builds the same provider over an explicitly injected `ai` provider (how
|
|
327
|
+
molecule.dev's hosted summarization service runs it) with a default model.
|
|
292
328
|
|
|
293
329
|
## E2E Tests
|
|
294
330
|
|
package/dist/index.d.ts
CHANGED
|
@@ -89,7 +89,9 @@
|
|
|
89
89
|
* throws at `summarize()` time, not at import). Pass `provider` on the input to
|
|
90
90
|
* target a specific named `ai` provider. Because it is just prompt orchestration
|
|
91
91
|
* over the bonded LLM, swapping the `ai` provider swaps the model behind every
|
|
92
|
-
* summary without touching call sites.
|
|
92
|
+
* summary without touching call sites. `createProvider({ aiProvider, model })`
|
|
93
|
+
* builds the same provider over an explicitly injected `ai` provider (how
|
|
94
|
+
* molecule.dev's hosted summarization service runs it) with a default model.
|
|
93
95
|
*
|
|
94
96
|
* @module
|
|
95
97
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgGG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -89,7 +89,9 @@
|
|
|
89
89
|
* throws at `summarize()` time, not at import). Pass `provider` on the input to
|
|
90
90
|
* target a specific named `ai` provider. Because it is just prompt orchestration
|
|
91
91
|
* over the bonded LLM, swapping the `ai` provider swaps the model behind every
|
|
92
|
-
* summary without touching call sites.
|
|
92
|
+
* summary without touching call sites. `createProvider({ aiProvider, model })`
|
|
93
|
+
* builds the same provider over an explicitly injected `ai` provider (how
|
|
94
|
+
* molecule.dev's hosted summarization service runs it) with a default model.
|
|
93
95
|
*
|
|
94
96
|
* @module
|
|
95
97
|
*/
|
package/dist/provider.d.ts
CHANGED
|
@@ -9,7 +9,33 @@
|
|
|
9
9
|
*
|
|
10
10
|
* @module
|
|
11
11
|
*/
|
|
12
|
+
import { type AIProvider } from '@molecule/api-ai';
|
|
12
13
|
import type { AISummarizationProvider } from '@molecule/api-ai-summarization';
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for {@link createProvider}.
|
|
16
|
+
*/
|
|
17
|
+
export interface LlmSummarizationConfig {
|
|
18
|
+
/**
|
|
19
|
+
* The `ai` provider this provider composes: an already-resolved provider
|
|
20
|
+
* instance, or the name of one to resolve at call time. Defaults to the
|
|
21
|
+
* bonded singleton of `@molecule/api-ai`.
|
|
22
|
+
*/
|
|
23
|
+
aiProvider?: AIProvider | string;
|
|
24
|
+
/** Default model for every summarize call. A call's own `model` wins. */
|
|
25
|
+
model?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creates an LLM summarization provider.
|
|
29
|
+
*
|
|
30
|
+
* Composes the `ai` chat provider (`@molecule/api-ai`) — an AI provider MUST be
|
|
31
|
+
* bonded first (`bond('ai', <provider>)`), or `summarize()` throws, unless
|
|
32
|
+
* `config.aiProvider` supplies one directly (how a hosted handler injects its
|
|
33
|
+
* own provider). The module-level `provider` is this factory with no config.
|
|
34
|
+
*
|
|
35
|
+
* @param config - Optional `ai` provider and default model.
|
|
36
|
+
* @returns An `AISummarizationProvider` composed over the `ai` bond.
|
|
37
|
+
*/
|
|
38
|
+
export declare function createProvider(config?: LlmSummarizationConfig): AISummarizationProvider;
|
|
13
39
|
/**
|
|
14
40
|
* Default AI summarization provider.
|
|
15
41
|
*
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EACL,KAAK,UAAU,EAKhB,MAAM,kBAAkB,CAAA;AACzB,OAAO,KAAK,EACV,uBAAuB,EAGxB,MAAM,gCAAgC,CAAA;AAgEvC;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,UAAU,CAAC,EAAE,UAAU,GAAG,MAAM,CAAA;IAChC,yEAAyE;IACzE,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,MAAM,GAAE,sBAA2B,GAAG,uBAAuB,CA2C3F;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,EAAE,uBAA0C,CAAA;AASjE;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG5C;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAehD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAS3D;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAS9D"}
|
package/dist/provider.js
CHANGED
|
@@ -61,6 +61,53 @@ function buildSystemPrompt(input) {
|
|
|
61
61
|
lines.push('Return only the summary, with no preamble, labels, or commentary.');
|
|
62
62
|
return lines.join('\n');
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Creates an LLM summarization provider.
|
|
66
|
+
*
|
|
67
|
+
* Composes the `ai` chat provider (`@molecule/api-ai`) — an AI provider MUST be
|
|
68
|
+
* bonded first (`bond('ai', <provider>)`), or `summarize()` throws, unless
|
|
69
|
+
* `config.aiProvider` supplies one directly (how a hosted handler injects its
|
|
70
|
+
* own provider). The module-level `provider` is this factory with no config.
|
|
71
|
+
*
|
|
72
|
+
* @param config - Optional `ai` provider and default model.
|
|
73
|
+
* @returns An `AISummarizationProvider` composed over the `ai` bond.
|
|
74
|
+
*/
|
|
75
|
+
export function createProvider(config = {}) {
|
|
76
|
+
return {
|
|
77
|
+
name: 'llm',
|
|
78
|
+
async summarize(input) {
|
|
79
|
+
let ai;
|
|
80
|
+
if (config.aiProvider && typeof config.aiProvider === 'object') {
|
|
81
|
+
ai = config.aiProvider;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const named = input.provider ?? (typeof config.aiProvider === 'string' ? config.aiProvider : undefined);
|
|
85
|
+
ai = named ? getAIProviderByName(named) : requireAIProvider();
|
|
86
|
+
}
|
|
87
|
+
if (!ai) {
|
|
88
|
+
throw new Error(t('ai-summarization.error.aiProviderMissing', {
|
|
89
|
+
provider: input.provider ?? (typeof config.aiProvider === 'string' ? config.aiProvider : ''),
|
|
90
|
+
}, {
|
|
91
|
+
defaultValue: 'AI provider "{{provider}}" is not bonded. Bond it before summarizing.',
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
const resolved = { ...input, model: input.model ?? config.model };
|
|
95
|
+
const sentences = resolved.sentences ?? (resolved.format === 'tldr' ? 1 : undefined);
|
|
96
|
+
if (!resolved.maxWords && !sentences) {
|
|
97
|
+
const params = {
|
|
98
|
+
messages: [{ role: 'user', content: resolved.text }],
|
|
99
|
+
system: buildSystemPrompt(resolved),
|
|
100
|
+
model: resolved.model,
|
|
101
|
+
stream: false,
|
|
102
|
+
signal: resolved.signal,
|
|
103
|
+
};
|
|
104
|
+
const { text, usage } = await complete(ai, params);
|
|
105
|
+
return { summary: text.trim(), usage };
|
|
106
|
+
}
|
|
107
|
+
return summarizeCapped(ai, resolved, sentences ?? 1);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
64
111
|
/**
|
|
65
112
|
* Default AI summarization provider.
|
|
66
113
|
*
|
|
@@ -69,30 +116,7 @@ function buildSystemPrompt(input) {
|
|
|
69
116
|
* it with `bond('ai-summarization', provider)`; swap in a custom
|
|
70
117
|
* `AISummarizationProvider` to replace it without touching call sites.
|
|
71
118
|
*/
|
|
72
|
-
export const provider =
|
|
73
|
-
name: 'llm',
|
|
74
|
-
async summarize(input) {
|
|
75
|
-
const ai = input.provider ? getAIProviderByName(input.provider) : requireAIProvider();
|
|
76
|
-
if (!ai) {
|
|
77
|
-
throw new Error(t('ai-summarization.error.aiProviderMissing', { provider: input.provider ?? '' }, {
|
|
78
|
-
defaultValue: 'AI provider "{{provider}}" is not bonded. Bond it before summarizing.',
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
const sentences = input.sentences ?? (input.format === 'tldr' ? 1 : undefined);
|
|
82
|
-
if (!input.maxWords && !sentences) {
|
|
83
|
-
const params = {
|
|
84
|
-
messages: [{ role: 'user', content: input.text }],
|
|
85
|
-
system: buildSystemPrompt(input),
|
|
86
|
-
model: input.model,
|
|
87
|
-
stream: false,
|
|
88
|
-
signal: input.signal,
|
|
89
|
-
};
|
|
90
|
-
const { text, usage } = await complete(ai, params);
|
|
91
|
-
return { summary: text.trim(), usage };
|
|
92
|
-
}
|
|
93
|
-
return summarizeCapped(ai, input, sentences ?? 1);
|
|
94
|
-
},
|
|
95
|
-
};
|
|
119
|
+
export const provider = createProvider();
|
|
96
120
|
/** Words that never end a finished sentence — a summary ending on one was cut. */
|
|
97
121
|
const FUNCTION_WORDS = new Set('a an the and or but nor of to in on at by for with from into onto as than that which who whose whom is are was were be been its their his her our your my each every this these those so if then'.split(' '));
|
|
98
122
|
/**
|
|
@@ -117,6 +141,15 @@ export function cleanSummary(raw) {
|
|
|
117
141
|
s = s.replace(/\s+/g, ' ').trim();
|
|
118
142
|
s = s.replace(/^(?:\*\*)?(?:tl;?\s?dr|summary)(?:\*\*)?\s*[:\-–—]\s*/i, '');
|
|
119
143
|
s = s.replace(/^(\*\*|__|["“'‘])+/, '').replace(/(\*\*|__|["”'’])+$/, '');
|
|
144
|
+
// A summary is plain text: markdown the model added (`code`, **bold**, _em_,
|
|
145
|
+
// [links](url)) would show literally wherever the summary is displayed.
|
|
146
|
+
s = s
|
|
147
|
+
.replace(/`([^`]*)`/g, '$1')
|
|
148
|
+
.replace(/\*\*([^*]+)\*\*/g, '$1')
|
|
149
|
+
.replace(/__([^_]+)__/g, '$1')
|
|
150
|
+
.replace(/(^|[\s(])[*_]([^*_\s][^*_]*?)[*_](?=[\s).,;:!?]|$)/g, '$1$2')
|
|
151
|
+
.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
|
|
152
|
+
.replace(/`/g, '');
|
|
120
153
|
return s.trim();
|
|
121
154
|
}
|
|
122
155
|
/**
|
|
@@ -171,8 +204,11 @@ async function summarizeCapped(ai, input, sentences) {
|
|
|
171
204
|
const cap = input.maxWords;
|
|
172
205
|
const attempts = Math.max(1, input.attempts ?? 3);
|
|
173
206
|
const shape = sentences === 1 ? 'one sentence' : `at most ${sentences} sentences`;
|
|
207
|
+
// Models treat a stated maximum as the target and land on it exactly, so ask
|
|
208
|
+
// for a length well under the cap; the cap itself is enforced below.
|
|
209
|
+
const target = cap ? Math.max(5, Math.round(cap * 0.7)) : undefined;
|
|
174
210
|
const system = [
|
|
175
|
-
`Summarize the user's text in ${shape}${cap ? ` of
|
|
211
|
+
`Summarize the user's text in ${shape}${cap ? ` of about ${target} words (never more than ${cap})` : ''}, stating what the text does or says.`,
|
|
176
212
|
input.focus ? `Focus on: ${input.focus}` : '',
|
|
177
213
|
'Reply with the summary only: no preamble, no label, no quotes, no markdown.',
|
|
178
214
|
]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-summarization-llm",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Default LLM-composed AI summarization provider for molecule.dev — summarizes over any bonded @molecule/api-ai chat provider",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
"@molecule/api-i18n": "^1.0.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/api-ai": "1.3.
|
|
36
|
+
"@molecule/api-ai": "1.3.4",
|
|
37
37
|
"@molecule/api-ai-summarization": "1.1.0",
|
|
38
38
|
"@molecule/api-bond": "1.0.2",
|
|
39
39
|
"@molecule/api-i18n": "1.0.3",
|
|
40
40
|
"@types/node": "26.1.2",
|
|
41
41
|
"typescript": "6.0.3",
|
|
42
|
-
"vitest": "4.1.
|
|
42
|
+
"vitest": "4.1.12"
|
|
43
43
|
},
|
|
44
44
|
"repository": {
|
|
45
45
|
"type": "git",
|