@arcforge/types 2.0.59
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/package.json +29 -0
- package/src/bench/aggregate.ts +67 -0
- package/src/bench/artifact.ts +39 -0
- package/src/bench/bench.ts +62 -0
- package/src/bench/contribution.ts +40 -0
- package/src/bench/dimension.ts +15 -0
- package/src/bench/execution.ts +76 -0
- package/src/bench/factor.ts +69 -0
- package/src/bench/index.ts +108 -0
- package/src/bench/manifest.ts +49 -0
- package/src/bench/measurement.ts +41 -0
- package/src/bench/observation.ts +42 -0
- package/src/bench/protocol.ts +21 -0
- package/src/bench/result.ts +32 -0
- package/src/bench/value.ts +7 -0
- package/src/bench/workspace.ts +76 -0
- package/src/blueprint.ts +125 -0
- package/src/capsule-config.ts +111 -0
- package/src/cognet/blueprint.ts +38 -0
- package/src/cognet/cognet.ts +69 -0
- package/src/cognet/hooks.ts +43 -0
- package/src/cognet/index.ts +3 -0
- package/src/config.ts +72 -0
- package/src/define.ts +184 -0
- package/src/deploy.ts +54 -0
- package/src/engine.ts +254 -0
- package/src/error.ts +88 -0
- package/src/handle.ts +137 -0
- package/src/hooks.ts +31 -0
- package/src/http.ts +49 -0
- package/src/index.ts +23 -0
- package/src/kernel/abi.ts +244 -0
- package/src/kernel/index.ts +2 -0
- package/src/middleware.ts +7 -0
- package/src/module.ts +193 -0
- package/src/plugin.ts +9 -0
- package/src/policy.ts +89 -0
- package/src/prompts.ts +25 -0
- package/src/route.ts +15 -0
- package/src/scope.ts +29 -0
- package/src/scripts.ts +21 -0
- package/src/session/envelope.ts +61 -0
- package/src/session/events/activity.ts +77 -0
- package/src/session/events/bench.ts +49 -0
- package/src/session/events/capsule.ts +114 -0
- package/src/session/events/cognet.ts +51 -0
- package/src/session/events/engine.ts +46 -0
- package/src/session/events/entries.ts +66 -0
- package/src/session/events/kernel.ts +44 -0
- package/src/session/events/log.ts +27 -0
- package/src/session/events/runtime.ts +76 -0
- package/src/session/events/stdio/output.ts +94 -0
- package/src/session/events/stdio/shared.ts +50 -0
- package/src/session/events/stdio/stimuli.ts +110 -0
- package/src/session/events/test.ts +52 -0
- package/src/session/index.ts +40 -0
- package/src/session/session.ts +107 -0
- package/src/tools.ts +54 -0
- package/tsconfig.base.json +14 -0
package/src/define.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { AxonConfig } from "./config"
|
|
2
|
+
import type { BenchConfig, BenchDefinition } from "./bench"
|
|
3
|
+
import type { ModuleConfig, ModuleOptionSchema } from "./module"
|
|
4
|
+
|
|
5
|
+
/** Return shape produced by `defineAgent()`. */
|
|
6
|
+
export type AgentDefinition<EventMap extends Record<string, unknown> = Record<string, unknown>> = {
|
|
7
|
+
_kind: "agent"
|
|
8
|
+
config: AxonConfig<EventMap>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Defines an Axon agent configuration.
|
|
13
|
+
*
|
|
14
|
+
* Use this as the default export from `axon.config.ts`. The helper preserves
|
|
15
|
+
* the exact shape of your config for editor inference while marking the export
|
|
16
|
+
* as an Axon agent definition for the CLI and runtime.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { Axon } from "@arcforge/engines"
|
|
20
|
+
*
|
|
21
|
+
* export default defineAgent({
|
|
22
|
+
* description: "Reviews incoming pull requests",
|
|
23
|
+
* engine: Axon({ model: "claude-sonnet-4-6" }),
|
|
24
|
+
* policy: {
|
|
25
|
+
* fs: { read: ["./src/**"], write: ["./reports/**"] },
|
|
26
|
+
* network: { allow: ["api.github.com"] },
|
|
27
|
+
* },
|
|
28
|
+
* })
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see https://axon.arclabs.it/docs/v2/agent/config
|
|
32
|
+
* @see https://axon.arclabs.it/docs/v2/api/config/engine
|
|
33
|
+
* @see https://axon.arclabs.it/docs/v2/api/config/policy
|
|
34
|
+
*/
|
|
35
|
+
export function defineAgent<EventMap extends Record<string, unknown> = Record<string, unknown>>(
|
|
36
|
+
config: AxonConfig<EventMap>
|
|
37
|
+
): AgentDefinition<EventMap> {
|
|
38
|
+
return { _kind: "agent", config }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Defines a reusable Axon module configuration.
|
|
43
|
+
*
|
|
44
|
+
* Use this as the default export from `module.config.ts`. Modules can contribute
|
|
45
|
+
* tools, prompts, scripts, routes, environment requirements, policy needs, and
|
|
46
|
+
* boot-time setup hooks to any agent that installs them.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* export default defineModule({
|
|
50
|
+
* name: "github",
|
|
51
|
+
* description: "GitHub issue and pull request tools",
|
|
52
|
+
* options: {
|
|
53
|
+
* owner: { type: "string", required: true },
|
|
54
|
+
* repo: { type: "string", required: true },
|
|
55
|
+
* },
|
|
56
|
+
* env: {
|
|
57
|
+
* GITHUB_TOKEN: { required: true, description: "Token used for GitHub API calls" },
|
|
58
|
+
* },
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see https://axon.arclabs.it/docs/v2/modules/config
|
|
63
|
+
* @see https://axon.arclabs.it/docs/v2/modules/building
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Defines a benchmark configuration.
|
|
67
|
+
*
|
|
68
|
+
* Use this as the default export from `bench.config.ts`. Declares which
|
|
69
|
+
* control axes the harness sweeps. Tasks remain ordinary Bun tests; this
|
|
70
|
+
* file only declares the experiment applied to them.
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* export default defineBench({
|
|
74
|
+
* description: "Software engineering tasks",
|
|
75
|
+
* factors: [{
|
|
76
|
+
* id: "model",
|
|
77
|
+
* label: "Model",
|
|
78
|
+
* kind: "model",
|
|
79
|
+
* role: "subject",
|
|
80
|
+
* values: [sonnet, haiku],
|
|
81
|
+
* }],
|
|
82
|
+
* measurements: [{
|
|
83
|
+
* id: "correct",
|
|
84
|
+
* label: "Correct",
|
|
85
|
+
* description: "Whether the answer is correct",
|
|
86
|
+
* value: { kind: "boolean" },
|
|
87
|
+
* aggregate: "rate",
|
|
88
|
+
* }],
|
|
89
|
+
* trials: 5,
|
|
90
|
+
* tests: ["tests/<glob>.bench.ts"],
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @see https://axon.arclabs.it/docs/v2/bench/config
|
|
95
|
+
*/
|
|
96
|
+
export function defineBench(config: BenchConfig): BenchDefinition {
|
|
97
|
+
return { _kind: "bench", config }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function defineModule<O extends Record<string, ModuleOptionSchema>>(config: ModuleConfig<O>): ModuleConfig<O> {
|
|
101
|
+
// Inject the caller's file path so the manifest generator can resolve the module
|
|
102
|
+
// root from a live ModuleConfig object without re-importing or filesystem scanning.
|
|
103
|
+
//
|
|
104
|
+
// We use Error().stack to capture the call site (module.config.ts) rather than
|
|
105
|
+
// import.meta.url which would always point to this file (define.ts).
|
|
106
|
+
let sourcePath: string | undefined
|
|
107
|
+
try {
|
|
108
|
+
const stack = new Error().stack ?? ""
|
|
109
|
+
// Stack lines vary by runtime:
|
|
110
|
+
// Node/V8: " at Object.<anonymous> (file:///path/to/module.config.ts:5:20)"
|
|
111
|
+
// Bun: " at defineModule (/path/to/module.config.ts:2:23)"
|
|
112
|
+
// " at /path/to/module.config.ts:8" (top-level, no function name)
|
|
113
|
+
// We skip the first frame (this function) and find the first frame
|
|
114
|
+
// that is NOT define.ts — that is the module.config.ts caller.
|
|
115
|
+
const lines = stack.split("\n")
|
|
116
|
+
for (const line of lines.slice(1)) {
|
|
117
|
+
// Bare filesystem path (Bun), optionally wrapped in parens after a function name.
|
|
118
|
+
const bare = line.match(/\(?(\/[^():]+\.[jt]sx?):\d+(?::\d+)?\)?/)
|
|
119
|
+
// file:// URL (Node/V8).
|
|
120
|
+
const url = line.match(/\(?(file:\/\/[^)]+)\)?/)
|
|
121
|
+
|
|
122
|
+
const raw = url?.[1] ?? bare?.[1]
|
|
123
|
+
if (!raw) continue
|
|
124
|
+
if (raw.includes("define.ts")) continue
|
|
125
|
+
if (raw.startsWith("native:")) continue
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
sourcePath = raw.startsWith("file://") ? new URL(raw).pathname : raw
|
|
129
|
+
} catch {
|
|
130
|
+
sourcePath = raw.replace(/^file:\/\//, "").split("?")[0]
|
|
131
|
+
}
|
|
132
|
+
break
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// Stack introspection failed — proceed without _sourcePath.
|
|
136
|
+
// The manifest generator will warn and skip this module.
|
|
137
|
+
}
|
|
138
|
+
return sourcePath ? { ...config, _sourcePath: sourcePath } : config
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Declares typed arguments for the current script.
|
|
143
|
+
*
|
|
144
|
+
* This is a compile-time authoring macro. At runtime, Axon injects the script's
|
|
145
|
+
* arguments before the script runs; `defineArgs<T>()` gives those values a type
|
|
146
|
+
* and lets `axon prepare` extract the argument schema for generated declarations
|
|
147
|
+
* and CLI help.
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* const { issueId, title } = defineArgs<{
|
|
151
|
+
* issueId: string
|
|
152
|
+
* title?: string
|
|
153
|
+
* }>()
|
|
154
|
+
*
|
|
155
|
+
* await axon.request(`Triaging ${issueId}: ${title ?? "untitled"}`)
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @see https://axon.arclabs.it/docs/v2/api/scripts
|
|
159
|
+
* @see https://axon.arclabs.it/docs/v2/agent/src/scripts
|
|
160
|
+
*/
|
|
161
|
+
export function defineArgs<T extends Record<string, unknown>>(): T {
|
|
162
|
+
return (globalThis as any).args as T
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Declares typed props for a dynamic Vuedown prompt component.
|
|
167
|
+
*
|
|
168
|
+
* Use this in prompt/component `.vue` files when the template expects variables.
|
|
169
|
+
* `axon prepare` reads the generic type and generates typed `axon.prompt()`
|
|
170
|
+
* overloads, so callers must pass the props your prompt requires.
|
|
171
|
+
*
|
|
172
|
+
* ```ts
|
|
173
|
+
* const { domain, limit = 5 } = defineProps<{
|
|
174
|
+
* domain: string
|
|
175
|
+
* limit?: number
|
|
176
|
+
* }>()
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @see https://axon.arclabs.it/docs/v2/api/prompt
|
|
180
|
+
* @see https://axon.arclabs.it/docs/v2/concepts/vuedown
|
|
181
|
+
*/
|
|
182
|
+
export function defineProps<T extends Record<string, unknown>>(): T {
|
|
183
|
+
return (globalThis as any).__axon_skill_props__ as T
|
|
184
|
+
}
|
package/src/deploy.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deployment target for an agent bundle.
|
|
3
|
+
*
|
|
4
|
+
* - `"axon"` deploys to Axon-managed infrastructure.
|
|
5
|
+
* - `"self"` emits a portable bundle and Dockerfile for your own platform.
|
|
6
|
+
*
|
|
7
|
+
* @see https://axon.arclabs.it/docs/v2/api/config/deploy
|
|
8
|
+
*/
|
|
9
|
+
export type DeployTarget = "axon" | "self"
|
|
10
|
+
|
|
11
|
+
/** Autoscaling bounds for managed deployments. */
|
|
12
|
+
export type ScalingConfig = {
|
|
13
|
+
/** Minimum running instances. Defaults to `0`, allowing scale-to-zero. */
|
|
14
|
+
min?: number
|
|
15
|
+
/** Maximum running instances. Defaults to `3`. */
|
|
16
|
+
max?: number // 1
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Container runtime customization for deployed agents. */
|
|
20
|
+
export type RuntimeConfig = {
|
|
21
|
+
/**
|
|
22
|
+
* System packages to add to the container via apk (Alpine base).
|
|
23
|
+
* e.g. ["git", "ripgrep"]
|
|
24
|
+
*/
|
|
25
|
+
packages?: string[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Deployment configuration used by `axon agent deploy` and bundle commands.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* export default defineAgent({
|
|
34
|
+
* deploy: {
|
|
35
|
+
* target: "axon",
|
|
36
|
+
* runtime: { packages: ["git", "ripgrep"] },
|
|
37
|
+
* scaling: { min: 0, max: 2 },
|
|
38
|
+
* },
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see https://axon.arclabs.it/docs/v2/deploy
|
|
43
|
+
* @see https://axon.arclabs.it/docs/v2/api/config/deploy
|
|
44
|
+
*/
|
|
45
|
+
export type DeployConfig = {
|
|
46
|
+
/**
|
|
47
|
+
* "axon" — Axon-managed infra. Used by `axon agent deploy`.
|
|
48
|
+
* "self" — produce .agent/ bundle + Dockerfile only. Used by `axon agent bundle`.
|
|
49
|
+
* Default: "axon"
|
|
50
|
+
*/
|
|
51
|
+
target?: DeployTarget
|
|
52
|
+
runtime?: RuntimeConfig
|
|
53
|
+
scaling?: ScalingConfig
|
|
54
|
+
}
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { AxonEngineEvent } from "./session/events/engine"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Engine configuration — what the user puts in axon.config.ts. Either a
|
|
5
|
+
* constructed definition (`Axon({ model })`, `Mock()`) or a declarative
|
|
6
|
+
* reference (`{ provider: "axon", model: "claude-sonnet-4-6" }`). Both are
|
|
7
|
+
* resolved to a live driver by the runtime's Engine() manager — the one
|
|
8
|
+
* seam every boot path shares — via @arcforge/engines' resolveEngine().
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* export default defineAgent({
|
|
12
|
+
* engine: { provider: "axon", model: "claude-sonnet-4-6" },
|
|
13
|
+
* // or, for advanced wiring:
|
|
14
|
+
* // engine: Axon({ model: "claude-sonnet-4-6", url: "ws://localhost:8787" }),
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @see https://axon.arclabs.it/docs/v2/api/config/engine
|
|
19
|
+
*/
|
|
20
|
+
export type EngineConfig = AxonEngineDef | EngineRef
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The declarative engine reference — pure data. What the managed base
|
|
24
|
+
* workspace writes, what a model pick in the TUI serializes, and the form
|
|
25
|
+
* the product contract promises: swap the reference, zero code changes.
|
|
26
|
+
*/
|
|
27
|
+
export type EngineRef =
|
|
28
|
+
| { provider: "axon"; model?: string; optimize?: EngineAutoWeights; limit?: EngineAutoLimits }
|
|
29
|
+
| { provider: "openrouter"; model: string }
|
|
30
|
+
| { provider: "codex"; model?: string }
|
|
31
|
+
| { provider: "ollama"; model: string }
|
|
32
|
+
| { provider: "mock" }
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Axon({ model: "auto" }) weighting — resolved server-side against a
|
|
36
|
+
* curated model table (never shipped client-side, so curation updates
|
|
37
|
+
* reach every agent using "auto" with no client update). Unset axes
|
|
38
|
+
* contribute 0; weights are normalized to sum to 1 at resolution time, so
|
|
39
|
+
* `{ cost: 2, intelligence: 1 }` and `{ cost: 0.5, intelligence: 0.25 }`
|
|
40
|
+
* are equivalent.
|
|
41
|
+
*/
|
|
42
|
+
export type EngineAutoWeights = { cost?: number; intelligence?: number; speed?: number }
|
|
43
|
+
|
|
44
|
+
/** Hard ceilings applied before scoring — a candidate that fails a limit is never a possible pick, at any weight. */
|
|
45
|
+
export type EngineAutoLimits = { cost?: number }
|
|
46
|
+
|
|
47
|
+
/** An engine definition: named, constructed by the runtime with resources. */
|
|
48
|
+
export type AxonEngineDef = {
|
|
49
|
+
/** Provider name — telemetry and error messages ("axon", "ollama", "mock"). */
|
|
50
|
+
name: string
|
|
51
|
+
/**
|
|
52
|
+
* The model id the constructor was given, if any (e.g. Codex({ model:
|
|
53
|
+
* "gpt-5.6-terra" }) → "gpt-5.6-terra") — static, known at config-read
|
|
54
|
+
* time, no engine call needed. Every constructor in @arcforge/engines
|
|
55
|
+
* stamps this from its own `options.model`. "auto" (Axon({ model:
|
|
56
|
+
* "auto" })) is the one value that ISN'T the real model — it only
|
|
57
|
+
* resolves server-side, per call; a client showing it should say so.
|
|
58
|
+
*/
|
|
59
|
+
model?: string
|
|
60
|
+
create(res: EngineResources): AxonEngineDriver
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What the runtime hands an engine def at construction. */
|
|
64
|
+
export type EngineResources = {
|
|
65
|
+
/** Resolved agent environment — API keys, base URLs. No process.env reads in drivers. */
|
|
66
|
+
env: Record<string, string | undefined>
|
|
67
|
+
/**
|
|
68
|
+
* The runtime's cloud client — engines call what they need (vault
|
|
69
|
+
* connection tokens today; engine resolution and the Cognos connection
|
|
70
|
+
* when the Axon engine is ported). Provider tokens are minted
|
|
71
|
+
* server-side and cached until expiry, so per-request resolution is
|
|
72
|
+
* free and rotation never rebuilds a driver.
|
|
73
|
+
*/
|
|
74
|
+
cloud: EngineCloud
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Hand-written contract for the slice of AxonCloudClient engines consume —
|
|
79
|
+
* same pattern as EngineConnection in handle.ts (@arclabs/cloud depends on
|
|
80
|
+
* this package, so the type can't be imported; the real client satisfies
|
|
81
|
+
* this structurally). Grow it deliberately as engines need more surface.
|
|
82
|
+
*/
|
|
83
|
+
export type EngineCloud = {
|
|
84
|
+
user: {
|
|
85
|
+
vault: {
|
|
86
|
+
connections: {
|
|
87
|
+
openai: {
|
|
88
|
+
/** Narrow short-lived Codex token — refresh material never reaches a host. Throws when the account isn't connected. */
|
|
89
|
+
token(): Promise<CodexToken>
|
|
90
|
+
}
|
|
91
|
+
openrouter: {
|
|
92
|
+
/** The user's vaulted OpenRouter API key, minted as a narrow token. Throws when the account isn't connected. */
|
|
93
|
+
token(): Promise<{ accessToken: string; expiresAt: number }>
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
cloud: {
|
|
99
|
+
engine: {
|
|
100
|
+
/**
|
|
101
|
+
* Managed inference — the backend routes to the target model
|
|
102
|
+
* server-side, meters usage from the provider's own accounting,
|
|
103
|
+
* and debits the caller's ledger (markup is enforced where the
|
|
104
|
+
* money is, never client-side). The server assembles the
|
|
105
|
+
* terminal done event, so its meta (tokens, cost) is
|
|
106
|
+
* authoritative — it IS the ledger entry.
|
|
107
|
+
*/
|
|
108
|
+
stream(input: EngineCloudStream): AsyncGenerator<AxonEngineRawEvent>
|
|
109
|
+
/** Single-shot completion — drains the stream, returns the done response. */
|
|
110
|
+
request(input: EngineCloudStream): Promise<AxonEngineResponse>
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** One managed-inference call — the request plus how to route it. */
|
|
116
|
+
export type EngineCloudStream = {
|
|
117
|
+
request: AxonEngineRequest
|
|
118
|
+
/** Catalog model ID e.g. "claude-sonnet-4-6", or "auto" — server-resolved, see EngineAutoWeights. Omit for the account default. */
|
|
119
|
+
model?: string
|
|
120
|
+
/** Only meaningful when model is "auto" — ignored otherwise. */
|
|
121
|
+
optimize?: EngineAutoWeights
|
|
122
|
+
/** Only meaningful when model is "auto" — ignored otherwise. */
|
|
123
|
+
limit?: EngineAutoLimits
|
|
124
|
+
/** Engine deployment ID — pin a specific engine by registry ID (e.g. "helios"). */
|
|
125
|
+
id?: string
|
|
126
|
+
/** Direct WebSocket URL override — local dev / self-hosted harnesses only. */
|
|
127
|
+
url?: string
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What a Codex driver needs for one call — deliberately no refresh material. */
|
|
131
|
+
export type CodexToken = {
|
|
132
|
+
accessToken: string
|
|
133
|
+
/** chatgpt_account_id — sent as a header alongside the bearer token. */
|
|
134
|
+
accountId: string
|
|
135
|
+
/** unix ms — when this access token dies (the client caches until then). */
|
|
136
|
+
expiresAt: number
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The driver contract — what adapter authors implement. A dumb token pipe:
|
|
141
|
+
* messages in, raw deltas out, one terminal done. No AIR parsing, no bus,
|
|
142
|
+
* no blocks — the runtime's Engine() manager owns all of that, so the
|
|
143
|
+
* grammar is implemented exactly once.
|
|
144
|
+
*
|
|
145
|
+
* Errors: drivers THROW on failure (network, auth, provider error). There
|
|
146
|
+
* is no error event — a failed call is a runtime error the manager
|
|
147
|
+
* handles, never model-visible content.
|
|
148
|
+
*/
|
|
149
|
+
export type AxonEngineDriver = {
|
|
150
|
+
stream(req: AxonEngineRequest): AsyncGenerator<AxonEngineRawEvent>
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Raw wire events from a driver.
|
|
155
|
+
* text:delta — a chunk of model output (AIR-formatted text, unparsed)
|
|
156
|
+
* thinking:delta — a chunk of provider-native reasoning, when exposed
|
|
157
|
+
* done — terminal; carries the authoritative full response
|
|
158
|
+
*/
|
|
159
|
+
export type AxonEngineRawEvent =
|
|
160
|
+
| { type: "text:delta"; content: string }
|
|
161
|
+
| { type: "thinking:delta"; content: string }
|
|
162
|
+
| { type: "done"; response: AxonEngineResponse }
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The engine contract the kernel consumes: parsed block events
|
|
166
|
+
* (AxonEngineEvent). Implemented by the runtime's Engine() manager —
|
|
167
|
+
* driver deltas run through the one Air parser, telemetry is emitted
|
|
168
|
+
* around every call. Loop defs never see a raw driver.
|
|
169
|
+
*/
|
|
170
|
+
export type AxonEngine = {
|
|
171
|
+
/** Stream block events in real time, terminated by a single agent:done. */
|
|
172
|
+
stream: (req: AxonEngineRequest) => AsyncGenerator<AxonEngineEvent>
|
|
173
|
+
/** Single-shot completion. Same response shape as the stream's done event. */
|
|
174
|
+
request: (req: AxonEngineRequest) => Promise<AxonEngineResponse>
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export type AxonEngineMessage = {
|
|
178
|
+
role: "user" | "assistant" | "system"
|
|
179
|
+
content: string
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Request payload passed to an engine. */
|
|
183
|
+
export type AxonEngineRequest = {
|
|
184
|
+
messages: AxonEngineMessage[]
|
|
185
|
+
model?: string
|
|
186
|
+
maxTokens?: number
|
|
187
|
+
temperature?: number
|
|
188
|
+
/** First-class cancellation — engines must abort in-flight work when fired. */
|
|
189
|
+
signal?: AbortSignal
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The authoritative result of one engine call — returned by request() and
|
|
194
|
+
* carried on the stream's `done` event.
|
|
195
|
+
*/
|
|
196
|
+
export type AxonEngineResponse = {
|
|
197
|
+
/** Full raw model output — what the AIR parser consumes. */
|
|
198
|
+
text: string
|
|
199
|
+
/** Full reasoning trace, when the provider exposes it. */
|
|
200
|
+
thinking?: string
|
|
201
|
+
/**
|
|
202
|
+
* Why generation ended.
|
|
203
|
+
* "end" — model finished naturally
|
|
204
|
+
* "length" — truncated at maxTokens; the kernel treats trailing blocks as incomplete
|
|
205
|
+
* "abort" — cancelled via the request signal
|
|
206
|
+
*/
|
|
207
|
+
stopReason: "end" | "length" | "abort"
|
|
208
|
+
meta: AxonEngineMeta
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Per-call metadata — the billing and observability payload. */
|
|
212
|
+
export type AxonEngineMeta = {
|
|
213
|
+
provider: string
|
|
214
|
+
model: string
|
|
215
|
+
/** Provider-authoritative request identifier, when exposed. */
|
|
216
|
+
requestId?: string
|
|
217
|
+
/** Absent when the provider does not report usage — never fabricated as zero. */
|
|
218
|
+
tokens?: {
|
|
219
|
+
in: number
|
|
220
|
+
out: number
|
|
221
|
+
total: number
|
|
222
|
+
cachedIn?: number
|
|
223
|
+
reasoning?: number
|
|
224
|
+
}
|
|
225
|
+
/** USD. Absent means the engine cannot price the call (local inference) — not zero. */
|
|
226
|
+
cost?: { in: number; out: number; total: number }
|
|
227
|
+
durationMs: number
|
|
228
|
+
/** Time to the first provider delta, when observable. */
|
|
229
|
+
firstTokenMs?: number
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Stable failure vocabulary shared by drivers, the kernel log, and clients. */
|
|
233
|
+
export type AxonEngineFaultCode =
|
|
234
|
+
| "EMPTY_RESPONSE"
|
|
235
|
+
| "TRANSPORT"
|
|
236
|
+
| "RATE_LIMIT"
|
|
237
|
+
| "AUTH"
|
|
238
|
+
| "AUTH_NOT_CONNECTED"
|
|
239
|
+
| "QUOTA"
|
|
240
|
+
| "INVALID_REQUEST"
|
|
241
|
+
| "PROTOCOL"
|
|
242
|
+
| "ABORTED"
|
|
243
|
+
| "UNKNOWN"
|
|
244
|
+
|
|
245
|
+
/** Serializable engine failure — errors cross process/event boundaries as data. */
|
|
246
|
+
export type AxonEngineFault = {
|
|
247
|
+
code: AxonEngineFaultCode
|
|
248
|
+
message: string
|
|
249
|
+
retryable: boolean
|
|
250
|
+
provider: string
|
|
251
|
+
model?: string
|
|
252
|
+
status?: number
|
|
253
|
+
retryAfterMs?: number
|
|
254
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The AxonError wire contract.
|
|
3
|
+
*
|
|
4
|
+
* Lives in @arcforge/types because AxonError is a payload shape carried by
|
|
5
|
+
* failure events across every layer (runtime, cognet, test, bench) — it is a
|
|
6
|
+
* contract, not behavior. @axon/err owns the *runtime* (the err() constructor,
|
|
7
|
+
* stack capture, rendering, bus emission) and imports these types from here to
|
|
8
|
+
* implement them. Types owns the shape; err owns the behavior.
|
|
9
|
+
*
|
|
10
|
+
* Runtime-derived types stay in @axon/err: AxonErrorCode / AxonErrorMap are
|
|
11
|
+
* `keyof`/`typeof` over the concrete error map and cannot exist without it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** One line of captured source around a stack frame. */
|
|
15
|
+
export type AxonSourceLine = {
|
|
16
|
+
lineNumber: number
|
|
17
|
+
text: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A single structured stack frame, captured at error construction time. */
|
|
21
|
+
export type AxonStackFrame = {
|
|
22
|
+
functionName: string | null
|
|
23
|
+
fileName: string | null
|
|
24
|
+
lineNumber: number | null
|
|
25
|
+
columnNumber: number | null
|
|
26
|
+
/** Lines of source around lineNumber, captured at construction time. Null when the file wasn't reachable on disk. */
|
|
27
|
+
source: AxonSourceLine[] | null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Which subsystem an error originates from. */
|
|
31
|
+
export type AxonErrorSource =
|
|
32
|
+
| "runtime" | "manifest" | "capsule" | "server" | "thread" | "cli"
|
|
33
|
+
| "cloud" | "kernel" | "cognet" | "tui" | "bench"
|
|
34
|
+
|
|
35
|
+
/** Lifecycle impact of an error. */
|
|
36
|
+
export type AxonErrorSeverity = "fatal" | "recovered" | "degraded"
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Context every error code can carry — free-form per code. Deliberately
|
|
40
|
+
* Record<string, unknown> rather than one shape per code.
|
|
41
|
+
*/
|
|
42
|
+
export type AxonErrorContext = Record<string, unknown>
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The plain-data shape AxonError.toJSON() produces — what lands in the session
|
|
46
|
+
* JSONL and on the bus. AxonError IS the wire shape; there is no separate
|
|
47
|
+
* compact projection.
|
|
48
|
+
*/
|
|
49
|
+
export type AxonErrorJSON = {
|
|
50
|
+
isAxonError: true
|
|
51
|
+
code: string
|
|
52
|
+
title: string
|
|
53
|
+
description: string
|
|
54
|
+
message: string
|
|
55
|
+
source: AxonErrorSource
|
|
56
|
+
severity: AxonErrorSeverity
|
|
57
|
+
context?: AxonErrorContext
|
|
58
|
+
frames: AxonStackFrame[]
|
|
59
|
+
stack?: string
|
|
60
|
+
/**
|
|
61
|
+
* best-effort — a real Error's own message/stack, or String(cause) for
|
|
62
|
+
* anything else. `frame` is the cause's OWN first non-framework stack
|
|
63
|
+
* frame — where the user's code actually threw.
|
|
64
|
+
*/
|
|
65
|
+
cause?: { message: string; stack?: string; frame?: AxonStackFrame | null } | string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The rich, in-process error object. Every AxonError is a real Error plus the
|
|
70
|
+
* full renderable identity: code, title, description, severity, and a
|
|
71
|
+
* structured stack captured at construction time.
|
|
72
|
+
*/
|
|
73
|
+
export interface AxonError extends Error {
|
|
74
|
+
code: string
|
|
75
|
+
title: string
|
|
76
|
+
description: string
|
|
77
|
+
source: AxonErrorSource
|
|
78
|
+
severity: AxonErrorSeverity
|
|
79
|
+
context: AxonErrorContext | undefined
|
|
80
|
+
frames: AxonStackFrame[]
|
|
81
|
+
/** native Error.cause — walk this to render the full chain */
|
|
82
|
+
cause?: unknown
|
|
83
|
+
isAxonError: true
|
|
84
|
+
/** The full Rust-style report: title, description, message, every frame, cause chain. */
|
|
85
|
+
render(): string
|
|
86
|
+
/** Plain-data projection — called automatically by JSON.stringify. */
|
|
87
|
+
toJSON(): AxonErrorJSON
|
|
88
|
+
}
|