@alveus-ai/std 0.1.36 → 0.1.39
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/examples/README.md +32 -0
- package/examples/fan-out.ts +58 -0
- package/examples/pipeline.ts +72 -0
- package/examples/router.ts +73 -0
- package/examples/select.ts +100 -0
- package/package.json +3 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @alveus-ai/std Examples
|
|
2
|
+
|
|
3
|
+
Ready-to-deploy agent examples demonstrating composition patterns.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
| File | Pattern | Description |
|
|
8
|
+
| ------------- | ------------- | ---------------------------------------------------------- |
|
|
9
|
+
| `fan-out.ts` | mapAgent | Scrape multiple URLs in parallel with concurrency control |
|
|
10
|
+
| `pipeline.ts` | pipelineAgent | Fetch, summarize, then translate content sequentially |
|
|
11
|
+
| `router.ts` | routerAgent | Route support tickets to specialized handlers |
|
|
12
|
+
| `select.ts` | selectAgent | Race multiple summarizers, pick the best via a judge agent |
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Copy any example into your project as a starting point:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
alveus init
|
|
20
|
+
cp node_modules/@alveus-ai/std/examples/fan-out.ts src/agent.ts
|
|
21
|
+
# Edit to fit your use case
|
|
22
|
+
alveus deploy
|
|
23
|
+
alveus run @yourscope/fan-out --input '{"urls": ["https://example.com"]}'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Requirements
|
|
27
|
+
|
|
28
|
+
- `@alveus-ai/core` and `@alveus-ai/std` installed
|
|
29
|
+
- LLM API key configured via `alveus secrets set`
|
|
30
|
+
- An `alveus.config.ts` with your LLM provider defaults
|
|
31
|
+
|
|
32
|
+
See the [docs](https://www.alveus.ai/docs/concepts/agent-composition) for full API reference.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fan-Out Example
|
|
3
|
+
*
|
|
4
|
+
* Process a batch of items in parallel using mapAgent.
|
|
5
|
+
* Each item is handled by a dedicated child agent with concurrency control.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* alveus deploy
|
|
9
|
+
* alveus run @yourscope/fan-out --input '{"urls": ["https://a.com", "https://b.com"]}'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { agent$ } from '@alveus-ai/core';
|
|
13
|
+
import type { ExecutionContext } from '@alveus-ai/core';
|
|
14
|
+
import { mapAgent } from '@alveus-ai/std/composition';
|
|
15
|
+
|
|
16
|
+
interface ScrapeInput {
|
|
17
|
+
url: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ScrapeOutput {
|
|
21
|
+
url: string;
|
|
22
|
+
title: string;
|
|
23
|
+
summary: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const scraper = agent$(async (_state, input: ScrapeInput, ctx: ExecutionContext) => {
|
|
27
|
+
const response = await ctx.fetch(input.url);
|
|
28
|
+
const html = await response.text();
|
|
29
|
+
|
|
30
|
+
const result = await ctx.llm.chat([
|
|
31
|
+
{
|
|
32
|
+
role: 'user',
|
|
33
|
+
content: `Extract the title and a one-sentence summary from this HTML:\n\n${html.slice(0, 5000)}`,
|
|
34
|
+
},
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
const parsed = JSON.parse(result);
|
|
38
|
+
return { url: input.url, title: parsed.title, summary: parsed.summary } as ScrapeOutput;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
interface FanOutInput {
|
|
42
|
+
urls: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const fanOut = agent$(async (_state, input: FanOutInput, ctx: ExecutionContext) => {
|
|
46
|
+
const report = await mapAgent<string, ScrapeOutput>(ctx, scraper, input.urls, {
|
|
47
|
+
concurrency: 5,
|
|
48
|
+
failureMode: 'collect-errors',
|
|
49
|
+
inputFn: (url) => ({ url }),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
results: report.results.filter(Boolean),
|
|
54
|
+
succeeded: report.succeeded,
|
|
55
|
+
failed: report.failed,
|
|
56
|
+
total: report.total,
|
|
57
|
+
};
|
|
58
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pipeline Example
|
|
3
|
+
*
|
|
4
|
+
* Chain agents sequentially where each stage's output feeds the next.
|
|
5
|
+
* Useful for multi-step content processing workflows.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* alveus deploy
|
|
9
|
+
* alveus run @yourscope/content-pipeline --input '{"url": "https://example.com/article"}'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { agent$ } from '@alveus-ai/core';
|
|
13
|
+
import type { ExecutionContext } from '@alveus-ai/core';
|
|
14
|
+
import { pipelineAgent } from '@alveus-ai/std/composition';
|
|
15
|
+
|
|
16
|
+
const fetcher = agent$(async (_state, input: { url: string }, ctx: ExecutionContext) => {
|
|
17
|
+
const response = await ctx.fetch(input.url);
|
|
18
|
+
const content = await response.text();
|
|
19
|
+
return { content, source: input.url };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const summarizer = agent$(
|
|
23
|
+
async (_state, input: { content: string }, ctx: ExecutionContext) => {
|
|
24
|
+
const summary = await ctx.llm.chat([
|
|
25
|
+
{ role: 'system', content: 'Summarize the following content in 3 bullet points.' },
|
|
26
|
+
{ role: 'user', content: input.content.slice(0, 10000) },
|
|
27
|
+
]);
|
|
28
|
+
return { summary };
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const translator = agent$(
|
|
33
|
+
async (_state, input: { summary: string; targetLang: string }, ctx: ExecutionContext) => {
|
|
34
|
+
const translated = await ctx.llm.chat([
|
|
35
|
+
{
|
|
36
|
+
role: 'user',
|
|
37
|
+
content: `Translate to ${input.targetLang}:\n\n${input.summary}`,
|
|
38
|
+
},
|
|
39
|
+
]);
|
|
40
|
+
return { translated, language: input.targetLang };
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
interface PipelineInput {
|
|
45
|
+
url: string;
|
|
46
|
+
targetLang?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const contentPipeline = agent$(
|
|
50
|
+
async (_state, input: PipelineInput, ctx: ExecutionContext) => {
|
|
51
|
+
const result = await pipelineAgent(
|
|
52
|
+
ctx,
|
|
53
|
+
[
|
|
54
|
+
{ agent: fetcher },
|
|
55
|
+
{
|
|
56
|
+
agent: summarizer,
|
|
57
|
+
transformInput: (prev: { content: string }) => ({ content: prev.content }),
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
agent: translator,
|
|
61
|
+
transformInput: (prev: { summary: string }) => ({
|
|
62
|
+
summary: prev.summary,
|
|
63
|
+
targetLang: input.targetLang ?? 'Spanish',
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
{ url: input.url },
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
},
|
|
72
|
+
);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Router Example
|
|
3
|
+
*
|
|
4
|
+
* Route incoming requests to different handler agents based on predicates.
|
|
5
|
+
* Useful for ticket triage, content classification, or multi-tenant dispatch.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* alveus deploy
|
|
9
|
+
* alveus run @yourscope/ticket-router --input '{"subject": "Payment failed", "priority": "high", "body": "..."}'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { agent$ } from '@alveus-ai/core';
|
|
13
|
+
import type { ExecutionContext } from '@alveus-ai/core';
|
|
14
|
+
import { routerAgent } from '@alveus-ai/std/composition';
|
|
15
|
+
|
|
16
|
+
interface Ticket {
|
|
17
|
+
subject: string;
|
|
18
|
+
priority: string;
|
|
19
|
+
body: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Resolution {
|
|
23
|
+
action: string;
|
|
24
|
+
response: string;
|
|
25
|
+
escalated: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const urgentHandler = agent$(async (_state, input: Ticket, ctx: ExecutionContext) => {
|
|
29
|
+
const response = await ctx.llm.chat([
|
|
30
|
+
{ role: 'system', content: 'You handle urgent tickets. Be concise and action-oriented.' },
|
|
31
|
+
{ role: 'user', content: `Subject: ${input.subject}\n\n${input.body}` },
|
|
32
|
+
]);
|
|
33
|
+
return { action: 'immediate-response', response, escalated: true } as Resolution;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const billingHandler = agent$(async (_state, input: Ticket, ctx: ExecutionContext) => {
|
|
37
|
+
const response = await ctx.llm.chat([
|
|
38
|
+
{ role: 'system', content: 'You handle billing issues. Check common billing problems.' },
|
|
39
|
+
{ role: 'user', content: `Subject: ${input.subject}\n\n${input.body}` },
|
|
40
|
+
]);
|
|
41
|
+
return { action: 'billing-resolution', response, escalated: false } as Resolution;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const generalHandler = agent$(async (_state, input: Ticket, ctx: ExecutionContext) => {
|
|
45
|
+
const response = await ctx.llm.chat([
|
|
46
|
+
{ role: 'system', content: 'You handle general support tickets.' },
|
|
47
|
+
{ role: 'user', content: `Subject: ${input.subject}\n\n${input.body}` },
|
|
48
|
+
]);
|
|
49
|
+
return { action: 'general-response', response, escalated: false } as Resolution;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const ticketRouter = agent$(async (_state, input: Ticket, ctx: ExecutionContext) => {
|
|
53
|
+
const result = await routerAgent<Ticket, Resolution>(
|
|
54
|
+
ctx,
|
|
55
|
+
[
|
|
56
|
+
{
|
|
57
|
+
when: (ticket) => ticket.priority === 'high' || ticket.priority === 'critical',
|
|
58
|
+
agent: urgentHandler,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
when: (ticket) =>
|
|
62
|
+
ticket.subject.toLowerCase().includes('payment') ||
|
|
63
|
+
ticket.subject.toLowerCase().includes('billing') ||
|
|
64
|
+
ticket.subject.toLowerCase().includes('invoice'),
|
|
65
|
+
agent: billingHandler,
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
input,
|
|
69
|
+
{ defaultAgent: generalHandler },
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select Example
|
|
3
|
+
*
|
|
4
|
+
* Race multiple model configurations against each other and pick the best result.
|
|
5
|
+
* Useful for quality-sensitive tasks where you want to compare approaches.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* alveus deploy
|
|
9
|
+
* alveus run @yourscope/best-summary --input '{"text": "...", "maxLength": 100}'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { agent$ } from '@alveus-ai/core';
|
|
13
|
+
import type { ExecutionContext } from '@alveus-ai/core';
|
|
14
|
+
import { selectAgent } from '@alveus-ai/std/composition';
|
|
15
|
+
|
|
16
|
+
interface SummaryInput {
|
|
17
|
+
text: string;
|
|
18
|
+
maxLength: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface SummaryOutput {
|
|
22
|
+
summary: string;
|
|
23
|
+
approach: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const conciseSummarizer = agent$(
|
|
27
|
+
async (_state, input: SummaryInput, ctx: ExecutionContext) => {
|
|
28
|
+
const summary = await ctx.llm.chat([
|
|
29
|
+
{
|
|
30
|
+
role: 'system',
|
|
31
|
+
content: `Summarize in under ${input.maxLength} characters. Be extremely concise.`,
|
|
32
|
+
},
|
|
33
|
+
{ role: 'user', content: input.text },
|
|
34
|
+
]);
|
|
35
|
+
return { summary, approach: 'concise' } as SummaryOutput;
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const detailedSummarizer = agent$(
|
|
40
|
+
async (_state, input: SummaryInput, ctx: ExecutionContext) => {
|
|
41
|
+
const summary = await ctx.llm.chat([
|
|
42
|
+
{
|
|
43
|
+
role: 'system',
|
|
44
|
+
content: `Summarize in under ${input.maxLength} characters. Preserve key details and nuance.`,
|
|
45
|
+
},
|
|
46
|
+
{ role: 'user', content: input.text },
|
|
47
|
+
]);
|
|
48
|
+
return { summary, approach: 'detailed' } as SummaryOutput;
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const bulletSummarizer = agent$(
|
|
53
|
+
async (_state, input: SummaryInput, ctx: ExecutionContext) => {
|
|
54
|
+
const summary = await ctx.llm.chat([
|
|
55
|
+
{
|
|
56
|
+
role: 'system',
|
|
57
|
+
content: `Summarize as bullet points, total under ${input.maxLength} characters.`,
|
|
58
|
+
},
|
|
59
|
+
{ role: 'user', content: input.text },
|
|
60
|
+
]);
|
|
61
|
+
return { summary, approach: 'bullets' } as SummaryOutput;
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const judge = agent$(async (_state, input: any, ctx: ExecutionContext) => {
|
|
66
|
+
const candidates = input.candidates
|
|
67
|
+
.filter((c: any) => !c.failed)
|
|
68
|
+
.map((c: any) => `[${c.label}]: ${c.result?.summary}`)
|
|
69
|
+
.join('\n\n');
|
|
70
|
+
|
|
71
|
+
const verdict = await ctx.llm.chat([
|
|
72
|
+
{
|
|
73
|
+
role: 'system',
|
|
74
|
+
content:
|
|
75
|
+
'Pick the best summary. Return JSON: {"winnerIndex": <number>} where the index matches the candidate array position.',
|
|
76
|
+
},
|
|
77
|
+
{ role: 'user', content: `Original text: ${input.originalInput.text}\n\nCandidates:\n${candidates}` },
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
return JSON.parse(verdict);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
export const bestSummary = agent$(async (_state, input: SummaryInput, ctx: ExecutionContext) => {
|
|
84
|
+
const { winner, winnerIndex } = await selectAgent<SummaryOutput>(
|
|
85
|
+
ctx,
|
|
86
|
+
[
|
|
87
|
+
{ agent: conciseSummarizer, label: 'concise' },
|
|
88
|
+
{ agent: detailedSummarizer, label: 'detailed' },
|
|
89
|
+
{ agent: bulletSummarizer, label: 'bullets' },
|
|
90
|
+
],
|
|
91
|
+
input,
|
|
92
|
+
{ mode: { kind: 'judge', judgeAgent: judge } },
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
summary: winner.summary,
|
|
97
|
+
selectedApproach: winner.approach,
|
|
98
|
+
winnerIndex,
|
|
99
|
+
};
|
|
100
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alveus-ai/std",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.39",
|
|
4
4
|
"description": "Standard library of reusable agents and utilities for Alveus",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
|
-
"dist"
|
|
35
|
+
"dist",
|
|
36
|
+
"examples"
|
|
36
37
|
],
|
|
37
38
|
"scripts": {
|
|
38
39
|
"build": "tsc && node --import tsx scripts/build-blobs.ts",
|