@hiper2d/ai-agents 0.1.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 +21 -0
- package/README.md +85 -0
- package/dist/index.d.mts +1191 -0
- package/dist/index.d.ts +1191 -0
- package/dist/index.js +4496 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4364 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hiper2d
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @hiper2d/ai-agents
|
|
2
|
+
|
|
3
|
+
Multi-provider AI agent layer for TypeScript apps: one `AbstractAgent` interface over 11 LLM
|
|
4
|
+
providers, schema-validated JSON asks (zod), reasoning/thinking extraction, a model catalog
|
|
5
|
+
with per-model tuning defaults, and token cost accounting (cache tiers, extended context,
|
|
6
|
+
peak-valley pricing).
|
|
7
|
+
|
|
8
|
+
Extracted from the [AI Werewolf](https://aiwerewolf.net) game so its model layer can be
|
|
9
|
+
shared across apps. Text agents today; TTS, STT and image-generation agents are planned as
|
|
10
|
+
subpath exports.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i @hiper2d/ai-agents zod
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`zod` is a peer dependency on purpose: the schema converter reads zod internals, and two
|
|
19
|
+
copies of zod across a package boundary would break it.
|
|
20
|
+
|
|
21
|
+
## Use
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { AgentFactory, LLM_CONSTANTS, API_KEY_CONSTANTS } from '@hiper2d/ai-agents';
|
|
25
|
+
import { z } from 'zod';
|
|
26
|
+
|
|
27
|
+
const agent = AgentFactory.createAgent(
|
|
28
|
+
'Mira', // agent name (used in logs and cache keys)
|
|
29
|
+
'You are Mira, a retired cartographer.', // system instruction
|
|
30
|
+
LLM_CONSTANTS.CLAUDE_SONNET, // catalog id
|
|
31
|
+
{ [API_KEY_CONSTANTS.ANTHROPIC]: process.env.ANTHROPIC_API_KEY! },
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const [answer, thinking, usage] = await agent.askWithZodSchema(
|
|
35
|
+
z.object({ reply: z.string() }),
|
|
36
|
+
[{ role: 'user', content: 'What do you see?' }],
|
|
37
|
+
);
|
|
38
|
+
// answer.reply, thinking (provider reasoning, when surfaced), usage.costUSD
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`askText(messages)` returns plain text the same way. Every agent honors per-instance
|
|
42
|
+
`maxOutputTokens`, `reasoningEffort` and `thinkingBudgetTokens` (catalog defaults,
|
|
43
|
+
overridable per call).
|
|
44
|
+
|
|
45
|
+
### Catalog and pricing
|
|
46
|
+
|
|
47
|
+
`SupportedAiModels` is the model catalog (API names, thinking dialect, reasoning-effort
|
|
48
|
+
pins, output ceilings, speed/cost tags); `MODEL_PRICING` the price table; `calculateModelCost`
|
|
49
|
+
the cost function. Consumers layer their own policy on top:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { createCatalog } from '@hiper2d/ai-agents';
|
|
53
|
+
const catalog = createCatalog({ glm: { temperature: 0.9 } }); // shallow per-model overrides
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Logging
|
|
57
|
+
|
|
58
|
+
The library logs through an injectable sink — `setLlmLogger(fn)` — so a host app can route
|
|
59
|
+
agent request/response logs to its own logger. Default: console.
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm test # unit suites (mocked, free)
|
|
65
|
+
npm run test:live # *.live.test.ts — real provider calls; needs keys in .env (see .env.example)
|
|
66
|
+
npm run build # tsup → dist/ (cjs + esm + d.ts)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Live suites skip themselves per provider when the key is missing.
|
|
70
|
+
|
|
71
|
+
## Releasing
|
|
72
|
+
|
|
73
|
+
Bump `version` in package.json, commit, then tag and push:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
git tag v0.1.1 && git push origin v0.1.1
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `Publish` workflow verifies the tag matches the package version, runs typecheck/tests/build,
|
|
80
|
+
and publishes to npm with provenance via Trusted Publishing (OIDC — no token secret; configured
|
|
81
|
+
once on the npm package's settings page). A tag whose version is already on npm is a no-op.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|