@molecule/api-ai-summarization-llm 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean any work of authorship, including the
45
+ original version of the Work and any modifications or additions
46
+ to that Work, that is intentionally submitted to the Licensor for
47
+ inclusion in the Work by the copyright owner or by an individual or
48
+ Legal Entity authorized to submit on behalf of the copyright owner.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ patent license to make, have made, use, offer to sell, sell, import,
65
+ and otherwise transfer the Work.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 7. Disclaimer of Warranty. Unless required by applicable law or
95
+ agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
+
98
+ 8. Limitation of Liability. In no event and under no legal theory shall
99
+ any Contributor be liable to You for damages.
100
+
101
+ 9. Accepting Warranty or Additional Liability.
102
+
103
+ Copyright 2026 Molecule Dev, Inc.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=browser-guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Browser guard — `@molecule/api-ai-summarization-llm` is SERVER-ONLY.
3
+ *
4
+ * Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
5
+ * Evaluating a server package in a browser bundle is always an import-graph mistake
6
+ * (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
7
+ * ("Buffer is not defined") far from the culprit. Throwing here names the package and
8
+ * the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
9
+ * unaffected: the throw requires browser globals AND the absence of a node runtime.
10
+ */
11
+ const g = globalThis;
12
+ if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
13
+ throw new Error('@molecule/api-ai-summarization-llm is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
14
+ 'from server code (a server route/function or your API), or dynamic-import it inside ' +
15
+ 'the server handler — never from components or shared client modules, and never ' +
16
+ 'polyfill Buffer/process to silence this.');
17
+ }
18
+ export {};
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Default (`llm`) AI summarization provider for molecule.dev.
3
+ *
4
+ * Turns a block of text into a concise summary over whatever LLM the app has
5
+ * bonded. It has no vendor of its own — it composes the swappable `ai` chat
6
+ * bond (`@molecule/api-ai`) with a summarizer system prompt built from the
7
+ * requested format / length / focus, so swapping the LLM swaps the summarizer.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { provider as anthropic } from '@molecule/api-ai-anthropic'
12
+ * import { requireProvider } from '@molecule/api-ai-summarization'
13
+ * import { provider as summarization } from '@molecule/api-ai-summarization-llm'
14
+ * import { bond } from '@molecule/api-bond'
15
+ *
16
+ * // Wire the AI chat provider this composes, then bond the summarizer.
17
+ * bond('ai', anthropic)
18
+ * bond('ai-summarization', summarization)
19
+ *
20
+ * // Use anywhere after startup.
21
+ * const { summary, usage } = await requireProvider().summarize({
22
+ * text: longArticle,
23
+ * format: 'bullets',
24
+ * maxLength: 60,
25
+ * focus: 'the financial impact',
26
+ * })
27
+ * ```
28
+ *
29
+ * @remarks
30
+ * Composes the swappable `ai` bond: an `ai` provider MUST be bonded first
31
+ * (`bond('ai', <provider>)`) or `summarize()` throws (a missing AI provider
32
+ * throws at `summarize()` time, not at import). Pass `provider` on the input to
33
+ * target a specific named `ai` provider. Because it is just prompt orchestration
34
+ * over the bonded LLM, swapping the `ai` provider swaps the model behind every
35
+ * summary without touching call sites.
36
+ *
37
+ * @module
38
+ */
39
+ export * from './browser-guard.js';
40
+ export * from './provider.js';
41
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Default (`llm`) AI summarization provider for molecule.dev.
3
+ *
4
+ * Turns a block of text into a concise summary over whatever LLM the app has
5
+ * bonded. It has no vendor of its own — it composes the swappable `ai` chat
6
+ * bond (`@molecule/api-ai`) with a summarizer system prompt built from the
7
+ * requested format / length / focus, so swapping the LLM swaps the summarizer.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { provider as anthropic } from '@molecule/api-ai-anthropic'
12
+ * import { requireProvider } from '@molecule/api-ai-summarization'
13
+ * import { provider as summarization } from '@molecule/api-ai-summarization-llm'
14
+ * import { bond } from '@molecule/api-bond'
15
+ *
16
+ * // Wire the AI chat provider this composes, then bond the summarizer.
17
+ * bond('ai', anthropic)
18
+ * bond('ai-summarization', summarization)
19
+ *
20
+ * // Use anywhere after startup.
21
+ * const { summary, usage } = await requireProvider().summarize({
22
+ * text: longArticle,
23
+ * format: 'bullets',
24
+ * maxLength: 60,
25
+ * focus: 'the financial impact',
26
+ * })
27
+ * ```
28
+ *
29
+ * @remarks
30
+ * Composes the swappable `ai` bond: an `ai` provider MUST be bonded first
31
+ * (`bond('ai', <provider>)`) or `summarize()` throws (a missing AI provider
32
+ * throws at `summarize()` time, not at import). Pass `provider` on the input to
33
+ * target a specific named `ai` provider. Because it is just prompt orchestration
34
+ * over the bonded LLM, swapping the `ai` provider swaps the model behind every
35
+ * summary without touching call sites.
36
+ *
37
+ * @module
38
+ */
39
+ export * from './browser-guard.js';
40
+ export * from './provider.js';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Default (`llm`) AI summarization provider.
3
+ *
4
+ * Composes the swappable `ai` chat bond (`@molecule/api-ai`) with a summarizer
5
+ * system prompt built from the requested format / length / focus. It has no
6
+ * vendor of its own — whatever LLM the app bonds under `ai` powers it, so
7
+ * summarization is prompt orchestration over the bonded model, not a separate
8
+ * vendor integration.
9
+ *
10
+ * @module
11
+ */
12
+ import type { AISummarizationProvider } from '@molecule/api-ai-summarization';
13
+ /**
14
+ * Default AI summarization provider.
15
+ *
16
+ * Composes the bonded `ai` chat provider (`@molecule/api-ai`) — an AI provider
17
+ * MUST be bonded first (`bond('ai', <provider>)`), or `summarize()` throws. Bond
18
+ * it with `bond('ai-summarization', provider)`; swap in a custom
19
+ * `AISummarizationProvider` to replace it without touching call sites.
20
+ */
21
+ export declare const provider: AISummarizationProvider;
22
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AASH,OAAO,KAAK,EACV,uBAAuB,EAGxB,MAAM,gCAAgC,CAAA;AAgEvC;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,EAAE,uBA2BtB,CAAA"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Default (`llm`) AI summarization provider.
3
+ *
4
+ * Composes the swappable `ai` chat bond (`@molecule/api-ai`) with a summarizer
5
+ * system prompt built from the requested format / length / focus. It has no
6
+ * vendor of its own — whatever LLM the app bonds under `ai` powers it, so
7
+ * summarization is prompt orchestration over the bonded model, not a separate
8
+ * vendor integration.
9
+ *
10
+ * @module
11
+ */
12
+ import { getProviderByName as getAIProviderByName, requireProvider as requireAIProvider, } from '@molecule/api-ai';
13
+ import { t } from '@molecule/api-i18n';
14
+ /**
15
+ * Collects a non-streaming chat completion into a single string + usage.
16
+ *
17
+ * Shared shape for AI-composed providers (summarization, translation, …):
18
+ * iterate the provider's event stream, concatenate `text` events, capture the
19
+ * final `done` event's usage, and surface an `error` event as a thrown error.
20
+ *
21
+ * @param ai - The resolved AI provider to drive.
22
+ * @param params - Chat parameters (the caller forces non-streaming).
23
+ * @returns The concatenated text and the final token usage (when reported).
24
+ */
25
+ async function complete(ai, params) {
26
+ let text = '';
27
+ let usage;
28
+ for await (const event of ai.chat(params)) {
29
+ if (event.type === 'text') {
30
+ text += event.content;
31
+ }
32
+ else if (event.type === 'done') {
33
+ usage = event.usage;
34
+ }
35
+ else if (event.type === 'error') {
36
+ throw new Error(t('ai-summarization.error.aiFailed', { message: event.message }, { defaultValue: 'AI summarization failed: {{message}}' }));
37
+ }
38
+ }
39
+ return { text, usage };
40
+ }
41
+ /**
42
+ * Builds the summarizer system prompt from the requested shape/length/focus.
43
+ *
44
+ * @param input - The summarize controls.
45
+ * @returns A system prompt string for the AI chat call.
46
+ */
47
+ function buildSystemPrompt(input) {
48
+ const format = input.format ?? 'paragraph';
49
+ const shape = format === 'bullets'
50
+ ? 'a concise bulleted list'
51
+ : format === 'tldr'
52
+ ? 'a single-sentence TL;DR'
53
+ : 'a concise paragraph';
54
+ const lines = [`You are an expert summarizer. Produce ${shape} summary of the user's text.`];
55
+ if (typeof input.maxLength === 'number' && input.maxLength > 0) {
56
+ lines.push(`Keep it to roughly ${input.maxLength} words max.`);
57
+ }
58
+ if (input.focus) {
59
+ lines.push(`Focus on: ${input.focus}`);
60
+ }
61
+ lines.push('Return only the summary, with no preamble, labels, or commentary.');
62
+ return lines.join('\n');
63
+ }
64
+ /**
65
+ * Default AI summarization provider.
66
+ *
67
+ * Composes the bonded `ai` chat provider (`@molecule/api-ai`) — an AI provider
68
+ * MUST be bonded first (`bond('ai', <provider>)`), or `summarize()` throws. Bond
69
+ * it with `bond('ai-summarization', provider)`; swap in a custom
70
+ * `AISummarizationProvider` to replace it without touching call sites.
71
+ */
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 params = {
82
+ messages: [{ role: 'user', content: input.text }],
83
+ system: buildSystemPrompt(input),
84
+ model: input.model,
85
+ stream: false,
86
+ signal: input.signal,
87
+ };
88
+ const { text, usage } = await complete(ai, params);
89
+ return { summary: text.trim(), usage };
90
+ },
91
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@molecule/api-ai-summarization-llm",
3
+ "version": "1.0.0",
4
+ "description": "Default LLM-composed AI summarization provider for molecule.dev — summarizes over any bonded @molecule/api-ai chat provider",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run",
11
+ "test:watch": "vitest"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "molecule",
24
+ "ai",
25
+ "summarization",
26
+ "llm"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "peerDependencies": {
30
+ "@molecule/api-ai": "^1.0.0",
31
+ "@molecule/api-ai-summarization": "^1.0.0",
32
+ "@molecule/api-i18n": "^1.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@molecule/api-ai": "1.0.0",
36
+ "@molecule/api-ai-summarization": "1.0.0",
37
+ "@molecule/api-bond": "1.0.0",
38
+ "@molecule/api-i18n": "1.0.0",
39
+ "@types/node": "26.1.2",
40
+ "typescript": "6.0.3",
41
+ "vitest": "4.1.10"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/molecule-dev/molecule.git",
46
+ "directory": "packages/api/bonds/ai-summarization/llm"
47
+ },
48
+ "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/bonds/ai-summarization/llm",
49
+ "bugs": "https://github.com/molecule-dev/molecule/issues",
50
+ "publishConfig": {
51
+ "access": "public"
52
+ }
53
+ }