@framers/agentos 0.1.157 → 0.1.159

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.
Files changed (48) hide show
  1. package/README.md +51 -10
  2. package/dist/api/agent.d.ts +24 -1
  3. package/dist/api/agent.d.ts.map +1 -1
  4. package/dist/api/agent.js +80 -1
  5. package/dist/api/agent.js.map +1 -1
  6. package/dist/api/generateText.d.ts +79 -0
  7. package/dist/api/generateText.d.ts.map +1 -1
  8. package/dist/api/generateText.js +110 -3
  9. package/dist/api/generateText.js.map +1 -1
  10. package/dist/api/index.d.ts +7 -0
  11. package/dist/api/index.d.ts.map +1 -1
  12. package/dist/api/index.js +4 -0
  13. package/dist/api/index.js.map +1 -1
  14. package/dist/api/streamText.d.ts.map +1 -1
  15. package/dist/api/streamText.js +119 -4
  16. package/dist/api/streamText.js.map +1 -1
  17. package/dist/memory/AgentMemory.d.ts.map +1 -1
  18. package/dist/memory/AgentMemory.js +1 -0
  19. package/dist/memory/AgentMemory.js.map +1 -1
  20. package/dist/memory/core/types.d.ts +1 -1
  21. package/dist/memory/core/types.d.ts.map +1 -1
  22. package/dist/query-router/QueryRouter.d.ts.map +1 -1
  23. package/dist/query-router/QueryRouter.js.map +1 -1
  24. package/dist/query-router/types.d.ts +12 -0
  25. package/dist/query-router/types.d.ts.map +1 -1
  26. package/dist/query-router/types.js +1 -0
  27. package/dist/query-router/types.js.map +1 -1
  28. package/dist/rag/citation/CitationVerifier.d.ts +29 -0
  29. package/dist/rag/citation/CitationVerifier.d.ts.map +1 -0
  30. package/dist/rag/citation/CitationVerifier.js +116 -0
  31. package/dist/rag/citation/CitationVerifier.js.map +1 -0
  32. package/dist/rag/citation/cosine.d.ts +10 -0
  33. package/dist/rag/citation/cosine.d.ts.map +1 -0
  34. package/dist/rag/citation/cosine.js +21 -0
  35. package/dist/rag/citation/cosine.js.map +1 -0
  36. package/dist/rag/citation/index.d.ts +8 -0
  37. package/dist/rag/citation/index.d.ts.map +1 -0
  38. package/dist/rag/citation/index.js +7 -0
  39. package/dist/rag/citation/index.js.map +1 -0
  40. package/dist/rag/citation/types.d.ts +68 -0
  41. package/dist/rag/citation/types.d.ts.map +1 -0
  42. package/dist/rag/citation/types.js +9 -0
  43. package/dist/rag/citation/types.js.map +1 -0
  44. package/dist/rag/index.d.ts +2 -0
  45. package/dist/rag/index.d.ts.map +1 -1
  46. package/dist/rag/index.js +2 -0
  47. package/dist/rag/index.js.map +1 -1
  48. package/package.json +1 -1
package/README.md CHANGED
@@ -23,9 +23,9 @@
23
23
 
24
24
  ## Overview
25
25
 
26
- AgentOS is an open-source TypeScript runtime for building autonomous AI agents. Agents have HEXACO personality traits that shape how they think, cognitive memory with Ebbinghaus decay that determines what they remember, and emergent capabilities that let them forge new tools at runtime. The result is agents that adapt and improve without retraining.
26
+ AgentOS is an open-source TypeScript runtime for building autonomous AI agents. Agents have personality traits that shape how they think, cognitive memory that determines what they remember, and emergent capabilities that let them forge new tools at runtime. The result is agents that adapt and improve without retraining.
27
27
 
