write-language 0.1.21 → 0.1.23
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 +255 -255
- package/dist/write-language.cjs.js.map +1 -1
- package/dist/write-language.es.js.map +1 -1
- package/package.json +1 -1
- package/src/generate-response.ts +196 -196
- package/src/generation-types.ts +90 -90
- package/src/index.ts +40 -40
- package/src/language-model-families.ts +139 -139
- package/src/language-model-registry.ts +1336 -1336
- package/src/prompt-templates.ts +651 -651
- package/src/provider-factory.ts +98 -98
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "write-language",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
4
4
|
"description": "Multi-provider language generation toolkit using Vercel AI SDK - generate responses with 10+ LLM providers including OpenAI, Anthropic, Google, and more.",
|
|
5
5
|
"author": "vtempest <grokthiscontact@gmail.com>",
|
|
6
6
|
"license": "rights.institute/PROSPER",
|
package/src/generate-response.ts
CHANGED
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Core logic for generating AI language responses using Vercel AI SDK and various LLM providers.
|
|
3
|
-
* Handles prompt interpolation, tool calling, and response formatting.
|
|
4
|
-
*/
|
|
5
|
-
import { generateText, stepCountIs, tool } from "ai";
|
|
6
|
-
import { AGENT_PROMPTS } from "./prompt-templates";
|
|
7
|
-
import { AGENT_TOOLS } from "./tools/qwksearch-api-tools";
|
|
8
|
-
import { LANGUAGE_MODELS, LANGUAGE_PROVIDERS } from "./language-model-registry";
|
|
9
|
-
import { createLLMProvider } from "./provider-factory";
|
|
10
|
-
import { convertMarkdownToHTMLEscaped } from "./utils/markdown-to-html";
|
|
11
|
-
import type {
|
|
12
|
-
AgentPrompt,
|
|
13
|
-
AgentTool,
|
|
14
|
-
GenerateLanguageOptions,
|
|
15
|
-
GenerateLanguageResult,
|
|
16
|
-
} from "./generation-types";
|
|
17
|
-
|
|
18
|
-
export type {
|
|
19
|
-
LLMProviderName,
|
|
20
|
-
GenerateLanguageOptions,
|
|
21
|
-
GenerateLanguageResult,
|
|
22
|
-
} from "./generation-types";
|
|
23
|
-
export { convertMarkdownToHTMLEscaped } from "./utils/markdown-to-html";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* ### Generate Language Response
|
|
27
|
-
* Writes a language response that shows human-like understanding of the
|
|
28
|
-
* question and context.
|
|
29
|
-
* - _Requires_: LLM provider, API key, agent name, and context variables.
|
|
30
|
-
* - _Providers_: groq, togetherai, openai, anthropic, xai, google,
|
|
31
|
-
* perplexity, cloudflare, nvidia
|
|
32
|
-
* - _Agent Templates_: custom local entries defined in AGENT_PROMPTS.
|
|
33
|
-
* - _How it Works_: Language models predict the most likely next token given
|
|
34
|
-
* a prompt. They represent words as high-dimensional vectors, use
|
|
35
|
-
* transformer attention across all prior tokens, and sample from the
|
|
36
|
-
* resulting probability distribution to produce human-like text.
|
|
37
|
-
*
|
|
38
|
-
* @see [Vercel AI SDK generateText docs](https://sdk.vercel.ai/docs/reference/ai-sdk-core/generate-text)
|
|
39
|
-
* @see [Hugging Face tutorials](https://huggingface.co/learn)
|
|
40
|
-
* @see [Illustrated Transformer](https://jalammar.github.io/illustrated-transformer/)
|
|
41
|
-
* @see [Building a Transformer with PyTorch](https://www.datacamp.com/tutorial/building-a-transformer-with-py-torch)
|
|
42
|
-
* @see [LLM training example](https://github.com/vtempest/ai-research-agent/blob/master/packages/neural-net/src/train/predict-next-word.js)
|
|
43
|
-
*
|
|
44
|
-
* @param options - Configuration for the language-model call
|
|
45
|
-
* @returns Resolved response object with `content`, optional `extract`, or `error`
|
|
46
|
-
* @author [Language Model Researchers](https://arc.net/folder/D0472A20-9C20-4D3F-B145-D2865C0A9FEE)
|
|
47
|
-
* @example
|
|
48
|
-
* const response = await writeLanguageResponse({
|
|
49
|
-
* query: "Explain neural networks",
|
|
50
|
-
* agent: "question",
|
|
51
|
-
* provider: "groq",
|
|
52
|
-
* apiKey: "your-api-key",
|
|
53
|
-
* });
|
|
54
|
-
*/
|
|
55
|
-
export async function writeLanguageResponse(
|
|
56
|
-
options: GenerateLanguageOptions = {} as GenerateLanguageOptions,
|
|
57
|
-
): Promise<GenerateLanguageResult> {
|
|
58
|
-
const {
|
|
59
|
-
apiKey,
|
|
60
|
-
agent = "question",
|
|
61
|
-
temperature = 1,
|
|
62
|
-
html = true,
|
|
63
|
-
applyContextLimit = true,
|
|
64
|
-
...context
|
|
65
|
-
} = options;
|
|
66
|
-
|
|
67
|
-
// Normalise provider to lowercase for consistent switch matching
|
|
68
|
-
const provider = options.provider?.toLowerCase();
|
|
69
|
-
|
|
70
|
-
// Resolve model: explicit override \u2192 provider's registered default
|
|
71
|
-
const model =
|
|
72
|
-
options.model ??
|
|
73
|
-
(LANGUAGE_MODELS as Array<{ provider: string; default?: string }>).find(
|
|
74
|
-
(m) => m.provider.toLowerCase() === provider,
|
|
75
|
-
)?.default ??
|
|
76
|
-
"";
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
// \u2500\u2500 1. Validate required inputs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
80
|
-
const validProviders = LANGUAGE_PROVIDERS as string[];
|
|
81
|
-
if (!apiKey || !provider || !validProviders.includes(provider)) {
|
|
82
|
-
return {
|
|
83
|
-
error:
|
|
84
|
-
"API key and provider are required. Valid providers: " +
|
|
85
|
-
validProviders.join(", "),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// \u2500\u2500 2. Load agent prompt from local registry \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
90
|
-
const agentObject = (AGENT_PROMPTS as AgentPrompt[]).find(
|
|
91
|
-
(p) => p?.name === agent,
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
if (!agentObject) return { error: `Agent "${agent}" not found` };
|
|
95
|
-
|
|
96
|
-
// \u2500\u2500 3. Pre-process the prompt template via optional `before` hook \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
97
|
-
if (agentObject.before) {
|
|
98
|
-
agentObject.prompt = agentObject.before(agentObject.prompt, options);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// \u2500\u2500 4. Build template variable map and interpolate placeholders \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
102
|
-
const templateVars: Record<string, unknown> = {
|
|
103
|
-
...options,
|
|
104
|
-
input: `${context.query ?? ""} ${context.article ?? ""}`,
|
|
105
|
-
};
|
|
106
|
-
let prompt = interpolateTemplate(agentObject.template ?? "", templateVars);
|
|
107
|
-
|
|
108
|
-
// \u2500\u2500 5. Trim prompt to the model's context window \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
109
|
-
if (applyContextLimit) {
|
|
110
|
-
const modelConfig = (
|
|
111
|
-
LANGUAGE_MODELS as Array<{
|
|
112
|
-
provider: string;
|
|
113
|
-
models: Array<{ id: string; contextLength: number }>;
|
|
114
|
-
}>
|
|
115
|
-
)
|
|
116
|
-
.find((m) => m.provider.toLowerCase() === provider)
|
|
117
|
-
?.models.find((m) => m.id === model);
|
|
118
|
-
|
|
119
|
-
if (modelConfig) {
|
|
120
|
-
prompt = prompt.slice(0, modelConfig.contextLength);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// \u2500\u2500 6. Instantiate the LLM provider \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
125
|
-
const llm = createLLMProvider(provider, apiKey, model, temperature);
|
|
126
|
-
if (!llm) return { error: "Invalid provider selected" };
|
|
127
|
-
|
|
128
|
-
// \u2500\u2500 7. Resolve tools declared by the agent \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
129
|
-
const agentToolDefs = (AGENT_TOOLS as AgentTool[]).filter((t) =>
|
|
130
|
-
agentObject.tools?.includes(t.name),
|
|
131
|
-
);
|
|
132
|
-
const tools =
|
|
133
|
-
agentToolDefs.length > 0
|
|
134
|
-
? Object.fromEntries(
|
|
135
|
-
agentToolDefs.map((t) => [
|
|
136
|
-
t.name,
|
|
137
|
-
tool({
|
|
138
|
-
description: t.description as string,
|
|
139
|
-
inputSchema: t.schema as any,
|
|
140
|
-
execute: t.func as (args: any) => Promise<string>,
|
|
141
|
-
}),
|
|
142
|
-
]),
|
|
143
|
-
)
|
|
144
|
-
: undefined;
|
|
145
|
-
|
|
146
|
-
// \u2500\u2500 8. Invoke LLM via Vercel AI SDK generateText \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
147
|
-
const { text: rawReply } = await generateText({
|
|
148
|
-
model: llm,
|
|
149
|
-
prompt,
|
|
150
|
-
temperature,
|
|
151
|
-
...(tools && { tools, stopWhen: stepCountIs(10) }),
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
// \u2500\u2500 9. Format output (HTML or raw Markdown) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
155
|
-
const content: string = html
|
|
156
|
-
? await convertMarkdownToHTMLEscaped(rawReply)
|
|
157
|
-
: rawReply;
|
|
158
|
-
|
|
159
|
-
// \u2500\u2500 10. Extract structured data via optional `after` hook \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
160
|
-
const extract = agentObject.after?.(rawReply, options);
|
|
161
|
-
|
|
162
|
-
return { content, ...(extract !== undefined && { extract }) };
|
|
163
|
-
} catch (err) {
|
|
164
|
-
const error = err as { response?: { status?: number }; message?: string };
|
|
165
|
-
return {
|
|
166
|
-
error:
|
|
167
|
-
error.response?.status === 429
|
|
168
|
-
? "Rate limit exceeded. Please wait before trying again."
|
|
169
|
-
: (error.message ??
|
|
170
|
-
"Failed to generate response. Please try again later."),
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Substitutes `{variableName}` placeholders in a template string with values
|
|
177
|
-
* from `vars`. Object/array values are pretty-printed without braces or
|
|
178
|
-
* commas. Unmatched keys fall back to `"[not provided]"`.
|
|
179
|
-
*
|
|
180
|
-
* @param template - Template string containing `{key}` placeholders
|
|
181
|
-
* @param vars - Map of variable names to their replacement values
|
|
182
|
-
* @returns The fully interpolated string
|
|
183
|
-
*/
|
|
184
|
-
function interpolateTemplate(
|
|
185
|
-
template: string,
|
|
186
|
-
vars: Record<string, unknown>,
|
|
187
|
-
): string {
|
|
188
|
-
return template.replace(/\{(.+?)\}/g, (_match, key: string) => {
|
|
189
|
-
if (!(key in vars)) return "[not provided]";
|
|
190
|
-
const value = vars[key];
|
|
191
|
-
if (typeof value === "string") return value;
|
|
192
|
-
return JSON.stringify(value, null, 2)
|
|
193
|
-
.replace(/[{}"]/g, "")
|
|
194
|
-
.replace(/,/g, "\n");
|
|
195
|
-
});
|
|
196
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core logic for generating AI language responses using Vercel AI SDK and various LLM providers.
|
|
3
|
+
* Handles prompt interpolation, tool calling, and response formatting.
|
|
4
|
+
*/
|
|
5
|
+
import { generateText, stepCountIs, tool } from "ai";
|
|
6
|
+
import { AGENT_PROMPTS } from "./prompt-templates";
|
|
7
|
+
import { AGENT_TOOLS } from "./tools/qwksearch-api-tools";
|
|
8
|
+
import { LANGUAGE_MODELS, LANGUAGE_PROVIDERS } from "./language-model-registry";
|
|
9
|
+
import { createLLMProvider } from "./provider-factory";
|
|
10
|
+
import { convertMarkdownToHTMLEscaped } from "./utils/markdown-to-html";
|
|
11
|
+
import type {
|
|
12
|
+
AgentPrompt,
|
|
13
|
+
AgentTool,
|
|
14
|
+
GenerateLanguageOptions,
|
|
15
|
+
GenerateLanguageResult,
|
|
16
|
+
} from "./generation-types";
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
LLMProviderName,
|
|
20
|
+
GenerateLanguageOptions,
|
|
21
|
+
GenerateLanguageResult,
|
|
22
|
+
} from "./generation-types";
|
|
23
|
+
export { convertMarkdownToHTMLEscaped } from "./utils/markdown-to-html";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* ### Generate Language Response
|
|
27
|
+
* Writes a language response that shows human-like understanding of the
|
|
28
|
+
* question and context.
|
|
29
|
+
* - _Requires_: LLM provider, API key, agent name, and context variables.
|
|
30
|
+
* - _Providers_: groq, togetherai, openai, anthropic, xai, google,
|
|
31
|
+
* perplexity, cloudflare, nvidia
|
|
32
|
+
* - _Agent Templates_: custom local entries defined in AGENT_PROMPTS.
|
|
33
|
+
* - _How it Works_: Language models predict the most likely next token given
|
|
34
|
+
* a prompt. They represent words as high-dimensional vectors, use
|
|
35
|
+
* transformer attention across all prior tokens, and sample from the
|
|
36
|
+
* resulting probability distribution to produce human-like text.
|
|
37
|
+
*
|
|
38
|
+
* @see [Vercel AI SDK generateText docs](https://sdk.vercel.ai/docs/reference/ai-sdk-core/generate-text)
|
|
39
|
+
* @see [Hugging Face tutorials](https://huggingface.co/learn)
|
|
40
|
+
* @see [Illustrated Transformer](https://jalammar.github.io/illustrated-transformer/)
|
|
41
|
+
* @see [Building a Transformer with PyTorch](https://www.datacamp.com/tutorial/building-a-transformer-with-py-torch)
|
|
42
|
+
* @see [LLM training example](https://github.com/vtempest/ai-research-agent/blob/master/packages/neural-net/src/train/predict-next-word.js)
|
|
43
|
+
*
|
|
44
|
+
* @param options - Configuration for the language-model call
|
|
45
|
+
* @returns Resolved response object with `content`, optional `extract`, or `error`
|
|
46
|
+
* @author [Language Model Researchers](https://arc.net/folder/D0472A20-9C20-4D3F-B145-D2865C0A9FEE)
|
|
47
|
+
* @example
|
|
48
|
+
* const response = await writeLanguageResponse({
|
|
49
|
+
* query: "Explain neural networks",
|
|
50
|
+
* agent: "question",
|
|
51
|
+
* provider: "groq",
|
|
52
|
+
* apiKey: "your-api-key",
|
|
53
|
+
* });
|
|
54
|
+
*/
|
|
55
|
+
export async function writeLanguageResponse(
|
|
56
|
+
options: GenerateLanguageOptions = {} as GenerateLanguageOptions,
|
|
57
|
+
): Promise<GenerateLanguageResult> {
|
|
58
|
+
const {
|
|
59
|
+
apiKey,
|
|
60
|
+
agent = "question",
|
|
61
|
+
temperature = 1,
|
|
62
|
+
html = true,
|
|
63
|
+
applyContextLimit = true,
|
|
64
|
+
...context
|
|
65
|
+
} = options;
|
|
66
|
+
|
|
67
|
+
// Normalise provider to lowercase for consistent switch matching
|
|
68
|
+
const provider = options.provider?.toLowerCase();
|
|
69
|
+
|
|
70
|
+
// Resolve model: explicit override \u2192 provider's registered default
|
|
71
|
+
const model =
|
|
72
|
+
options.model ??
|
|
73
|
+
(LANGUAGE_MODELS as Array<{ provider: string; default?: string }>).find(
|
|
74
|
+
(m) => m.provider.toLowerCase() === provider,
|
|
75
|
+
)?.default ??
|
|
76
|
+
"";
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
// \u2500\u2500 1. Validate required inputs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
80
|
+
const validProviders = LANGUAGE_PROVIDERS as string[];
|
|
81
|
+
if (!apiKey || !provider || !validProviders.includes(provider)) {
|
|
82
|
+
return {
|
|
83
|
+
error:
|
|
84
|
+
"API key and provider are required. Valid providers: " +
|
|
85
|
+
validProviders.join(", "),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// \u2500\u2500 2. Load agent prompt from local registry \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
90
|
+
const agentObject = (AGENT_PROMPTS as AgentPrompt[]).find(
|
|
91
|
+
(p) => p?.name === agent,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (!agentObject) return { error: `Agent "${agent}" not found` };
|
|
95
|
+
|
|
96
|
+
// \u2500\u2500 3. Pre-process the prompt template via optional `before` hook \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
97
|
+
if (agentObject.before) {
|
|
98
|
+
agentObject.prompt = agentObject.before(agentObject.prompt, options);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// \u2500\u2500 4. Build template variable map and interpolate placeholders \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
102
|
+
const templateVars: Record<string, unknown> = {
|
|
103
|
+
...options,
|
|
104
|
+
input: `${context.query ?? ""} ${context.article ?? ""}`,
|
|
105
|
+
};
|
|
106
|
+
let prompt = interpolateTemplate(agentObject.template ?? "", templateVars);
|
|
107
|
+
|
|
108
|
+
// \u2500\u2500 5. Trim prompt to the model's context window \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
109
|
+
if (applyContextLimit) {
|
|
110
|
+
const modelConfig = (
|
|
111
|
+
LANGUAGE_MODELS as Array<{
|
|
112
|
+
provider: string;
|
|
113
|
+
models: Array<{ id: string; contextLength: number }>;
|
|
114
|
+
}>
|
|
115
|
+
)
|
|
116
|
+
.find((m) => m.provider.toLowerCase() === provider)
|
|
117
|
+
?.models.find((m) => m.id === model);
|
|
118
|
+
|
|
119
|
+
if (modelConfig) {
|
|
120
|
+
prompt = prompt.slice(0, modelConfig.contextLength);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// \u2500\u2500 6. Instantiate the LLM provider \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
125
|
+
const llm = createLLMProvider(provider, apiKey, model, temperature);
|
|
126
|
+
if (!llm) return { error: "Invalid provider selected" };
|
|
127
|
+
|
|
128
|
+
// \u2500\u2500 7. Resolve tools declared by the agent \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
129
|
+
const agentToolDefs = (AGENT_TOOLS as AgentTool[]).filter((t) =>
|
|
130
|
+
agentObject.tools?.includes(t.name),
|
|
131
|
+
);
|
|
132
|
+
const tools =
|
|
133
|
+
agentToolDefs.length > 0
|
|
134
|
+
? Object.fromEntries(
|
|
135
|
+
agentToolDefs.map((t) => [
|
|
136
|
+
t.name,
|
|
137
|
+
tool({
|
|
138
|
+
description: t.description as string,
|
|
139
|
+
inputSchema: t.schema as any,
|
|
140
|
+
execute: t.func as (args: any) => Promise<string>,
|
|
141
|
+
}),
|
|
142
|
+
]),
|
|
143
|
+
)
|
|
144
|
+
: undefined;
|
|
145
|
+
|
|
146
|
+
// \u2500\u2500 8. Invoke LLM via Vercel AI SDK generateText \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
147
|
+
const { text: rawReply } = await generateText({
|
|
148
|
+
model: llm,
|
|
149
|
+
prompt,
|
|
150
|
+
temperature,
|
|
151
|
+
...(tools && { tools, stopWhen: stepCountIs(10) }),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// \u2500\u2500 9. Format output (HTML or raw Markdown) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
155
|
+
const content: string = html
|
|
156
|
+
? await convertMarkdownToHTMLEscaped(rawReply)
|
|
157
|
+
: rawReply;
|
|
158
|
+
|
|
159
|
+
// \u2500\u2500 10. Extract structured data via optional `after` hook \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
160
|
+
const extract = agentObject.after?.(rawReply, options);
|
|
161
|
+
|
|
162
|
+
return { content, ...(extract !== undefined && { extract }) };
|
|
163
|
+
} catch (err) {
|
|
164
|
+
const error = err as { response?: { status?: number }; message?: string };
|
|
165
|
+
return {
|
|
166
|
+
error:
|
|
167
|
+
error.response?.status === 429
|
|
168
|
+
? "Rate limit exceeded. Please wait before trying again."
|
|
169
|
+
: (error.message ??
|
|
170
|
+
"Failed to generate response. Please try again later."),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Substitutes `{variableName}` placeholders in a template string with values
|
|
177
|
+
* from `vars`. Object/array values are pretty-printed without braces or
|
|
178
|
+
* commas. Unmatched keys fall back to `"[not provided]"`.
|
|
179
|
+
*
|
|
180
|
+
* @param template - Template string containing `{key}` placeholders
|
|
181
|
+
* @param vars - Map of variable names to their replacement values
|
|
182
|
+
* @returns The fully interpolated string
|
|
183
|
+
*/
|
|
184
|
+
function interpolateTemplate(
|
|
185
|
+
template: string,
|
|
186
|
+
vars: Record<string, unknown>,
|
|
187
|
+
): string {
|
|
188
|
+
return template.replace(/\{(.+?)\}/g, (_match, key: string) => {
|
|
189
|
+
if (!(key in vars)) return "[not provided]";
|
|
190
|
+
const value = vars[key];
|
|
191
|
+
if (typeof value === "string") return value;
|
|
192
|
+
return JSON.stringify(value, null, 2)
|
|
193
|
+
.replace(/[{}"]/g, "")
|
|
194
|
+
.replace(/,/g, "\n");
|
|
195
|
+
});
|
|
196
|
+
}
|
package/src/generation-types.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* @module research/agents/generate-language-types
|
|
4
|
-
* @description Shared types for the generate-language module.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/** Supported LLM provider identifiers */
|
|
8
|
-
export type LLMProviderName =
|
|
9
|
-
| "nvidia"
|
|
10
|
-
| "openrouter"
|
|
11
|
-
| "anthropic"
|
|
12
|
-
| "google"
|
|
13
|
-
| "openai"
|
|
14
|
-
| "xai"
|
|
15
|
-
| "groq"
|
|
16
|
-
| "cloudflare"
|
|
17
|
-
| "perplexity"
|
|
18
|
-
| "amazon"
|
|
19
|
-
| "bedrock"
|
|
20
|
-
| "togetherai"
|
|
21
|
-
| (string & {}); // preserve autocomplete while allowing arbitrary strings
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Configuration options for {@link writeLanguageResponse}.
|
|
25
|
-
*/
|
|
26
|
-
export interface GenerateLanguageOptions {
|
|
27
|
-
/** LLM provider to use */
|
|
28
|
-
provider: LLMProviderName;
|
|
29
|
-
/**
|
|
30
|
-
* API key for the provider.
|
|
31
|
-
* For `cloudflare`, use the `"apiToken:accountId"` format.
|
|
32
|
-
* For `amazon`/`bedrock`, use bearer token or `"region:accessKeyId:secretAccessKey"` format.
|
|
33
|
-
*/
|
|
34
|
-
apiKey?: string;
|
|
35
|
-
/** Agent prompt template name (default: `"question"`) */
|
|
36
|
-
agent?: string;
|
|
37
|
-
/** Specific model ID. Falls back to the provider's registered default. */
|
|
38
|
-
model?: string;
|
|
39
|
-
/**
|
|
40
|
-
* Sampling temperature (0\u20132).
|
|
41
|
-
* Lower = more deterministic; higher = more creative. Default: `1`
|
|
42
|
-
*/
|
|
43
|
-
temperature?: number;
|
|
44
|
-
/** User query text */
|
|
45
|
-
query?: string;
|
|
46
|
-
/** Article or document text to process */
|
|
47
|
-
article?: string;
|
|
48
|
-
/** Prior conversation history for context-aware agents */
|
|
49
|
-
chat_history?: string;
|
|
50
|
-
/** Return `HTML` (`true`) or raw Markdown (`false`). Default: `true` */
|
|
51
|
-
html?: boolean;
|
|
52
|
-
/** Truncate the prompt to the model's context window length. Default: `true` */
|
|
53
|
-
applyContextLimit?: boolean;
|
|
54
|
-
/** Additional template variables forwarded to the agent prompt */
|
|
55
|
-
[key: string]: unknown;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Return value of {@link writeLanguageResponse} */
|
|
59
|
-
export interface GenerateLanguageResult {
|
|
60
|
-
/** Generated response in HTML or Markdown format */
|
|
61
|
-
content?: string;
|
|
62
|
-
/** Structured data extracted by the agent's `after` callback */
|
|
63
|
-
extract?: unknown;
|
|
64
|
-
/** Human-readable error message when generation fails */
|
|
65
|
-
error?: string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Agent prompt template definition */
|
|
69
|
-
export interface AgentPrompt {
|
|
70
|
-
name: string;
|
|
71
|
-
/** Mustache-style template string with `{variableName}` placeholders */
|
|
72
|
-
template?: string;
|
|
73
|
-
prompt?: string;
|
|
74
|
-
/** Called before template substitution to pre-process the prompt */
|
|
75
|
-
before?: (
|
|
76
|
-
prompt: string | undefined,
|
|
77
|
-
options: GenerateLanguageOptions,
|
|
78
|
-
) => string;
|
|
79
|
-
/** Called after generation to extract structured data from the raw reply */
|
|
80
|
-
after?: (reply: string, options: GenerateLanguageOptions) => unknown;
|
|
81
|
-
/** Names of registered agent tools to attach to this agent */
|
|
82
|
-
tools?: string[];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** A registered agent tool with a name, schema, and async handler */
|
|
86
|
-
export interface AgentTool {
|
|
87
|
-
name: string;
|
|
88
|
-
func: (...args: unknown[]) => unknown | Promise<unknown>;
|
|
89
|
-
[key: string]: unknown;
|
|
90
|
-
}
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @module research/agents/generate-language-types
|
|
4
|
+
* @description Shared types for the generate-language module.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Supported LLM provider identifiers */
|
|
8
|
+
export type LLMProviderName =
|
|
9
|
+
| "nvidia"
|
|
10
|
+
| "openrouter"
|
|
11
|
+
| "anthropic"
|
|
12
|
+
| "google"
|
|
13
|
+
| "openai"
|
|
14
|
+
| "xai"
|
|
15
|
+
| "groq"
|
|
16
|
+
| "cloudflare"
|
|
17
|
+
| "perplexity"
|
|
18
|
+
| "amazon"
|
|
19
|
+
| "bedrock"
|
|
20
|
+
| "togetherai"
|
|
21
|
+
| (string & {}); // preserve autocomplete while allowing arbitrary strings
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Configuration options for {@link writeLanguageResponse}.
|
|
25
|
+
*/
|
|
26
|
+
export interface GenerateLanguageOptions {
|
|
27
|
+
/** LLM provider to use */
|
|
28
|
+
provider: LLMProviderName;
|
|
29
|
+
/**
|
|
30
|
+
* API key for the provider.
|
|
31
|
+
* For `cloudflare`, use the `"apiToken:accountId"` format.
|
|
32
|
+
* For `amazon`/`bedrock`, use bearer token or `"region:accessKeyId:secretAccessKey"` format.
|
|
33
|
+
*/
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
/** Agent prompt template name (default: `"question"`) */
|
|
36
|
+
agent?: string;
|
|
37
|
+
/** Specific model ID. Falls back to the provider's registered default. */
|
|
38
|
+
model?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Sampling temperature (0\u20132).
|
|
41
|
+
* Lower = more deterministic; higher = more creative. Default: `1`
|
|
42
|
+
*/
|
|
43
|
+
temperature?: number;
|
|
44
|
+
/** User query text */
|
|
45
|
+
query?: string;
|
|
46
|
+
/** Article or document text to process */
|
|
47
|
+
article?: string;
|
|
48
|
+
/** Prior conversation history for context-aware agents */
|
|
49
|
+
chat_history?: string;
|
|
50
|
+
/** Return `HTML` (`true`) or raw Markdown (`false`). Default: `true` */
|
|
51
|
+
html?: boolean;
|
|
52
|
+
/** Truncate the prompt to the model's context window length. Default: `true` */
|
|
53
|
+
applyContextLimit?: boolean;
|
|
54
|
+
/** Additional template variables forwarded to the agent prompt */
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Return value of {@link writeLanguageResponse} */
|
|
59
|
+
export interface GenerateLanguageResult {
|
|
60
|
+
/** Generated response in HTML or Markdown format */
|
|
61
|
+
content?: string;
|
|
62
|
+
/** Structured data extracted by the agent's `after` callback */
|
|
63
|
+
extract?: unknown;
|
|
64
|
+
/** Human-readable error message when generation fails */
|
|
65
|
+
error?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Agent prompt template definition */
|
|
69
|
+
export interface AgentPrompt {
|
|
70
|
+
name: string;
|
|
71
|
+
/** Mustache-style template string with `{variableName}` placeholders */
|
|
72
|
+
template?: string;
|
|
73
|
+
prompt?: string;
|
|
74
|
+
/** Called before template substitution to pre-process the prompt */
|
|
75
|
+
before?: (
|
|
76
|
+
prompt: string | undefined,
|
|
77
|
+
options: GenerateLanguageOptions,
|
|
78
|
+
) => string;
|
|
79
|
+
/** Called after generation to extract structured data from the raw reply */
|
|
80
|
+
after?: (reply: string, options: GenerateLanguageOptions) => unknown;
|
|
81
|
+
/** Names of registered agent tools to attach to this agent */
|
|
82
|
+
tools?: string[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A registered agent tool with a name, schema, and async handler */
|
|
86
|
+
export interface AgentTool {
|
|
87
|
+
name: string;
|
|
88
|
+
func: (...args: unknown[]) => unknown | Promise<unknown>;
|
|
89
|
+
[key: string]: unknown;
|
|
90
|
+
}
|