@accelerate/ai 0.0.0-stage → 0.1.0-next.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 CHANGED
@@ -1,3 +1,20 @@
1
- # Temporary Holding Version
1
+ # Accelerate Ai
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Model gateways, prompts, structured output, agents and evaluation.
4
+
5
+ Once published, install with `npm install @accelerate/ai@next`. See the [Accelerate SDK catalog](https://www.npmjs.com/package/@accelerate/sdk) for the other packages.
6
+
7
+ ## Import migration
8
+
9
+ Use the subpaths below. Update related imports together wherever your app shares class or provider identity.
10
+
11
+ | Existing import | Preview import |
12
+ | --- | --- |
13
+ | `@accelerate/agents-core` | `@accelerate/ai/agents` |
14
+ | `@accelerate/ai-core` | `@accelerate/ai/core` |
15
+ | `@accelerate/ai-evaluation` | `@accelerate/ai/evaluation` |
16
+ | `@accelerate/ai-gateway` | `@accelerate/ai/gateway` |
17
+ | `@accelerate/ai-prompts` | `@accelerate/ai/prompts` |
18
+ | `@accelerate/ai-structured` | `@accelerate/ai/structured` |
19
+ | `@accelerate/ai-tools` | `@accelerate/ai/tools` |
20
+ | `@accelerate/ai-usage` | `@accelerate/ai/usage` |
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { Message, ModelProvider, ModelRequest, Usage } from "../../ai-core/dist/index.js";
2
+ import type { ToolRegistry, ToolAuthorization } from "../../ai-tools/dist/index.js";
3
+ export interface AgentResult {
4
+ readonly content: string;
5
+ readonly messages: readonly Message[];
6
+ readonly usage: Usage;
7
+ readonly steps: number;
8
+ }
9
+ export declare function runAgent(provider: ModelProvider, tools: ToolRegistry, request: ModelRequest, options: {
10
+ authorize: ToolAuthorization;
11
+ maxSteps?: number;
12
+ maxToolCalls?: number;
13
+ }): Promise<AgentResult>;
@@ -0,0 +1,20 @@
1
+ export async function runAgent(provider, tools, request, options) { const maxSteps = options.maxSteps ?? 8, maxCalls = options.maxToolCalls ?? 16; if (!Number.isSafeInteger(maxSteps) || maxSteps < 1 || !Number.isSafeInteger(maxCalls) || maxCalls < 0)
2
+ throw new RangeError("Invalid agent limits"); const messages = [...request.messages]; let inputTokens = 0, outputTokens = 0, calls = 0; const seen = new Set(); for (let step = 1; step <= maxSteps; step++) {
3
+ request.signal?.throwIfAborted();
4
+ const response = await provider.generate({ ...request, messages: [...messages], tools: tools.definitions() });
5
+ inputTokens += response.usage?.inputTokens ?? 0;
6
+ outputTokens += response.usage?.outputTokens ?? 0;
7
+ messages.push({ role: "assistant", content: response.content, toolCalls: response.toolCalls });
8
+ if (!response.toolCalls?.length)
9
+ return { content: response.content, messages, usage: { inputTokens, outputTokens }, steps: step };
10
+ if (step === maxSteps || calls + response.toolCalls.length > maxCalls)
11
+ throw new Error("Agent execution limit reached");
12
+ for (const call of response.toolCalls) {
13
+ if (seen.has(call.id))
14
+ throw new Error("Duplicate tool call id");
15
+ seen.add(call.id);
16
+ calls++;
17
+ const result = await tools.execute(call, options.authorize, request.signal);
18
+ messages.push({ role: "tool", toolCallId: call.id, content: JSON.stringify(result) ?? "null" });
19
+ }
20
+ } throw new Error("Agent step limit reached"); }
@@ -0,0 +1,45 @@
1
+ export interface ToolCall {
2
+ readonly id: string;
3
+ readonly name: string;
4
+ readonly arguments: unknown;
5
+ }
6
+ export type Message = {
7
+ readonly role: "system" | "user";
8
+ readonly content: string;
9
+ } | {
10
+ readonly role: "assistant";
11
+ readonly content: string;
12
+ readonly toolCalls?: readonly ToolCall[];
13
+ } | {
14
+ readonly role: "tool";
15
+ readonly content: string;
16
+ readonly toolCallId: string;
17
+ };
18
+ export interface Usage {
19
+ readonly inputTokens: number;
20
+ readonly outputTokens: number;
21
+ }
22
+ export interface ToolDefinition {
23
+ readonly name: string;
24
+ readonly description: string;
25
+ readonly inputSchema: Readonly<Record<string, unknown>>;
26
+ }
27
+ export interface ModelRequest {
28
+ readonly model: string;
29
+ readonly messages: readonly Message[];
30
+ readonly tools?: readonly ToolDefinition[];
31
+ readonly signal?: AbortSignal;
32
+ readonly maxOutputTokens?: number;
33
+ }
34
+ export interface ModelResponse {
35
+ readonly content: string;
36
+ readonly toolCalls?: readonly ToolCall[];
37
+ readonly usage?: Usage;
38
+ }
39
+ export interface ModelProvider {
40
+ generate(request: ModelRequest): Promise<ModelResponse>;
41
+ }
42
+ export declare class ModelError extends Error {
43
+ readonly retryable: boolean;
44
+ constructor(message: string, retryable?: boolean, options?: ErrorOptions);
45
+ }
@@ -0,0 +1,7 @@
1
+ export class ModelError extends Error {
2
+ constructor(message, retryable = false, options) {
3
+ super(message, options);
4
+ this.retryable = retryable;
5
+ this.name = "ModelError";
6
+ }
7
+ }
@@ -0,0 +1,15 @@
1
+ import type { ModelProvider, ModelRequest, ModelResponse } from "../../ai-core/dist/index.js";
2
+ export interface EvaluationCase {
3
+ readonly id: string;
4
+ readonly request: ModelRequest;
5
+ score(response: ModelResponse): number | Promise<number>;
6
+ }
7
+ export declare function evaluate(provider: ModelProvider, cases: readonly EvaluationCase[]): Promise<{
8
+ id: string;
9
+ score: number;
10
+ error?: unknown;
11
+ }[]>;
12
+ export declare function assertEvaluation(results: readonly {
13
+ score: number;
14
+ error?: unknown;
15
+ }[], minimum: number): void;
@@ -0,0 +1,16 @@
1
+ import { AssertionFailure } from "@accelerate/testing/core";
2
+ export async function evaluate(provider, cases) { const results = []; for (const item of cases) {
3
+ item.request.signal?.throwIfAborted();
4
+ try {
5
+ const score = await item.score(await provider.generate(item.request));
6
+ if (!Number.isFinite(score) || score < 0 || score > 1)
7
+ throw new RangeError("Score must be between zero and one");
8
+ results.push({ id: item.id, score });
9
+ }
10
+ catch (error) {
11
+ item.request.signal?.throwIfAborted();
12
+ results.push({ id: item.id, score: 0, error });
13
+ }
14
+ } return results; }
15
+ export function assertEvaluation(results, minimum) { if (!Number.isFinite(minimum) || minimum < 0 || minimum > 1 || !results.length || results.some(r => Object.hasOwn(r, "error") || !Number.isFinite(r.score) || r.score < minimum || r.score > 1))
16
+ throw new AssertionFailure("Evaluation threshold not met"); }
@@ -0,0 +1,10 @@
1
+ import { type ModelProvider, type ModelRequest, type ModelResponse } from "../../ai-core/dist/index.js";
2
+ export interface Route {
3
+ readonly provider: ModelProvider;
4
+ readonly model: string;
5
+ }
6
+ export declare class ModelGateway implements ModelProvider {
7
+ private readonly routes;
8
+ constructor(routes: ReadonlyMap<string, readonly Route[]>);
9
+ generate(request: ModelRequest): Promise<ModelResponse>;
10
+ }
@@ -0,0 +1,19 @@
1
+ import { ModelError } from "../../ai-core/dist/index.js";
2
+ export class ModelGateway {
3
+ constructor(routes) {
4
+ this.routes = routes;
5
+ }
6
+ async generate(request) { const routes = this.routes.get(request.model); if (!routes?.length)
7
+ throw new ModelError("Unknown model route"); for (let i = 0; i < routes.length; i++) {
8
+ request.signal?.throwIfAborted();
9
+ const route = routes[i];
10
+ try {
11
+ return await route.provider.generate({ ...request, model: route.model });
12
+ }
13
+ catch (error) {
14
+ request.signal?.throwIfAborted();
15
+ if (!(error instanceof ModelError && error.retryable) || i === routes.length - 1)
16
+ throw error;
17
+ }
18
+ } throw new ModelError("No route available"); }
19
+ }
@@ -0,0 +1,10 @@
1
+ import type { Message } from "../../ai-core/dist/index.js";
2
+ export interface PromptTemplate {
3
+ readonly id: string;
4
+ readonly version: string;
5
+ readonly messages: readonly {
6
+ role: "system" | "user";
7
+ content: string;
8
+ }[];
9
+ }
10
+ export declare function renderPrompt(template: PromptTemplate, variables: Readonly<Record<string, string>>): readonly Message[];
@@ -0,0 +1,2 @@
1
+ export function renderPrompt(template, variables) { return template.messages.map(message => ({ ...message, content: message.content.replace(/\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g, (_, key) => { if (!Object.hasOwn(variables, key))
2
+ throw new Error("Missing prompt variable: " + key); return variables[key]; }) })); }
@@ -0,0 +1,3 @@
1
+ import type { ModelResponse } from "../../ai-core/dist/index.js";
2
+ import { type Schema } from "@accelerate/core/validation";
3
+ export declare function parseStructured<T>(response: ModelResponse, schema: Schema<T>): T;
@@ -0,0 +1,7 @@
1
+ import { validate } from "@accelerate/core/validation";
2
+ export function parseStructured(response, schema) { let value; try {
3
+ value = JSON.parse(response.content);
4
+ }
5
+ catch {
6
+ throw new Error("Model output is not valid JSON");
7
+ } return validate(schema, value); }
@@ -0,0 +1,13 @@
1
+ import type { ToolCall, ToolDefinition } from "../../ai-core/dist/index.js";
2
+ export interface RegisteredTool {
3
+ readonly definition: ToolDefinition;
4
+ validate(input: unknown): unknown;
5
+ execute(input: unknown, signal?: AbortSignal): Promise<unknown>;
6
+ }
7
+ export type ToolAuthorization = (call: ToolCall) => boolean | Promise<boolean>;
8
+ export declare class ToolRegistry {
9
+ private readonly tools;
10
+ register(tool: RegisteredTool): void;
11
+ definitions(): readonly ToolDefinition[];
12
+ execute(call: ToolCall, authorize: ToolAuthorization, signal?: AbortSignal): Promise<unknown>;
13
+ }
@@ -0,0 +1,11 @@
1
+ export class ToolRegistry {
2
+ constructor() {
3
+ this.tools = new Map();
4
+ }
5
+ register(tool) { if (!tool.definition.name || this.tools.has(tool.definition.name))
6
+ throw new Error("Invalid or duplicate tool"); this.tools.set(tool.definition.name, tool); }
7
+ definitions() { return [...this.tools.values()].map(t => t.definition); }
8
+ async execute(call, authorize, signal) { signal?.throwIfAborted(); const tool = this.tools.get(call.name); if (!tool)
9
+ throw new Error("Unknown tool"); if (!await authorize(call))
10
+ throw new Error("Tool execution denied"); signal?.throwIfAborted(); return tool.execute(tool.validate(call.arguments), signal); }
11
+ }
@@ -0,0 +1,15 @@
1
+ import type { Usage } from "../../ai-core/dist/index.js";
2
+ export interface Rates {
3
+ readonly inputPerMillion: number;
4
+ readonly outputPerMillion: number;
5
+ }
6
+ export declare function estimateCost(usage: Usage, rates: Rates): number;
7
+ export declare class UsageBudget {
8
+ readonly limit: number;
9
+ private spent;
10
+ constructor(limit: number);
11
+ get consumed(): number;
12
+ get remaining(): number;
13
+ canAfford(estimate: number): boolean;
14
+ record(cost: number): void;
15
+ }
@@ -0,0 +1,19 @@
1
+ export function estimateCost(usage, rates) { for (const n of [usage.inputTokens, usage.outputTokens])
2
+ if (!Number.isSafeInteger(n) || n < 0)
3
+ throw new RangeError("Invalid token count"); for (const n of [rates.inputPerMillion, rates.outputPerMillion])
4
+ if (!Number.isFinite(n) || n < 0)
5
+ throw new RangeError("Invalid rate"); const cost = (usage.inputTokens * rates.inputPerMillion + usage.outputTokens * rates.outputPerMillion) / 1e6; if (!Number.isFinite(cost))
6
+ throw new RangeError("Cost overflow"); return cost; }
7
+ export class UsageBudget {
8
+ constructor(limit) {
9
+ this.limit = limit;
10
+ this.spent = 0;
11
+ if (!Number.isFinite(limit) || limit < 0)
12
+ throw new RangeError("Invalid budget");
13
+ }
14
+ get consumed() { return this.spent; }
15
+ get remaining() { return Math.max(0, this.limit - this.spent); }
16
+ canAfford(estimate) { return Number.isFinite(estimate) && estimate >= 0 && estimate <= this.remaining; }
17
+ record(cost) { if (!Number.isFinite(cost) || cost < 0 || !Number.isFinite(this.spent + cost))
18
+ throw new RangeError("Invalid cost"); this.spent += cost; }
19
+ }
package/package.json CHANGED
@@ -1,6 +1,85 @@
1
1
  {
2
2
  "name": "@accelerate/ai",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-next.0",
4
+ "description": "Accelerate — Model gateways, prompts, structured output, agents and evaluation.",
5
+ "keywords": [
6
+ "accelerate",
7
+ "typescript",
8
+ "ai"
9
+ ],
10
+ "type": "module",
11
+ "sideEffects": false,
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./agents": {
20
+ "types": "./dist/modules/agents-core/dist/index.d.ts",
21
+ "default": "./dist/modules/agents-core/dist/index.js"
22
+ },
23
+ "./core": {
24
+ "types": "./dist/modules/ai-core/dist/index.d.ts",
25
+ "default": "./dist/modules/ai-core/dist/index.js"
26
+ },
27
+ "./evaluation": {
28
+ "types": "./dist/modules/ai-evaluation/dist/index.d.ts",
29
+ "default": "./dist/modules/ai-evaluation/dist/index.js"
30
+ },
31
+ "./gateway": {
32
+ "types": "./dist/modules/ai-gateway/dist/index.d.ts",
33
+ "default": "./dist/modules/ai-gateway/dist/index.js"
34
+ },
35
+ "./prompts": {
36
+ "types": "./dist/modules/ai-prompts/dist/index.d.ts",
37
+ "default": "./dist/modules/ai-prompts/dist/index.js"
38
+ },
39
+ "./structured": {
40
+ "types": "./dist/modules/ai-structured/dist/index.d.ts",
41
+ "default": "./dist/modules/ai-structured/dist/index.js"
42
+ },
43
+ "./tools": {
44
+ "types": "./dist/modules/ai-tools/dist/index.d.ts",
45
+ "default": "./dist/modules/ai-tools/dist/index.js"
46
+ },
47
+ "./usage": {
48
+ "types": "./dist/modules/ai-usage/dist/index.d.ts",
49
+ "default": "./dist/modules/ai-usage/dist/index.js"
50
+ }
51
+ },
52
+ "files": [
53
+ "dist"
54
+ ],
55
+ "scripts": {
56
+ "build": "node ../../tools/registry/consolidation.mjs build ai",
57
+ "typecheck": "tsc --noEmit --skipLibCheck --module NodeNext --moduleResolution NodeNext dist/index.d.ts"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/archubbuck/accelerate.git",
62
+ "directory": "packages/ai"
63
+ },
64
+ "homepage": "https://github.com/archubbuck/accelerate/blob/main/packages/ai/README.md",
65
+ "bugs": "https://github.com/archubbuck/accelerate/issues",
66
+ "publishConfig": {
67
+ "access": "public",
68
+ "registry": "https://registry.npmjs.org/",
69
+ "tag": "next"
70
+ },
71
+ "dependencies": {
72
+ "@accelerate/testing": "0.1.0-next.0",
73
+ "@accelerate/core": "0.1.0-next.0"
74
+ },
75
+ "devDependencies": {
76
+ "@accelerate/agents-core": "0.1.0",
77
+ "@accelerate/ai-core": "0.1.0",
78
+ "@accelerate/ai-evaluation": "0.1.0",
79
+ "@accelerate/ai-gateway": "0.1.0",
80
+ "@accelerate/ai-prompts": "0.1.0",
81
+ "@accelerate/ai-structured": "0.1.0",
82
+ "@accelerate/ai-tools": "0.1.0",
83
+ "@accelerate/ai-usage": "0.1.0"
84
+ }
85
+ }