28
- Unlike frameworks that focus purely on LLM orchestration, AgentOS treats each agent as a cognitive entity with its own personality, memory lifecycle, and behavioral adaptation loop. This makes it particularly suited for long-running agents, multi-agent teams, and applications where agent identity and consistency matter.
28
+ Unlike frameworks that focus purely on LLM orchestration, AgentOS treats each agent as a **Generalized Mind Instance** (GMI) — a cognitive entity with its own identity, personality, memory lifecycle, and behavioral adaptation loop. This makes it particularly suited for long-running agents, multi-agent teams, and applications where agent consistency matters.
29
29
 
30
30
  **What makes it different:**
31
31
 
@@ -369,6 +369,47 @@ const secureBot = agent({
369
369
  - **Grounding Guard** -- RAG-source claim verification and hallucination detection
370
370
  - **Content Policy Rewriter** -- 8 categories, LLM rewrite/block, 4 presets
371
371
 
372
+ ### 10. Citation Verification
373
+
374
+ Verify claims in agent responses against sources using cosine similarity:
375
+
376
+ ```typescript
377
+ import { CitationVerifier } from '@framers/agentos';
378
+
379
+ const verifier = new CitationVerifier({
380
+ embedFn: async (texts) => embeddingManager.embedBatch(texts),
381
+ });
382
+
383
+ const result = await verifier.verify(
384
+ "Tokyo has a population of 14 million. It is the capital of Japan.",
385
+ [
386
+ { content: "Tokyo proper has a population of approximately 14 million.", url: "https://example.com" },
387
+ { content: "Tokyo is the capital and largest city of Japan.", url: "https://example.com/japan" },
388
+ ]
389
+ );
390
+
391
+ console.log(result.summary);
392
+ // "2/2 claims verified (100%)"
393
+ console.log(result.claims[0]);
394
+ // { text: "Tokyo has a population of 14 million.", verdict: "supported", confidence: 0.87 }
395
+ ```
396
+
397
+ On-demand tool for agents:
398
+
399
+ ```typescript
400
+ // Agent can call verify_citations to check its own output
401
+ verify_citations({
402
+ text: "The speed of light is 300,000 km/s.",
403
+ webFallback: true, // search web if sources don't match
404
+ })
405
+ ```
406
+
407
+ Automatic during deep research — set `verifyCitations: true` in config:
408
+
409
+ ```json
410
+ { "queryRouter": { "verifyCitations": true } }
411
+ ```
412
+
372
413
  ### Default Models Per Provider
373
414
 
374
415
  When you specify `provider` without `model`, these defaults are used:
@@ -427,14 +468,14 @@ Personality traits are set at agent creation and can be adapted within bounded l
427
468
 
428
469
  | Mechanism | Effect | Citation |
429
470
  |-----------|--------|----------|
430
- | Reconsolidation | Retrieved memories drift toward current mood | Nader, Schiller & LeDoux (2000). *Nature*, 406, 722-726 |
431
- | Retrieval-Induced Forgetting | Retrieving one memory suppresses similar competitors | Anderson, Bjork & Bjork (1994). *JEP: Learning*, 20, 1063-1087 |
432
- | Involuntary Recall | Random surfacing of old high-vividness memories | Berntsen (1996). *Applied Cognitive Psychology*, 10, 435-454 |
433
- | Metacognitive FOK | Feeling-of-knowing scoring for tip-of-tongue states | Hart (1965). *JEPG*, 56, 208-216 |
434
- | Temporal Gist Extraction | Old traces compressed to core assertions | Reyna & Brainerd (1995). *Developmental Review*, 15, 3-47 |
435
- | Schema Encoding | Novel input boosted, schema-matching encoded efficiently | Bartlett (1932). *Remembering*. Cambridge University Press |
436
- | Source Confidence Decay | Agent inferences decay faster than observations | Johnson, Hashtroudi & Lindsay (1993). *Psych. Bulletin*, 114, 3-28 |
437
- | Emotion Regulation | Reappraisal + suppression during consolidation | Gross (1998). *Review of General Psychology*, 2, 271-299 |
471
+ | Reconsolidation | Retrieved memories drift toward current mood | [Nader, Schiller & LeDoux (2000)](https://doi.org/10.1038/35021052). *Nature*, 406, 722-726 |
472
+ | Retrieval-Induced Forgetting | Retrieving one memory suppresses similar competitors | [Anderson, Bjork & Bjork (1994)](https://doi.org/10.1037/0278-7393.20.5.1063). *JEP: Learning*, 20, 1063-1087 |
473
+ | Involuntary Recall | Random surfacing of old high-vividness memories | [Berntsen (1996)](https://doi.org/10.1002/(SICI)1099-0720(199610)10:5%3C435::AID-ACP395%3E3.0.CO;2-8). *Applied Cognitive Psychology*, 10, 435-454 |
474
+ | Metacognitive FOK | Feeling-of-knowing scoring for tip-of-tongue states | [Hart (1965)](https://doi.org/10.1037/h0022263). *JEPG*, 56, 208-216 |
475
+ | Temporal Gist Extraction | Old traces compressed to core assertions | [Reyna & Brainerd (1995)](https://doi.org/10.1006/drev.1995.1002). *Developmental Review*, 15, 3-47 |
476
+ | Schema Encoding | Novel input boosted, schema-matching encoded efficiently | [Bartlett (1932)](https://doi.org/10.1017/CBO9780511759185). *Remembering*. Cambridge University Press |
477
+ | Source Confidence Decay | Agent inferences decay faster than observations | [Johnson, Hashtroudi & Lindsay (1993)](https://doi.org/10.1037/0033-2909.114.1.3). *Psych. Bulletin*, 114, 3-28 |
478
+ | Emotion Regulation | Reappraisal + suppression during consolidation | [Gross (1998)](https://doi.org/10.1037/1089-2680.2.3.271). *Review of General Psychology*, 2, 271-299 |
438
479
 
439
480
  **HEXACO Personality Modulation** -- each mechanism's intensity is governed by one or more HEXACO traits:
440
481
 
@@ -8,8 +8,10 @@
8
8
  * are not actively enforced in this lightweight layer — use the full AgentOS
9
9
  * runtime (`AgentOSOrchestrator`) or `agency()` for guardrail enforcement.
10
10
  */
11
- import { type FallbackProviderEntry, type GenerateTextOptions, type GenerateTextResult, type Message } from './generateText.js';
11
+ import { type FallbackProviderEntry, type GenerateTextOptions, type GenerateTextResult, type GenerationHookContext, type GenerationHookResult, type Message, type ToolCallHookInfo } from './generateText.js';
12
12
  import { type StreamTextResult } from './streamText.js';
13
+ import type { IModelRouter } from '../core/llm/routing/IModelRouter.js';
14
+ import type { SkillEntry } from '../skills/types.js';
13
15
  import { type AgentOSUsageAggregate, type AgentOSUsageLedgerOptions } from './runtime/usageLedger.js';
14
16
  import type { BaseAgentConfig } from './types.js';
15
17
  import { type AgentExportConfig } from './agentExport.js';
@@ -51,6 +53,27 @@ export interface AgentOptions extends BaseAgentConfig {
51
53
  * @param fallbackProvider - The provider identifier being tried next.
52
54
  */
53
55
  onFallback?: (error: Error, fallbackProvider: string) => void;
56
+ /** Model router for intelligent provider selection per-call. */
57
+ router?: IModelRouter;
58
+ /** Pre-generation hook, called before each LLM step. */
59
+ onBeforeGeneration?: (context: GenerationHookContext) => Promise<GenerationHookContext | void>;
60
+ /** Post-generation hook, called after each LLM step. */
61
+ onAfterGeneration?: (result: GenerationHookResult) => Promise<GenerationHookResult | void>;
62
+ /** Pre-tool-execution hook. */
63
+ onBeforeToolExecution?: (info: ToolCallHookInfo) => Promise<ToolCallHookInfo | null>;
64
+ /**
65
+ * Optional memory provider. When provided:
66
+ * - `session.send()`/`stream()` calls `memory.getContext()` before each turn
67
+ * and prepends results to the system prompt.
68
+ * - `session.send()`/`stream()` calls `memory.observe()` after each turn
69
+ * to encode the exchange into long-term memory.
70
+ */
71
+ memoryProvider?: any;
72
+ /**
73
+ * Optional skill entries to inject into the system prompt.
74
+ * Skill content is appended to the system prompt as markdown sections.
75
+ */
76
+ skills?: SkillEntry[];
54
77
  }
55
78
  /**
56
79
  * A named conversation session returned by `Agent.session()`.
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/api/agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAE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;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAAC;IACvC,+EAA+E;IAC/E,QAAQ,IAAI,OAAO,EAAE,CAAC;IACtB,wFAAwF;IACxF,KAAK,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAAC;IACxC,uDAAuD;IACvD,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IAC3F;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GAAG,gBAAgB,CAAC;IAC9E;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC;IACnC,8EAA8E;IAC9E,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC1D,+DAA+D;IAC/D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC,UAAU,CAAC,GAAG,iBAAiB,CAAC;IACpE;;;;OAIG;IACH,UAAU,CAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC;CAC9D;AA4CD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,KAAK,CAuP/C"}
package/dist/api/agent.js CHANGED
@@ -16,6 +16,8 @@ function mergeUsageLedgerOptions(...parts) {
16
16
  const merged = Object.assign({}, ...parts.filter(Boolean));
17
17
  return Object.keys(merged).length > 0 ? merged : undefined;
18
18
  }
19
+ /** Timeout for memory operations to prevent blocking generation. */
20
+ const MEMORY_TIMEOUT_MS = 5000;
19
21
  function buildSystemPrompt(opts) {
20
22
  const sections = [];
21
23
  if (opts.instructions?.trim()) {
@@ -32,6 +34,14 @@ function buildSystemPrompt(opts) {
32
34
  sections.push(`Behavior traits: ${traits.join(', ')}.`);
33
35
  }
34
36
  }
37
+ // Append skill content as markdown sections
38
+ if (opts.skills?.length) {
39
+ for (const entry of opts.skills) {
40
+ if (entry.skill.content?.trim()) {
41
+ sections.push(entry.skill.content.trim());
42
+ }
43
+ }
44
+ }
35
45
  return sections.length > 0 ? sections.join('\n\n') : undefined;
36
46
  }
37
47
  /**
@@ -88,6 +98,10 @@ export function agent(opts) {
88
98
  usageLedger: effectiveLedger,
89
99
  fallbackProviders: opts.fallbackProviders,
90
100
  onFallback: opts.onFallback,
101
+ router: opts.router,
102
+ onBeforeGeneration: opts.onBeforeGeneration,
103
+ onAfterGeneration: opts.onAfterGeneration,
104
+ onBeforeToolExecution: opts.onBeforeToolExecution,
91
105
  };
92
106
  const agentInstance = {
93
107
  async generate(prompt, extra) {
@@ -118,11 +132,33 @@ export function agent(opts) {
118
132
  return {
119
133
  id: sessionId,
120
134
  async send(text) {
135
+ // Memory recall before generation
136
+ let memorySystemMsg;
137
+ if (opts.memoryProvider?.getContext) {
138
+ try {
139
+ const ctx = await Promise.race([
140
+ opts.memoryProvider.getContext(text, { tokenBudget: 2000 }),
141
+ new Promise((resolve) => setTimeout(() => resolve(null), MEMORY_TIMEOUT_MS)),
142
+ ]);
143
+ if (ctx?.contextText) {
144
+ memorySystemMsg = ctx.contextText;
145
+ }
146
+ }
147
+ catch {
148
+ // Memory recall failure is non-fatal
149
+ }
150
+ }
151
+ // Prepend memory context to system prompt
152
+ let system = baseOpts.system;
153
+ if (memorySystemMsg) {
154
+ system = [memorySystemMsg, system].filter(Boolean).join('\n\n') || undefined;
155
+ }
121
156
  const requestMessages = useMemory
122
157
  ? [...history, { role: 'user', content: text }]
123
158
  : [{ role: 'user', content: text }];
124
159
  const result = await generateText({
125
160
  ...baseOpts,
161
+ system,
126
162
  messages: requestMessages,
127
163
  usageLedger: mergeUsageLedgerOptions(baseOpts.usageLedger, {
128
164
  sessionId,
@@ -133,14 +169,50 @@ export function agent(opts) {
133
169
  history.push({ role: 'user', content: text });
134
170
  history.push({ role: 'assistant', content: result.text });
135
171
  }
172
+ // Memory observe after generation (fire-and-forget)
173
+ if (opts.memoryProvider?.observe) {
174
+ opts.memoryProvider.observe('user', text).catch(() => { });
175
+ if (result.text) {
176
+ opts.memoryProvider.observe('assistant', result.text).catch(() => { });
177
+ }
178
+ }
136
179
  return result;
137
180
  },
138
181
  stream(text) {
182
+ // For streaming, use onBeforeGeneration hook to inject memory context
183
+ const originalBeforeHook = baseOpts.onBeforeGeneration;
139
184
  const result = streamText({
140
185
  ...baseOpts,
141
186
  messages: useMemory
142
187
  ? [...history, { role: 'user', content: text }]
143
188
  : [{ role: 'user', content: text }],
189
+ onBeforeGeneration: opts.memoryProvider?.getContext
190
+ ? async (ctx) => {
191
+ // Inject memory context
192
+ try {
193
+ const memCtx = await Promise.race([
194
+ opts.memoryProvider.getContext(text, { tokenBudget: 2000 }),
195
+ new Promise((resolve) => setTimeout(() => resolve(null), MEMORY_TIMEOUT_MS)),
196
+ ]);
197
+ if (memCtx?.contextText) {
198
+ ctx = {
199
+ ...ctx,
200
+ messages: [
201
+ { role: 'system', content: memCtx.contextText },
202
+ ...ctx.messages,
203
+ ],
204
+ };
205
+ }
206
+ }
207
+ catch { /* non-fatal */ }
208
+ // Chain with user's hook if present
209
+ if (originalBeforeHook) {
210
+ const userResult = await originalBeforeHook(ctx);
211
+ return userResult ?? ctx;
212
+ }
213
+ return ctx;
214
+ }
215
+ : originalBeforeHook,
144
216
  usageLedger: mergeUsageLedgerOptions(baseOpts.usageLedger, {
145
217
  sessionId,
146
218
  source: 'agent.session.stream',
@@ -150,7 +222,14 @@ export function agent(opts) {
150
222
  if (useMemory) {
151
223
  history.push({ role: 'user', content: text });
152
224
  void result.text
153
- .then((replyText) => history.push({ role: 'assistant', content: replyText }))
225
+ .then((replyText) => {
226
+ history.push({ role: 'assistant', content: replyText });
227
+ // Memory observe after stream completes
228
+ if (opts.memoryProvider?.observe) {
229
+ opts.memoryProvider.observe('user', text).catch(() => { });
230
+ opts.memoryProvider.observe('assistant', replyText).catch(() => { });
231
+ }
232
+ })
154
233
  .catch(() => {
155
234
  /* history update failed, non-critical */
156
235
  });
@@ -1 +1 @@
1
- {"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/api/agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACL,YAAY,GAKb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAyB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EACL,uBAAuB,GAGxB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAA0B,MAAM,kBAAkB,CAAC;AAsHpG,SAAS,uBAAuB,CAC9B,GAAG,KAAmD;IAEtD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IAC3D,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7D,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAkB;IAC3C,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,IAAI,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC;QAC9B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;QACtB,QAAQ,CAAC,IAAI,CAAC,mB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AE,IAAI;QACX,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import { type AdaptableToolInput } from './runtime/toolAdapter.js';
2
2
  import { type AgentOSUsageLedgerOptions } from './runtime/usageLedger.js';
3
+ import type { ITool } from '../core/tools/ITool.js';
3
4
  import type { AgentCallRecord, AgencyTraceEvent } from './types.js';
5
+ import type { IModelRouter, ModelRouteParams } from '../core/llm/routing/IModelRouter.js';
4
6
  /**
5
7
  * A single chat message in a conversation history.
6
8
  * Mirrors the OpenAI / Anthropic message shape accepted by provider adapters.
@@ -198,6 +200,35 @@ export interface GenerateTextOptions {
198
200
  * @param fallbackProvider - The provider identifier being tried next.
199
201
  */
200
202
  onFallback?: (error: Error, fallbackProvider: string) => void;
203
+ /**
204
+ * Optional model router for intelligent provider/model selection.
205
+ * When provided, the router's `selectModel()` is called before provider
206
+ * resolution. The router result overrides `model`/`provider`.
207
+ * If the router returns `null`, falls back to standard resolution.
208
+ */
209
+ router?: IModelRouter;
210
+ /**
211
+ * Routing hints passed to the model router. Extracted automatically
212
+ * from system prompt and tool names when not provided.
213
+ */
214
+ routerParams?: Partial<ModelRouteParams>;
215
+ /**
216
+ * Called before each LLM generation step. Can inject memory context
217
+ * into messages, sanitize input via guardrails, or modify the prompt.
218
+ * Return a modified context to transform input, or void to pass through.
219
+ */
220
+ onBeforeGeneration?: (context: GenerationHookContext) => Promise<GenerationHookContext | void>;
221
+ /**
222
+ * Called after each LLM generation step. Can check output against
223
+ * guardrails, redact PII, or transform the response.
224
+ * Return a modified result to transform output, or void to pass through.
225
+ */
226
+ onAfterGeneration?: (result: GenerationHookResult) => Promise<GenerationHookResult | void>;
227
+ /**
228
+ * Called before each tool execution. Can modify arguments, apply
229
+ * permission checks, or return `null` to skip the tool call entirely.
230
+ */
231
+ onBeforeToolExecution?: (info: ToolCallHookInfo) => Promise<ToolCallHookInfo | null>;
201
232
  }
202
233
  /**
203
234
  * The completed result returned by {@link generateText}.
@@ -242,6 +273,54 @@ export interface GenerateTextResult {
242
273
  */
243
274
  plan?: Plan;
244
275
  }
276
+ /**
277
+ * Context available to pre-generation hooks.
278
+ * Hooks may return a modified copy to transform the generation input.
279
+ */
280
+ export interface GenerationHookContext {
281
+ /** Current messages array (system + conversation + user). */
282
+ messages: Message[];
283
+ /** System prompt text. */
284
+ system: string | undefined;
285
+ /** Tool definitions available for this step. */
286
+ tools: ITool[];
287
+ /** Resolved model ID. */
288
+ model: string;
289
+ /** Resolved provider ID. */
290
+ provider: string;
291
+ /** Current agentic step index (0-based). */
292
+ step: number;
293
+ /** The original user prompt (from opts.prompt). */
294
+ prompt: string | undefined;
295
+ }
296
+ /**
297
+ * Context available to post-generation hooks.
298
+ * Hooks may return a modified copy to transform the generation output.
299
+ */
300
+ export interface GenerationHookResult {
301
+ /** Generated text from the LLM. */
302
+ text: string;
303
+ /** Tool calls requested by the LLM. */
304
+ toolCalls: ToolCallRecord[];
305
+ /** Token usage for this step. */
306
+ usage: TokenUsage;
307
+ /** Current agentic step index (0-based). */
308
+ step: number;
309
+ }
310
+ /**
311
+ * Info about a tool call before execution.
312
+ * Hooks may return a modified copy or `null` to skip execution.
313
+ */
314
+ export interface ToolCallHookInfo {
315
+ /** Tool name. */
316
+ name: string;
317
+ /** Parsed arguments. */
318
+ args: Record<string, unknown>;
319
+ /** Tool call ID from the LLM. */
320
+ id: string;
321
+ /** Current agentic step index. */
322
+ step: number;
323
+ }
245
324
  /**
246
325
  * Default chain-of-thought instruction prepended to the system prompt when
247
326
  * tools are available and `chainOfThought` is enabled. Encourages the model
@@ -1 +1 @@
1
- {"version":3,"file":"generateText.d.ts","sourceRoot":"","sources":["../../src/api/generateText.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAc,KAAK,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC/E,OAAO,EAAsB,KAAK,yBAAyB,EAAE,MAAM,0B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3F;AAeD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,UAAU,CAC9B,QAAQ,EAAE;IAAE,kBAAkB,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;CAAE,EAClE,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC5C,SAAS,EAAE,MAAM,EAAE,EACnB,MAAM,EAAE,cAAc,GAAG,SAAS,EAClC,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,CAyD3B;AAuBD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAQxD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAChC,eAAe,CAAC,EAAE,MAAM,GACvB,qBAAqB,EAAE,CAiBzB;AAwBD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAsdzF"}
@@ -261,7 +261,35 @@ export async function generateText(opts) {
261
261
  let metricModelId;
262
262
  try {
263
263
  return await withAgentOSSpan('agentos.api.generate_text', async (span) => {
264
- const { providerId, modelId } = resolveModelOption(opts, 'text');
264
+ let { providerId, modelId } = resolveModelOption(opts, 'text');
265
+ // --- Model routing (optional) ---
266
+ if (opts.router) {
267
+ try {
268
+ const toolNames = opts.tools
269
+ ? (Array.isArray(opts.tools)
270
+ ? opts.tools
271
+ : [...(opts.tools.values?.() ?? [])])
272
+ .map((t) => t.name ?? t.function?.name)
273
+ .filter(Boolean)
274
+ : [];
275
+ const routeParams = {
276
+ taskHint: opts.routerParams?.taskHint ?? opts.system ?? opts.prompt ?? '',
277
+ requiredCapabilities: opts.routerParams?.requiredCapabilities ??
278
+ (toolNames.length > 0 ? ['function_calling'] : undefined),
279
+ optimizationPreference: opts.routerParams?.optimizationPreference ?? 'balanced',
280
+ ...opts.routerParams,
281
+ };
282
+ const routeResult = await opts.router.selectModel(routeParams, undefined);
283
+ if (routeResult) {
284
+ providerId =
285
+ routeResult.modelInfo?.providerId ?? providerId;
286
+ modelId = routeResult.modelId;
287
+ }
288
+ }
289
+ catch (routerErr) {
290
+ console.warn('[agentos] Model router error, falling back to standard resolution:', routerErr);
291
+ }
292
+ }
265
293
  const resolved = resolveProvider(providerId, modelId, {
266
294
  apiKey: opts.apiKey,
267
295
  baseUrl: opts.baseUrl,
@@ -339,12 +367,34 @@ export async function generateText(opts) {
339
367
  }
340
368
  }
341
369
  for (let step = 0; step < maxSteps; step++) {
370
+ // --- onBeforeGeneration hook ---
371
+ let effectiveMessages = messages;
372
+ if (opts.onBeforeGeneration) {
373
+ try {
374
+ const hookCtx = {
375
+ messages: [...messages],
376
+ system: opts.system,
377
+ tools: Array.from(toolMap.values()),
378
+ model: resolved.modelId,
379
+ provider: resolved.providerId,
380
+ step,
381
+ prompt: opts.prompt,
382
+ };
383
+ const modified = await opts.onBeforeGeneration(hookCtx);
384
+ if (modified) {
385
+ effectiveMessages = modified.messages;
386
+ }
387
+ }
388
+ catch (hookErr) {
389
+ console.warn('[agentos] onBeforeGeneration hook error:', hookErr);
390
+ }
391
+ }
342
392
  const response = await withAgentOSSpan('agentos.api.generate_text.step', async (stepSpan) => {
343
393
  stepSpan?.setAttribute('llm.provider', resolved.providerId);
344
394
  stepSpan?.setAttribute('llm.model', resolved.modelId);
345
395
  stepSpan?.setAttribute('agentos.api.step', step + 1);
346
396
  stepSpan?.setAttribute('agentos.api.tool_count', tools.length);
347
- const stepResponse = await provider.generateCompletion(resolved.modelId, messages, {
397
+ const stepResponse = await provider.generateCompletion(resolved.modelId, effectiveMessages, {
348
398
  tools: toolSchemas,
349
399
  temperature: opts.temperature,
350
400
  maxTokens: opts.maxTokens,
@@ -369,7 +419,7 @@ export async function generateText(opts) {
369
419
  if (!choice)
370
420
  break;
371
421
  const content = choice.message?.content;
372
- const textContent = typeof content === 'string' ? content : (content?.text ?? '');
422
+ let textContent = typeof content === 'string' ? content : (content?.text ?? '');
373
423
  let toolCallsInChoice = choice.message?.tool_calls ?? [];
374
424
  // --- Text-based tool-call fallback ---
375
425
  // When the provider returns no native tool_calls but tools were
@@ -390,6 +440,37 @@ export async function generateText(opts) {
390
440
  }));
391
441
  }
392
442
  }
443
+ // --- onAfterGeneration hook ---
444
+ if (opts.onAfterGeneration) {
445
+ try {
446
+ const stepUsage = {
447
+ promptTokens: response.usage?.promptTokens ?? 0,
448
+ completionTokens: response.usage?.completionTokens ?? 0,
449
+ totalTokens: response.usage?.totalTokens ?? 0,
450
+ costUSD: response.usage?.costUSD,
451
+ };
452
+ const toolCallRecords = toolCallsInChoice.map((tc) => ({
453
+ name: tc.function?.name ?? tc.name ?? '',
454
+ args: tc.function?.arguments ?? '{}',
455
+ }));
456
+ const hookResult = {
457
+ text: textContent,
458
+ toolCalls: toolCallRecords,
459
+ usage: stepUsage,
460
+ step,
461
+ };
462
+ const modified = await opts.onAfterGeneration(hookResult);
463
+ if (modified) {
464
+ textContent = modified.text;
465
+ if (modified.toolCalls.length === 0 && toolCallsInChoice.length > 0) {
466
+ toolCallsInChoice = [];
467
+ }
468
+ }
469
+ }
470
+ catch (hookErr) {
471
+ console.warn('[agentos] onAfterGeneration hook error:', hookErr);
472
+ }
473
+ }
393
474
  if (textContent && toolCallsInChoice.length === 0) {
394
475
  metricUsage = totalUsage;
395
476
  span?.setAttribute('agentos.api.finish_reason', choice.finishReason ?? 'stop');
@@ -436,6 +517,32 @@ export async function generateText(opts) {
436
517
  allToolCalls.push(record);
437
518
  continue;
438
519
  }
520
+ // --- onBeforeToolExecution hook ---
521
+ if (opts.onBeforeToolExecution) {
522
+ try {
523
+ const hookInfo = {
524
+ name: fnName,
525
+ args: parsedArgs,
526
+ id: tcId || '',
527
+ step,
528
+ };
529
+ const hookResult = await opts.onBeforeToolExecution(hookInfo);
530
+ if (hookResult === null) {
531
+ record.error = 'Skipped by onBeforeToolExecution hook';
532
+ messages.push({
533
+ role: 'tool',
534
+ tool_call_id: tcId,
535
+ content: JSON.stringify({ skipped: true }),
536
+ });
537
+ allToolCalls.push(record);
538
+ continue;
539
+ }
540
+ parsedArgs = hookResult.args;
541
+ }
542
+ catch (hookErr) {
543
+ console.warn('[agentos] onBeforeToolExecution hook error:', hookErr);
544
+ }
545
+ }
439
546
  if (tool) {
440
547
  try {
441
548
  const result = await tool.execute(parsedArgs, buildHelperToolExecutionContext('generateText', helperToolRunId, step, tcId || undefined));