@newses/plugin-digest-summary 0.1.1 → 0.3.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/README.md +127 -29
- package/dist/digest.d.ts +125 -26
- package/dist/digest.js +344 -104
- package/dist/index.d.ts +33 -4
- package/dist/index.js +63 -26
- package/dist/prompts.d.ts +115 -53
- package/dist/prompts.js +231 -67
- package/dist/{claude.d.ts → providers/anthropic.d.ts} +9 -1
- package/dist/providers/anthropic.js +108 -0
- package/dist/providers/http.d.ts +24 -0
- package/dist/providers/http.js +77 -0
- package/dist/providers/index.d.ts +5 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/ollama.d.ts +9 -0
- package/dist/providers/ollama.js +137 -0
- package/dist/providers/openai.d.ts +9 -0
- package/dist/providers/openai.js +116 -0
- package/dist/providers/types.d.ts +65 -0
- package/dist/providers/types.js +1 -0
- package/dist/settings.d.ts +86 -4
- package/dist/settings.js +81 -8
- package/dist/tokens.d.ts +2 -0
- package/dist/tokens.js +2 -0
- package/package.json +8 -5
- package/dist/claude.js +0 -49
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { errorDetail, isTimeout, reachText, requestJson, ServerError, trimmed } from './http.js';
|
|
3
|
+
/** Low, so the overview keeps to the articles; not zero, which repeats itself on small models. */
|
|
4
|
+
const TEMPERATURE = 0.2;
|
|
5
|
+
const chatAnswer = z.object({
|
|
6
|
+
message: z.object({ content: z.string() }),
|
|
7
|
+
done_reason: z.string().optional(),
|
|
8
|
+
prompt_eval_count: z.number().optional(),
|
|
9
|
+
prompt_eval_duration: z.number().optional(),
|
|
10
|
+
eval_count: z.number().optional(),
|
|
11
|
+
eval_duration: z.number().optional(),
|
|
12
|
+
});
|
|
13
|
+
const showAnswer = z.object({
|
|
14
|
+
model_info: z.record(z.string(), z.unknown()).optional(),
|
|
15
|
+
capabilities: z.array(z.string()).optional(),
|
|
16
|
+
});
|
|
17
|
+
const formatted = (n) => new Intl.NumberFormat('en').format(n);
|
|
18
|
+
/** The longest context a model was trained for, from `/api/show`'s `<architecture>.context_length`. */
|
|
19
|
+
function trainedContext(info) {
|
|
20
|
+
const entry = Object.entries(info ?? {}).find(([key]) => key.endsWith('.context_length'));
|
|
21
|
+
return typeof entry?.[1] === 'number' ? entry[1] : null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A model on an Ollama server, through its own API (`/api/chat`): the context set with each
|
|
25
|
+
* request (`num_ctx`), so a prompt is never cut to Ollama's default; the answer held to the
|
|
26
|
+
* format's JSON schema; thinking on or off as the admin chose. The key, when set, is sent as a
|
|
27
|
+
* bearer token, for an Ollama behind a proxy that asks for one (Ollama itself has no keys).
|
|
28
|
+
*/
|
|
29
|
+
export function ollamaProvider(settings, fetch) {
|
|
30
|
+
const base = trimmed(settings.baseUrl ?? '');
|
|
31
|
+
const host = base ? new URL(base).host : 'the Ollama server';
|
|
32
|
+
const model = settings.localModel;
|
|
33
|
+
const headers = settings.localApiKey
|
|
34
|
+
? { authorization: `Bearer ${settings.localApiKey}` }
|
|
35
|
+
: {};
|
|
36
|
+
return {
|
|
37
|
+
name: 'ollama',
|
|
38
|
+
label: `${model} on Ollama`,
|
|
39
|
+
capacity: {
|
|
40
|
+
kind: 'context',
|
|
41
|
+
contextTokens: settings.contextTokens,
|
|
42
|
+
thinking: settings.thinking,
|
|
43
|
+
},
|
|
44
|
+
parallel: settings.parallelRequests,
|
|
45
|
+
async ask(request) {
|
|
46
|
+
let answer;
|
|
47
|
+
try {
|
|
48
|
+
const { status, body } = await requestJson(fetch, `${base}/api/chat`, {
|
|
49
|
+
body: {
|
|
50
|
+
model,
|
|
51
|
+
// One system message, the fixed prompt first, so Ollama reuses what it read of it.
|
|
52
|
+
messages: [
|
|
53
|
+
{ role: 'system', content: `${request.system[0]}\n\n${request.system[1]}` },
|
|
54
|
+
{ role: 'user', content: request.prompt },
|
|
55
|
+
],
|
|
56
|
+
stream: false,
|
|
57
|
+
format: request.format.schema,
|
|
58
|
+
think: settings.thinking && request.thinking,
|
|
59
|
+
// An error, rather than a prompt cut short, when it does not fit.
|
|
60
|
+
truncate: false,
|
|
61
|
+
shift: false,
|
|
62
|
+
options: {
|
|
63
|
+
num_ctx: settings.contextTokens,
|
|
64
|
+
num_predict: request.maxTokens,
|
|
65
|
+
temperature: TEMPERATURE,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
headers,
|
|
69
|
+
signal: request.signal,
|
|
70
|
+
});
|
|
71
|
+
if (status !== 200)
|
|
72
|
+
throw new ServerError(status, errorDetail(body));
|
|
73
|
+
const parsed = chatAnswer.safeParse(body);
|
|
74
|
+
if (!parsed.success)
|
|
75
|
+
throw new Error('The Ollama server answered with something else');
|
|
76
|
+
answer = parsed.data;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
return { ok: false, outcome: isTimeout(error) ? 'timed out' : 'failed', tokens: 0, error };
|
|
80
|
+
}
|
|
81
|
+
const promptTokens = answer.prompt_eval_count ?? 0;
|
|
82
|
+
const outputTokens = answer.eval_count ?? 0;
|
|
83
|
+
const tokens = promptTokens + outputTokens;
|
|
84
|
+
if (answer.done_reason !== undefined && answer.done_reason !== 'stop')
|
|
85
|
+
return { ok: false, outcome: 'invalid', tokens };
|
|
86
|
+
return {
|
|
87
|
+
ok: true,
|
|
88
|
+
text: answer.message.content,
|
|
89
|
+
tokens,
|
|
90
|
+
...(answer.prompt_eval_duration !== undefined &&
|
|
91
|
+
answer.eval_duration !== undefined && {
|
|
92
|
+
timing: {
|
|
93
|
+
promptTokens,
|
|
94
|
+
promptMs: answer.prompt_eval_duration / 1e6,
|
|
95
|
+
outputTokens,
|
|
96
|
+
outputMs: answer.eval_duration / 1e6,
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
async check(signal) {
|
|
102
|
+
if (!base)
|
|
103
|
+
return { ok: false, message: 'Set the URL of the Ollama server.' };
|
|
104
|
+
let shown;
|
|
105
|
+
try {
|
|
106
|
+
const { status, body } = await requestJson(fetch, `${base}/api/show`, {
|
|
107
|
+
body: { model },
|
|
108
|
+
headers,
|
|
109
|
+
signal,
|
|
110
|
+
});
|
|
111
|
+
if (status === 404)
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
message: `${model} is not on the Ollama server at ${host}: pull it there with \`ollama pull ${model}\`.`,
|
|
115
|
+
};
|
|
116
|
+
if (status !== 200)
|
|
117
|
+
throw new ServerError(status, errorDetail(body));
|
|
118
|
+
shown = showAnswer.parse(body);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
return { ok: false, message: reachText(error, host) };
|
|
122
|
+
}
|
|
123
|
+
const trained = trainedContext(shown.model_info);
|
|
124
|
+
if (trained !== null && settings.contextTokens > trained)
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
message: `${model} takes at most ${formatted(trained)} tokens of context, fewer than the ${formatted(settings.contextTokens)} set: lower Context.`,
|
|
128
|
+
};
|
|
129
|
+
if (settings.thinking && shown.capabilities && !shown.capabilities.includes('thinking'))
|
|
130
|
+
return { ok: false, message: `${model} does not think: turn Thinking off.` };
|
|
131
|
+
return {
|
|
132
|
+
ok: true,
|
|
133
|
+
message: `Ollama at ${host} has ${model}, with ${formatted(settings.contextTokens)} tokens of context.`,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { InstanceSettings } from '../settings.js';
|
|
2
|
+
import type { Provider } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A model on a server speaking the OpenAI chat completions API (`/chat/completions` under the
|
|
5
|
+
* URL set, which ends in `/v1`): llama.cpp's server, vLLM, LM Studio, LocalAI, Ollama's `/v1`.
|
|
6
|
+
* The answer is held to the format's JSON schema (`response_format`); the key, when set, is
|
|
7
|
+
* sent as a bearer token. The server's own context must be at least `contextTokens`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function openaiProvider(settings: Pick<InstanceSettings, 'baseUrl' | 'localApiKey' | 'localModel' | 'contextTokens' | 'parallelRequests'>, fetch: typeof globalThis.fetch): Provider;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { estimateTokens } from '../tokens.js';
|
|
3
|
+
import { errorDetail, isTimeout, reachText, requestJson, ServerError, trimmed } from './http.js';
|
|
4
|
+
/** Low, so the overview keeps to the articles; not zero, which repeats itself on small models. */
|
|
5
|
+
const TEMPERATURE = 0.2;
|
|
6
|
+
const completion = z.object({
|
|
7
|
+
choices: z
|
|
8
|
+
.array(z.object({
|
|
9
|
+
message: z.object({
|
|
10
|
+
content: z.string().nullable().optional(),
|
|
11
|
+
refusal: z.string().nullable().optional(),
|
|
12
|
+
}),
|
|
13
|
+
finish_reason: z.string().nullable().optional(),
|
|
14
|
+
}))
|
|
15
|
+
.min(1),
|
|
16
|
+
usage: z
|
|
17
|
+
.object({ prompt_tokens: z.number(), completion_tokens: z.number() })
|
|
18
|
+
.nullable()
|
|
19
|
+
.optional(),
|
|
20
|
+
});
|
|
21
|
+
const models = z.object({ data: z.array(z.object({ id: z.string() })) });
|
|
22
|
+
/**
|
|
23
|
+
* A model on a server speaking the OpenAI chat completions API (`/chat/completions` under the
|
|
24
|
+
* URL set, which ends in `/v1`): llama.cpp's server, vLLM, LM Studio, LocalAI, Ollama's `/v1`.
|
|
25
|
+
* The answer is held to the format's JSON schema (`response_format`); the key, when set, is
|
|
26
|
+
* sent as a bearer token. The server's own context must be at least `contextTokens`.
|
|
27
|
+
*/
|
|
28
|
+
export function openaiProvider(settings, fetch) {
|
|
29
|
+
const base = trimmed(settings.baseUrl ?? '');
|
|
30
|
+
const host = base ? new URL(base).host : 'the server';
|
|
31
|
+
const model = settings.localModel;
|
|
32
|
+
const headers = settings.localApiKey
|
|
33
|
+
? { authorization: `Bearer ${settings.localApiKey}` }
|
|
34
|
+
: {};
|
|
35
|
+
return {
|
|
36
|
+
name: 'openai',
|
|
37
|
+
label: `${model} at ${host}`,
|
|
38
|
+
capacity: { kind: 'context', contextTokens: settings.contextTokens, thinking: false },
|
|
39
|
+
parallel: settings.parallelRequests,
|
|
40
|
+
async ask(request) {
|
|
41
|
+
let answer;
|
|
42
|
+
try {
|
|
43
|
+
const { status, body } = await requestJson(fetch, `${base}/chat/completions`, {
|
|
44
|
+
body: {
|
|
45
|
+
model,
|
|
46
|
+
messages: [
|
|
47
|
+
{ role: 'system', content: `${request.system[0]}\n\n${request.system[1]}` },
|
|
48
|
+
{ role: 'user', content: request.prompt },
|
|
49
|
+
],
|
|
50
|
+
response_format: {
|
|
51
|
+
type: 'json_schema',
|
|
52
|
+
json_schema: { name: 'digest', strict: true, schema: request.format.schema },
|
|
53
|
+
},
|
|
54
|
+
max_tokens: request.maxTokens,
|
|
55
|
+
temperature: TEMPERATURE,
|
|
56
|
+
stream: false,
|
|
57
|
+
},
|
|
58
|
+
headers,
|
|
59
|
+
signal: request.signal,
|
|
60
|
+
});
|
|
61
|
+
if (status !== 200)
|
|
62
|
+
throw new ServerError(status, errorDetail(body));
|
|
63
|
+
const parsed = completion.safeParse(body);
|
|
64
|
+
if (!parsed.success)
|
|
65
|
+
throw new Error('The server answered with something else');
|
|
66
|
+
answer = parsed.data;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
return { ok: false, outcome: isTimeout(error) ? 'timed out' : 'failed', tokens: 0, error };
|
|
70
|
+
}
|
|
71
|
+
const [choice] = answer.choices;
|
|
72
|
+
const text = choice?.message.content ?? '';
|
|
73
|
+
// Servers that leave usage out are counted by estimate, so the budgets still hold.
|
|
74
|
+
const tokens = answer.usage
|
|
75
|
+
? answer.usage.prompt_tokens + answer.usage.completion_tokens
|
|
76
|
+
: estimateTokens(request.system.join('') + request.prompt + text);
|
|
77
|
+
if (choice?.message.refusal || choice?.finish_reason === 'content_filter')
|
|
78
|
+
return { ok: false, outcome: 'refused', tokens };
|
|
79
|
+
if (choice?.finish_reason && choice.finish_reason !== 'stop')
|
|
80
|
+
return { ok: false, outcome: 'invalid', tokens };
|
|
81
|
+
return { ok: true, text, tokens };
|
|
82
|
+
},
|
|
83
|
+
async check(signal) {
|
|
84
|
+
if (!base)
|
|
85
|
+
return { ok: false, message: 'Set the URL of the server, ending in /v1.' };
|
|
86
|
+
let listed;
|
|
87
|
+
try {
|
|
88
|
+
const { status, body } = await requestJson(fetch, `${base}/models`, { headers, signal });
|
|
89
|
+
if (status === 401 || status === 403)
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
message: `${host} refused the API key (HTTP ${String(status)}): check it in the server’s settings.`,
|
|
93
|
+
};
|
|
94
|
+
if (status !== 200)
|
|
95
|
+
throw new ServerError(status, errorDetail(body));
|
|
96
|
+
const parsed = models.safeParse(body);
|
|
97
|
+
if (!parsed.success)
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
message: `${host} did not list its models: check that the URL ends in /v1.`,
|
|
101
|
+
};
|
|
102
|
+
listed = parsed.data;
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
return { ok: false, message: reachText(error, host) };
|
|
106
|
+
}
|
|
107
|
+
const ids = listed.data.map((entry) => entry.id);
|
|
108
|
+
if (!ids.includes(model))
|
|
109
|
+
return {
|
|
110
|
+
ok: false,
|
|
111
|
+
message: `${host} has no model ${model}${ids.length ? `; it has ${ids.slice(0, 5).join(', ')}` : ''}.`,
|
|
112
|
+
};
|
|
113
|
+
return { ok: true, message: `${host} has ${model}.` };
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { OutputFormat } from '../prompts.js';
|
|
2
|
+
import type { InstanceSettings } from '../settings.js';
|
|
3
|
+
/** Why a request has no answer to read. */
|
|
4
|
+
export type AskOutcome = 'refused' | 'invalid' | 'failed' | 'timed out';
|
|
5
|
+
/** One request for an overview, or for a part's themes. */
|
|
6
|
+
export interface AskRequest {
|
|
7
|
+
/** The fixed system prompt, then the reader's preferences. */
|
|
8
|
+
system: readonly [string, string];
|
|
9
|
+
prompt: string;
|
|
10
|
+
format: OutputFormat;
|
|
11
|
+
/** Output tokens the answer may take, thinking included. */
|
|
12
|
+
maxTokens: number;
|
|
13
|
+
/** The model may think first, when the admin lets it: the overview's request; parts never do. */
|
|
14
|
+
thinking: boolean;
|
|
15
|
+
/** Ends the request: the hook's signal and the request's own time together. */
|
|
16
|
+
signal: AbortSignal;
|
|
17
|
+
/** Milliseconds the request may take. */
|
|
18
|
+
timeoutMs: number;
|
|
19
|
+
}
|
|
20
|
+
/** How long a self-hosted model took to read a prompt and write an answer. */
|
|
21
|
+
export interface Timing {
|
|
22
|
+
promptTokens: number;
|
|
23
|
+
promptMs: number;
|
|
24
|
+
outputTokens: number;
|
|
25
|
+
outputMs: number;
|
|
26
|
+
}
|
|
27
|
+
/** How one request ended: its text, or why it has none; and the tokens it used either way. */
|
|
28
|
+
export type Asked = {
|
|
29
|
+
ok: true;
|
|
30
|
+
text: string;
|
|
31
|
+
tokens: number;
|
|
32
|
+
timing?: Timing;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
outcome: AskOutcome;
|
|
36
|
+
tokens: number;
|
|
37
|
+
error?: unknown;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* How much one request may read: Claude's parts grow past `partTokens` for a digest larger than
|
|
41
|
+
* the parts hold; a self-hosted model's context is fixed, so it reads the newest articles that fit.
|
|
42
|
+
*/
|
|
43
|
+
export type Capacity = {
|
|
44
|
+
kind: 'grow';
|
|
45
|
+
partTokens: number;
|
|
46
|
+
} | {
|
|
47
|
+
kind: 'context';
|
|
48
|
+
contextTokens: number;
|
|
49
|
+
thinking: boolean;
|
|
50
|
+
};
|
|
51
|
+
/** A model the plugin writes with, whoever serves it. */
|
|
52
|
+
export interface Provider {
|
|
53
|
+
readonly name: InstanceSettings['provider'];
|
|
54
|
+
/** What the messages call it: `claude-opus-5-5 at low effort`, `qwen3:8b on Ollama`. */
|
|
55
|
+
readonly label: string;
|
|
56
|
+
readonly capacity: Capacity;
|
|
57
|
+
/** Parts read at once, at most. */
|
|
58
|
+
readonly parallel: number;
|
|
59
|
+
ask(request: AskRequest): Promise<Asked>;
|
|
60
|
+
/** Whether the model can be reached, for the test hook, as people read it. */
|
|
61
|
+
check(signal: AbortSignal): Promise<{
|
|
62
|
+
ok: boolean;
|
|
63
|
+
message: string;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/settings.d.ts
CHANGED
|
@@ -1,13 +1,64 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Who writes the overview: Claude through the Anthropic API, a model on an Ollama server, or one
|
|
4
|
+
* on a server speaking the OpenAI chat completions API (llama.cpp, vLLM, LM Studio, LocalAI…).
|
|
5
|
+
*/
|
|
6
|
+
export declare const PROVIDERS: readonly ["anthropic", "ollama", "openai"];
|
|
7
|
+
/** The Claude models an admin can choose: the current Opus, Sonnet, and Haiku. */
|
|
3
8
|
export declare const MODELS: readonly ["claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5"];
|
|
4
9
|
/** How much the model thinks before it writes. */
|
|
5
10
|
export declare const EFFORTS: readonly ["low", "medium", "high"];
|
|
6
11
|
/** Which of a user's feeds the overview reads. */
|
|
7
12
|
export declare const FEED_CHOICES: readonly ["all", "only", "except"];
|
|
8
|
-
/**
|
|
13
|
+
/** How long the overview is. */
|
|
14
|
+
export declare const LENGTHS: readonly ["brief", "standard", "detailed"];
|
|
15
|
+
/** How each section's points are shown: as paragraphs, or as a bulleted list. */
|
|
16
|
+
export declare const STYLES: readonly ["paragraphs", "bullets"];
|
|
17
|
+
/** Characters of a user's own instructions, at most. */
|
|
18
|
+
export declare const INSTRUCTIONS_LENGTH = 1000;
|
|
19
|
+
/** The sections a user starts with: a name, and what goes in it. */
|
|
20
|
+
export declare const DEFAULT_SECTIONS: readonly [{
|
|
21
|
+
readonly name: "World";
|
|
22
|
+
readonly about: "International news, conflicts, and diplomacy";
|
|
23
|
+
}, {
|
|
24
|
+
readonly name: "Politics";
|
|
25
|
+
readonly about: "Governments, elections, laws, and policy";
|
|
26
|
+
}, {
|
|
27
|
+
readonly name: "Business";
|
|
28
|
+
readonly about: "Companies, markets, the economy, and work";
|
|
29
|
+
}, {
|
|
30
|
+
readonly name: "Technology";
|
|
31
|
+
readonly about: "Computing, the internet, AI, gadgets, and the tech industry";
|
|
32
|
+
}, {
|
|
33
|
+
readonly name: "Science";
|
|
34
|
+
readonly about: "Research, space, the environment, and climate";
|
|
35
|
+
}, {
|
|
36
|
+
readonly name: "Health";
|
|
37
|
+
readonly about: "Medicine, public health, and wellbeing";
|
|
38
|
+
}, {
|
|
39
|
+
readonly name: "Sports";
|
|
40
|
+
readonly about: "Games, results, athletes, and leagues";
|
|
41
|
+
}, {
|
|
42
|
+
readonly name: "Culture";
|
|
43
|
+
readonly about: "Arts, entertainment, books, games, and media";
|
|
44
|
+
}, {
|
|
45
|
+
readonly name: "Other";
|
|
46
|
+
readonly about: "Notable stories that fit no other section";
|
|
47
|
+
}];
|
|
48
|
+
/** The context a self-hosted model is given, in tokens, at least and by default. */
|
|
49
|
+
export declare const MIN_CONTEXT_TOKENS = 8192;
|
|
50
|
+
export declare const DEFAULT_CONTEXT_TOKENS = 16384;
|
|
51
|
+
/**
|
|
52
|
+
* What an admin sets for the server: who writes the overview and how it is reached, the model,
|
|
53
|
+
* the budgets, and what users may add. Anthropic needs the API key; a self-hosted server, its URL.
|
|
54
|
+
*/
|
|
9
55
|
export declare const instanceSettings: z.ZodObject<{
|
|
10
|
-
|
|
56
|
+
provider: z.ZodDefault<z.ZodEnum<{
|
|
57
|
+
anthropic: "anthropic";
|
|
58
|
+
ollama: "ollama";
|
|
59
|
+
openai: "openai";
|
|
60
|
+
}>>;
|
|
61
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
11
62
|
model: z.ZodDefault<z.ZodEnum<{
|
|
12
63
|
"claude-opus-5-5": "claude-opus-5-5";
|
|
13
64
|
"claude-sonnet-5-5": "claude-sonnet-5-5";
|
|
@@ -18,10 +69,25 @@ export declare const instanceSettings: z.ZodObject<{
|
|
|
18
69
|
medium: "medium";
|
|
19
70
|
high: "high";
|
|
20
71
|
}>>;
|
|
72
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
73
|
+
localApiKey: z.ZodOptional<z.ZodString>;
|
|
74
|
+
localModel: z.ZodDefault<z.ZodString>;
|
|
75
|
+
contextTokens: z.ZodDefault<z.ZodNumber>;
|
|
76
|
+
parallelRequests: z.ZodDefault<z.ZodNumber>;
|
|
77
|
+
thinking: z.ZodDefault<z.ZodBoolean>;
|
|
21
78
|
monthlyTokens: z.ZodDefault<z.ZodNumber>;
|
|
22
79
|
userMonthlyTokens: z.ZodDefault<z.ZodNumber>;
|
|
80
|
+
allowInstructions: z.ZodDefault<z.ZodBoolean>;
|
|
81
|
+
}, z.core.$strip>;
|
|
82
|
+
/** A section of the overview, by name, with what goes in it. */
|
|
83
|
+
export declare const section: z.ZodObject<{
|
|
84
|
+
name: z.ZodString;
|
|
85
|
+
about: z.ZodDefault<z.ZodString>;
|
|
23
86
|
}, z.core.$strip>;
|
|
24
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* What each user chooses: all their feeds, only some, or all but some; the sections, the length
|
|
89
|
+
* and style, whether a lead opens it, and instructions of their own.
|
|
90
|
+
*/
|
|
25
91
|
export declare const userSettings: z.ZodObject<{
|
|
26
92
|
feedChoice: z.ZodDefault<z.ZodEnum<{
|
|
27
93
|
all: "all";
|
|
@@ -29,6 +95,22 @@ export declare const userSettings: z.ZodObject<{
|
|
|
29
95
|
except: "except";
|
|
30
96
|
}>>;
|
|
31
97
|
feeds: z.ZodDefault<z.ZodArray<z.ZodInt>>;
|
|
98
|
+
sections: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
99
|
+
name: z.ZodString;
|
|
100
|
+
about: z.ZodDefault<z.ZodString>;
|
|
101
|
+
}, z.core.$strip>>>;
|
|
102
|
+
length: z.ZodDefault<z.ZodEnum<{
|
|
103
|
+
brief: "brief";
|
|
104
|
+
standard: "standard";
|
|
105
|
+
detailed: "detailed";
|
|
106
|
+
}>>;
|
|
107
|
+
style: z.ZodDefault<z.ZodEnum<{
|
|
108
|
+
paragraphs: "paragraphs";
|
|
109
|
+
bullets: "bullets";
|
|
110
|
+
}>>;
|
|
111
|
+
lead: z.ZodDefault<z.ZodBoolean>;
|
|
112
|
+
instructions: z.ZodDefault<z.ZodString>;
|
|
32
113
|
}, z.core.$strip>;
|
|
33
114
|
export type InstanceSettings = z.output<typeof instanceSettings>;
|
|
34
115
|
export type UserSettings = z.output<typeof userSettings>;
|
|
116
|
+
export type Section = z.output<typeof section>;
|
package/dist/settings.js
CHANGED
|
@@ -1,22 +1,95 @@
|
|
|
1
|
-
import { feeds, secret } from '@newses/plugin-api';
|
|
1
|
+
import { DIGEST_LIMITS, feeds, multiline, secret, shownWhen, url } from '@newses/plugin-api';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Who writes the overview: Claude through the Anthropic API, a model on an Ollama server, or one
|
|
5
|
+
* on a server speaking the OpenAI chat completions API (llama.cpp, vLLM, LM Studio, LocalAI…).
|
|
6
|
+
*/
|
|
7
|
+
export const PROVIDERS = ['anthropic', 'ollama', 'openai'];
|
|
8
|
+
/** The Claude models an admin can choose: the current Opus, Sonnet, and Haiku. */
|
|
4
9
|
export const MODELS = ['claude-opus-5-5', 'claude-sonnet-5-5', 'claude-haiku-5-5'];
|
|
5
10
|
/** How much the model thinks before it writes. */
|
|
6
11
|
export const EFFORTS = ['low', 'medium', 'high'];
|
|
7
12
|
/** Which of a user's feeds the overview reads. */
|
|
8
13
|
export const FEED_CHOICES = ['all', 'only', 'except'];
|
|
14
|
+
/** How long the overview is. */
|
|
15
|
+
export const LENGTHS = ['brief', 'standard', 'detailed'];
|
|
16
|
+
/** How each section's points are shown: as paragraphs, or as a bulleted list. */
|
|
17
|
+
export const STYLES = ['paragraphs', 'bullets'];
|
|
18
|
+
/** Characters of a user's own instructions, at most. */
|
|
19
|
+
export const INSTRUCTIONS_LENGTH = 1000;
|
|
20
|
+
/** The sections a user starts with: a name, and what goes in it. */
|
|
21
|
+
export const DEFAULT_SECTIONS = [
|
|
22
|
+
{ name: 'World', about: 'International news, conflicts, and diplomacy' },
|
|
23
|
+
{ name: 'Politics', about: 'Governments, elections, laws, and policy' },
|
|
24
|
+
{ name: 'Business', about: 'Companies, markets, the economy, and work' },
|
|
25
|
+
{ name: 'Technology', about: 'Computing, the internet, AI, gadgets, and the tech industry' },
|
|
26
|
+
{ name: 'Science', about: 'Research, space, the environment, and climate' },
|
|
27
|
+
{ name: 'Health', about: 'Medicine, public health, and wellbeing' },
|
|
28
|
+
{ name: 'Sports', about: 'Games, results, athletes, and leagues' },
|
|
29
|
+
{ name: 'Culture', about: 'Arts, entertainment, books, games, and media' },
|
|
30
|
+
{ name: 'Other', about: 'Notable stories that fit no other section' },
|
|
31
|
+
];
|
|
32
|
+
/** The context a self-hosted model is given, in tokens, at least and by default. */
|
|
33
|
+
export const MIN_CONTEXT_TOKENS = 8_192;
|
|
34
|
+
export const DEFAULT_CONTEXT_TOKENS = 16_384;
|
|
9
35
|
const tokens = z.number().int().min(0).max(1_000_000_000);
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
36
|
+
/** Settings the form shows only for Claude, for a self-hosted model, or for Ollama. */
|
|
37
|
+
const anthropicOnly = { provider: ['anthropic'] };
|
|
38
|
+
const selfHosted = { provider: ['ollama', 'openai'] };
|
|
39
|
+
const ollamaOnly = { provider: ['ollama'] };
|
|
40
|
+
/**
|
|
41
|
+
* What an admin sets for the server: who writes the overview and how it is reached, the model,
|
|
42
|
+
* the budgets, and what users may add. Anthropic needs the API key; a self-hosted server, its URL.
|
|
43
|
+
*/
|
|
44
|
+
export const instanceSettings = z
|
|
45
|
+
.object({
|
|
46
|
+
provider: z.enum(PROVIDERS).default('anthropic'),
|
|
47
|
+
apiKey: shownWhen(anthropicOnly, secret().optional()),
|
|
48
|
+
model: shownWhen(anthropicOnly, z.enum(MODELS).default('claude-opus-5-5')),
|
|
49
|
+
effort: shownWhen(anthropicOnly, z.enum(EFFORTS).default('low')),
|
|
50
|
+
baseUrl: shownWhen(selfHosted, url().optional()),
|
|
51
|
+
// Never the Anthropic key, which is sent only to Anthropic.
|
|
52
|
+
localApiKey: shownWhen(selfHosted, secret().optional()),
|
|
53
|
+
localModel: shownWhen(selfHosted, z.string().trim().min(1).max(200).default('qwen3:8b')),
|
|
54
|
+
contextTokens: shownWhen(selfHosted, z.number().int().min(MIN_CONTEXT_TOKENS).max(1_000_000).default(DEFAULT_CONTEXT_TOKENS)),
|
|
55
|
+
parallelRequests: shownWhen(selfHosted, z.number().int().min(1).max(6).default(1)),
|
|
56
|
+
thinking: shownWhen(ollamaOnly, z.boolean().default(false)),
|
|
15
57
|
monthlyTokens: tokens.default(10_000_000),
|
|
16
58
|
userMonthlyTokens: tokens.default(1_000_000),
|
|
59
|
+
allowInstructions: z.boolean().default(true),
|
|
60
|
+
})
|
|
61
|
+
.superRefine((settings, context) => {
|
|
62
|
+
if (settings.provider === 'anthropic' && !settings.apiKey)
|
|
63
|
+
context.addIssue({
|
|
64
|
+
code: 'custom',
|
|
65
|
+
path: ['apiKey'],
|
|
66
|
+
message: 'Anthropic needs an API key',
|
|
67
|
+
});
|
|
68
|
+
if (settings.provider !== 'anthropic' && !settings.baseUrl)
|
|
69
|
+
context.addIssue({
|
|
70
|
+
code: 'custom',
|
|
71
|
+
path: ['baseUrl'],
|
|
72
|
+
message: 'A self-hosted model needs its server’s URL',
|
|
73
|
+
});
|
|
17
74
|
});
|
|
18
|
-
/**
|
|
75
|
+
/** A section of the overview, by name, with what goes in it. */
|
|
76
|
+
export const section = z.object({
|
|
77
|
+
name: z.string().trim().min(1).max(60),
|
|
78
|
+
about: z.string().trim().max(200).default(''),
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* What each user chooses: all their feeds, only some, or all but some; the sections, the length
|
|
82
|
+
* and style, whether a lead opens it, and instructions of their own.
|
|
83
|
+
*/
|
|
19
84
|
export const userSettings = z.object({
|
|
20
85
|
feedChoice: z.enum(FEED_CHOICES).default('all'),
|
|
21
86
|
feeds: feeds(),
|
|
87
|
+
sections: z
|
|
88
|
+
.array(section)
|
|
89
|
+
.max(DIGEST_LIMITS.sections)
|
|
90
|
+
.default(() => DEFAULT_SECTIONS.map((s) => ({ ...s }))),
|
|
91
|
+
length: z.enum(LENGTHS).default('standard'),
|
|
92
|
+
style: z.enum(STYLES).default('paragraphs'),
|
|
93
|
+
lead: z.boolean().default(true),
|
|
94
|
+
instructions: multiline().max(INSTRUCTIONS_LENGTH).default(''),
|
|
22
95
|
});
|
package/dist/tokens.d.ts
ADDED
package/dist/tokens.js
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newses/plugin-digest-summary",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A Newses plugin that opens your email digest with an overview of the common themes of your unread articles, written by Claude",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "A Newses plugin that opens your email digest with an overview of the common themes of your unread articles, written by Claude or a self-hosted model (Ollama, llama.cpp, vLLM\u2026)",
|
|
5
5
|
"license": "AGPL-3.0-or-later",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
"newses-plugin",
|
|
13
13
|
"digest",
|
|
14
14
|
"claude",
|
|
15
|
-
"anthropic"
|
|
15
|
+
"anthropic",
|
|
16
|
+
"ollama",
|
|
17
|
+
"llm",
|
|
18
|
+
"self-hosted"
|
|
16
19
|
],
|
|
17
20
|
"type": "module",
|
|
18
21
|
"exports": {
|
|
@@ -38,12 +41,12 @@
|
|
|
38
41
|
"prepack": "npm run build"
|
|
39
42
|
},
|
|
40
43
|
"peerDependencies": {
|
|
41
|
-
"@newses/plugin-api": "^0.1.
|
|
44
|
+
"@newses/plugin-api": "^0.1.3",
|
|
42
45
|
"zod": "^4.6.7"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"@eslint/js": "^10.0.1",
|
|
46
|
-
"@newses/plugin-api": "^0.1.
|
|
49
|
+
"@newses/plugin-api": "^0.1.3",
|
|
47
50
|
"@types/node": "^24.19.1",
|
|
48
51
|
"eslint": "^10.12.0",
|
|
49
52
|
"eslint-config-prettier": "^10.1.8",
|
package/dist/claude.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
-
/** The API's host, which an admin approves for the plugin. */
|
|
3
|
-
export const API_HOST = 'api.anthropic.com';
|
|
4
|
-
/** Server-side fallback when the model declines, on direct requests only. */
|
|
5
|
-
export const FALLBACK_BETA = 'server-side-fallback-2026-07-01';
|
|
6
|
-
/**
|
|
7
|
-
* A client sending requests through the hook's `fetch`, with the key from the settings. The host,
|
|
8
|
-
* key, and logging are set here, so nothing in the server's environment (`ANTHROPIC_*`) changes
|
|
9
|
-
* where requests go or what credentials they carry.
|
|
10
|
-
*/
|
|
11
|
-
export function clientFor(settings, fetch) {
|
|
12
|
-
return new Anthropic({
|
|
13
|
-
apiKey: settings.apiKey,
|
|
14
|
-
authToken: null,
|
|
15
|
-
baseURL: `https://${API_HOST}`,
|
|
16
|
-
fetch,
|
|
17
|
-
maxRetries: 1,
|
|
18
|
-
timeout: 40_000,
|
|
19
|
-
logLevel: 'off',
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
/** The text a response ends with: its text blocks after any switch to a fallback model. */
|
|
23
|
-
export function finalText(content) {
|
|
24
|
-
const switched = content.findLastIndex((block) => block.type === 'fallback');
|
|
25
|
-
return content
|
|
26
|
-
.slice(switched + 1)
|
|
27
|
-
.map((block) => (block.type === 'text' ? (block.text ?? '') : ''))
|
|
28
|
-
.join('');
|
|
29
|
-
}
|
|
30
|
-
/** An API failure as people read it, without anything from the request. */
|
|
31
|
-
export function failureText(error) {
|
|
32
|
-
if (error instanceof Anthropic.AuthenticationError)
|
|
33
|
-
return 'The API key was refused (HTTP 401): check it in the server’s settings';
|
|
34
|
-
if (error instanceof Anthropic.PermissionDeniedError)
|
|
35
|
-
return 'The API key may not use this model (HTTP 403)';
|
|
36
|
-
if (error instanceof Anthropic.NotFoundError)
|
|
37
|
-
return 'The model was not found (HTTP 404)';
|
|
38
|
-
if (error instanceof Anthropic.RateLimitError)
|
|
39
|
-
return 'Rate limited by the API (HTTP 429)';
|
|
40
|
-
if (error instanceof Anthropic.APIConnectionTimeoutError)
|
|
41
|
-
return 'The API did not answer in time';
|
|
42
|
-
if (error instanceof Anthropic.APIConnectionError)
|
|
43
|
-
return error.cause instanceof Error
|
|
44
|
-
? `The API could not be reached: ${error.cause.message}`
|
|
45
|
-
: 'The API could not be reached';
|
|
46
|
-
if (error instanceof Anthropic.APIError && error.status !== undefined)
|
|
47
|
-
return `The API answered HTTP ${String(error.status)}`;
|
|
48
|
-
return error instanceof Error ? error.message : String(error);
|
|
49
|
-
}
|