@arcforge/types 2.0.86 → 2.0.88

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcforge/types",
3
- "version": "2.0.86",
3
+ "version": "2.0.88",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./src/index.ts",
package/src/define.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { AxonConfig } from "./config"
2
2
  import type { BenchConfig, BenchDefinition } from "./bench"
3
- import type { ModuleConfig, ModuleOptionSchema } from "./module"
3
+ import type { ModuleConfig, ModuleEmitMap, ModuleOptionSchema } from "./module"
4
4
  import type { AxonPlugin } from "./plugin"
5
5
  import type { AxonHandle } from "./handle"
6
6
 
@@ -99,45 +99,26 @@ export function defineBench(config: BenchConfig): BenchDefinition {
99
99
  return { _kind: "bench", config }
100
100
  }
101
101
 
102
- export function defineModule<O extends Record<string, ModuleOptionSchema>>(config: ModuleConfig<O>): ModuleConfig<O> {
103
- // Inject the caller's file path so the manifest generator can resolve the module
104
- // root from a live ModuleConfig object without re-importing or filesystem scanning.
105
- //
106
- // We use Error().stack to capture the call site (module.config.ts) rather than
107
- // import.meta.url which would always point to this file (define.ts).
108
- let sourcePath: string | undefined
109
- try {
110
- const stack = new Error().stack ?? ""
111
- // Stack lines vary by runtime:
112
- // Node/V8: " at Object.<anonymous> (file:///path/to/module.config.ts:5:20)"
113
- // Bun: " at defineModule (/path/to/module.config.ts:2:23)"
114
- // " at /path/to/module.config.ts:8" (top-level, no function name)
115
- // We skip the first frame (this function) and find the first frame
116
- // that is NOT define.ts — that is the module.config.ts caller.
117
- const lines = stack.split("\n")
118
- for (const line of lines.slice(1)) {
119
- // Bare filesystem path (Bun), optionally wrapped in parens after a function name.
120
- const bare = line.match(/\(?(\/[^():]+\.[jt]sx?):\d+(?::\d+)?\)?/)
121
- // file:// URL (Node/V8).
122
- const url = line.match(/\(?(file:\/\/[^)]+)\)?/)
123
-
124
- const raw = url?.[1] ?? bare?.[1]
125
- if (!raw) continue
126
- if (raw.includes("define.ts")) continue
127
- if (raw.startsWith("native:")) continue
128
-
129
- try {
130
- sourcePath = raw.startsWith("file://") ? new URL(raw).pathname : raw
131
- } catch {
132
- sourcePath = raw.replace(/^file:\/\//, "").split("?")[0]
133
- }
134
- break
135
- }
136
- } catch {
137
- // Stack introspection failed — proceed without _sourcePath.
138
- // The manifest generator will warn and skip this module.
139
- }
140
- return sourcePath ? { ...config, _sourcePath: sourcePath } : config
102
+ /**
103
+ * Defines a reusable Axon module. The default export of `module.config.ts`.
104
+ *
105
+ * Pure identity — it returns the config unchanged. A module's file path (its
106
+ * `configPath`) is NOT recovered here: the blueprint scanner resolves it
107
+ * statically from the `import` statements in the agent config that declares
108
+ * the module, so there is no runtime stack to walk and no bundler-renamed
109
+ * filename to match. `defineModule` exists purely for editor inference and to
110
+ * mark intent; it does no work.
111
+ *
112
+ * The config's executable half (`setup`, and any hooks it registers) is run
113
+ * at agent boot by the core runtime, which imports the module by its resolved
114
+ * `configPath`. The static half (`env`, `options`, `emits`, `policy`) is read
115
+ * by the scanner from the file's AST.
116
+ */
117
+ export function defineModule<
118
+ O extends Record<string, ModuleOptionSchema>,
119
+ E extends ModuleEmitMap,
120
+ >(config: ModuleConfig<O, E>): ModuleConfig<O, E> {
121
+ return config
141
122
  }
142
123
 
143
124
  /**
@@ -195,17 +176,21 @@ export function defineProps<T extends Record<string, unknown>>(): T {
195
176
  *
196
177
  * ```ts
197
178
  * export default defineAxonPlugin(async (axon) => {
198
- * axon.hooks.on("telegram:message", async ({ text, reply }) => {
179
+ * axon.hooks.hook("telegram:message", async ({ text, reply }) => {
199
180
  * const result = await axon.request({ prompt: text })
200
181
  * await reply(result.text)
201
182
  * })
202
183
  * })
203
184
  * ```
204
185
  *
186
+ * The `axon` handle is passed in by the runtime that boots the plugin — never
187
+ * read from a global — so a plugin is always bound to its own Axon() instance
188
+ * even when a host runs several at once.
189
+ *
205
190
  * @see https://axon.arclabs.it/docs/v2/agent/server
206
191
  */
207
192
  export function defineAxonPlugin(fn: (axon: AxonHandle) => void | Promise<void>): AxonPlugin {
208
- return { name: fn.name || "plugin", fn: () => fn((globalThis as any).axon as AxonHandle) }
193
+ return { name: fn.name || "plugin", fn }
209
194
  }
210
195
 
211
196
  /** Handle returned by the test-runtime spawner `Axon()`. */
package/src/handle.ts CHANGED
@@ -2,7 +2,7 @@ import type { AxonEntry } from "./session"
2
2
  import type { AxonScript } from "./scripts"
3
3
  import type { AxonPrompt } from "./prompts"
4
4
  import type { AxonPartialBlueprint } from "./blueprint"
5
- import type { AxonHookName, AxonHooks } from "./hooks"
5
+ import type { AxonHookName, AxonHooks, AxonModuleEvents } from "./hooks"
6
6
  import type { AxonAmbient } from "./session/events/activity"
7
7
 
8
8
  /**
@@ -77,13 +77,25 @@ export type EngineConnection = {
77
77
 
78
78
  // ── Lifecycle hooks surface ────────────────────────────────────────────────────
79
79
 
80
+ /** Handler for a module domain event — receives the module's declared payload. */
81
+ export type AxonModuleEventHandler<N extends keyof AxonModuleEvents> =
82
+ (payload: AxonModuleEvents[N]) => void | Promise<void>
83
+
80
84
  /**
81
85
  * The hooks surface exposed on AxonHandle — register-only. Firing (`callHook`)
82
86
  * is runtime-internal; agent code and plugins only ever register.
87
+ *
88
+ * Two families, one `hook()`:
89
+ * - Runtime LIFECYCLE hooks (AxonHooks) — a fixed, closed set, function-typed.
90
+ * - Module domain EVENTS (AxonModuleEvents) — open, augmented per-agent by
91
+ * typegen from installed modules' `emits`, so `hook("discord:message.received",
92
+ * ({ content }) => ...)` is fully typed with no imports.
83
93
  */
84
94
  export type AxonHooksSurface = {
85
95
  hook<N extends AxonHookName>(name: N, fn: AxonHooks[N]): () => void
96
+ hook<N extends keyof AxonModuleEvents>(name: N, fn: AxonModuleEventHandler<N>): () => void
86
97
  removeHook<N extends AxonHookName>(name: N, fn: AxonHooks[N]): void
98
+ removeHook<N extends keyof AxonModuleEvents>(name: N, fn: AxonModuleEventHandler<N>): void
87
99
  }
88
100
 
89
101
  // ── AxonHandle ────────────────────────────────────────────────────────────────
@@ -138,6 +150,7 @@ export type AxonHandle = {
138
150
  session: { id: string }
139
151
  }
140
152
 
153
+
141
154
  /**
142
155
  * The honest Axon surface inside a capsule tool. A tool cannot re-enter the
143
156
  * Axon instance currently waiting for it, so request() is host-mediated and
package/src/hooks.ts CHANGED
@@ -29,3 +29,23 @@ export type AxonHooks = {
29
29
  }
30
30
 
31
31
  export type AxonHookName = keyof AxonHooks
32
+
33
+ /**
34
+ * Module event payloads — an OPEN interface, augmented per-agent by typegen.
35
+ *
36
+ * A module declares `emits: { "discord:message.received": {} as { ... } }`;
37
+ * `axon prepare` merges each installed module's emit payloads into this
38
+ * interface (declaration merging, in `.agent/axon.d.ts`). That makes
39
+ * `axon.hooks.hook("discord:message.received", ({ content }) => ...)` fully
40
+ * typed in userland — the event name autocompletes and the payload is inferred
41
+ * — with no imports and no generics.
42
+ *
43
+ * Distinct from AxonHooks: those are the fixed runtime lifecycle call points
44
+ * (a closed set, function-typed). Module events are domain events with a
45
+ * payload the handler receives; the name space is open because it depends on
46
+ * which modules the agent installed.
47
+ */
48
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
49
+ export interface AxonModuleEvents {}
50
+
51
+ export type AxonModuleEventName = keyof AxonModuleEvents
package/src/module.ts CHANGED
@@ -21,6 +21,13 @@ export type AxonModule = {
21
21
  version?: string
22
22
  /** Absolute path to the module root. */
23
23
  root: string
24
+ /**
25
+ * Absolute path to the module's `module.config.ts`. The core runtime
26
+ * imports this at boot to run the module's `setup()`. Resolved statically
27
+ * by the scanner from the agent config's import of this module — never
28
+ * from a runtime stack.
29
+ */
30
+ configPath: string
24
31
  /** Whether module files are auto-discovered and merged. Default true. */
25
32
  automerge: boolean
26
33
  /** Declared env variables and their requirements. */
@@ -51,7 +58,10 @@ export type OptionTypeMap = {
51
58
  * Module authoring configuration — the object passed to `defineModule()`
52
59
  * in `module.config.ts`.
53
60
  */
54
- export type ModuleConfig<O extends Record<string, ModuleOptionSchema> = Record<string, ModuleOptionSchema>> = {
61
+ export type ModuleConfig<
62
+ O extends Record<string, ModuleOptionSchema> = Record<string, ModuleOptionSchema>,
63
+ E extends ModuleEmitMap = ModuleEmitMap,
64
+ > = {
55
65
  /**
56
66
  * Identity — name, version, description — lives in package.json, not
57
67
  * here. It is what the registry publishes, what `bun install` resolves,
@@ -63,27 +73,30 @@ export type ModuleConfig<O extends Record<string, ModuleOptionSchema> = Record<s
63
73
  */
64
74
  /** Environment variables this module expects the host agent to provide. */
65
75
  env?: Record<string, { required: boolean; description?: string }>
66
- /** Event payloads this module may emit. */
67
- emits?: ModuleEmitMap
76
+ /**
77
+ * Event payloads this module may emit. Declared inline as `{} as { ... }`;
78
+ * the payload types are PRESERVED through the `E` generic (not widened to
79
+ * unknown), so typegen can recover them via `typeof Module.emits` and give
80
+ * subscribers typed payloads at `axon.hooks.hook("event", ...)`.
81
+ */
82
+ emits?: E
68
83
  /** Advisory sandbox permissions this module needs. */
69
84
  policy?: ModulePolicyNeeds
70
85
  /** User-configurable options accepted under `modules.<name>` in `axon.config.ts`. */
71
86
  options?: O
72
- /** Lifecycle or custom hook handlers registered when the module loads. */
73
- hooks?: Record<string, (...args: any[]) => void | Promise<void>>
74
- /** Boot-time setup callback for dynamic registration. */
87
+ /**
88
+ * Boot-time setup, run by the core runtime when the agent boots. Receives
89
+ * a narrow `axon` handle (hooks, env, policy) and the module's validated
90
+ * options. This is the ONE place a module registers behaviour: lifecycle
91
+ * hooks go through `ctx.axon.hook(...)`, not a separate declarative field.
92
+ * Runs again on hot-reload, after the previous setup's disposers.
93
+ */
75
94
  setup?: (ctx: ModuleSetupCtx<InferOptions<O>>) => void | Promise<void>
76
95
  /**
77
96
  * Sub-modules this module depends on. Flattened and prepended before the agent's
78
97
  * own module list at manifest generation time. First declaration wins on collision.
79
98
  */
80
99
  modules?: ModuleEntry[]
81
- /**
82
- * Injected by `defineModule()` at call time — absolute path of the
83
- * module.config.ts file. Never set manually.
84
- * @internal
85
- */
86
- _sourcePath?: string
87
100
  }
88
101
 
89
102
  /** Event payloads a module may emit. */
@@ -164,6 +177,14 @@ export type ModuleSetupAxon = {
164
177
  hook(name: string, handler: (...args: any[]) => void | Promise<void>): void
165
178
  /** Emit a hook — fires all registered handlers for this name. */
166
179
  callHook(name: string, ...args: any[]): Promise<void>
180
+ /**
181
+ * Register teardown for this module's live resources (connections, timers,
182
+ * sockets). Disposers run in REVERSE module order on shutdown, and on
183
+ * hot-reload before setup re-runs — so a reloaded agent is identical to a
184
+ * freshly-booted one. Call it once per resource; they run in reverse
185
+ * registration order within the module too.
186
+ */
187
+ onDispose(fn: () => void | Promise<void>): void
167
188
  env: {
168
189
  get(key: string): string | undefined
169
190
  require(key: string): string
package/src/plugin.ts CHANGED
@@ -1,9 +1,15 @@
1
+ import type { AxonHandle } from "./handle"
1
2
 
2
3
  /**
3
4
  * Boot-time plugin. Runs once, after middleware and before routes are
4
5
  * mounted. A throwing plugin aborts boot — plugin failure is always fatal.
6
+ *
7
+ * `fn` receives THIS runtime's `axon` handle as an argument — not the injected
8
+ * global. A process may host several Axon() instances at once (the TUI runs
9
+ * one per conversation), and a bare `globalThis.axon` is whichever booted
10
+ * last. Passing the handle binds each plugin to its own runtime.
5
11
  */
6
12
  export type AxonPlugin = {
7
13
  name: string
8
- fn: () => void | Promise<void>
14
+ fn: (axon: AxonHandle) => void | Promise<void>
9
15
  }
@@ -53,6 +53,17 @@ export type AxonRuntimeEvent = AxonLogEvents<"axon"> & {
53
53
  "axon:uninstall:complete": { name: string; durationMs: number }
54
54
  "axon:uninstall:failed": { name: string; error: AxonError }
55
55
 
56
+ // ── Module setup/teardown (boot-time defineModule() setup execution) ─────
57
+ // The determinism ledger: one start/complete (or failed) per module,
58
+ // in blueprint order, carrying the config content hash so two boots of
59
+ // the same blueprint are provably the same wiring.
60
+ "module:setup:start": { name: string; configHash: string; options: Record<string, unknown> }
61
+ "module:setup:complete": { name: string; durationMs: number }
62
+ "module:setup:failed": { name: string; error: AxonError }
63
+ "module:dispose:start": { name: string }
64
+ "module:dispose:complete": { name: string; durationMs: number }
65
+ "module:dispose:failed": { name: string; error: AxonError }
66
+
56
67
  // ── Session continuity ───────────────────────────────────────────────────
57
68
  "axon:session:start": {}
58
69
  "axon:session:restored": {}