@directive-run/ai 0.1.1 → 0.3.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 +97 -73
- package/dist/anthropic.cjs +1 -1
- package/dist/anthropic.cjs.map +1 -1
- package/dist/anthropic.d.cts +5 -9
- package/dist/anthropic.d.ts +5 -9
- package/dist/anthropic.js +1 -1
- package/dist/anthropic.js.map +1 -1
- package/dist/gemini.cjs +3 -0
- package/dist/gemini.cjs.map +1 -0
- package/dist/gemini.d.cts +93 -0
- package/dist/gemini.d.ts +93 -0
- package/dist/gemini.js +3 -0
- package/dist/gemini.js.map +1 -0
- package/dist/index.cjs +117 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1376 -2118
- package/dist/index.d.ts +1376 -2118
- package/dist/index.js +117 -45
- package/dist/index.js.map +1 -1
- package/dist/multi-agent-orchestrator-CxL8ycw_.d.cts +2290 -0
- package/dist/multi-agent-orchestrator-uMp8bLfV.d.ts +2290 -0
- package/dist/ollama.cjs.map +1 -1
- package/dist/ollama.d.cts +3 -2
- package/dist/ollama.d.ts +3 -2
- package/dist/ollama.js.map +1 -1
- package/dist/openai.cjs +2 -2
- package/dist/openai.cjs.map +1 -1
- package/dist/openai.d.cts +4 -8
- package/dist/openai.d.ts +4 -8
- package/dist/openai.js +2 -2
- package/dist/openai.js.map +1 -1
- package/dist/semantic-cache-F0psCRuz.d.cts +271 -0
- package/dist/semantic-cache-F0psCRuz.d.ts +271 -0
- package/dist/testing.cjs +42 -7
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +365 -5
- package/dist/testing.d.ts +365 -5
- package/dist/testing.js +42 -7
- package/dist/testing.js.map +1 -1
- package/dist/types-Co4BzMiH.d.cts +1373 -0
- package/dist/types-Co4BzMiH.d.ts +1373 -0
- package/package.json +8 -3
- package/dist/types-BKCdgKC-.d.cts +0 -300
- package/dist/types-BKCdgKC-.d.ts +0 -300
package/README.md
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
# @directive-run/ai
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@directive-run/ai)
|
|
4
|
+
[](https://www.npmjs.com/package/@directive-run/ai)
|
|
5
|
+
[](https://bundlephobia.com/package/@directive-run/ai)
|
|
6
|
+
[](https://github.com/directive-run/directive/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
AI agent orchestration with guardrails, cost tracking, and multi-agent coordination. Built on [Directive](https://www.npmjs.com/package/@directive-run/core)'s constraint-driven runtime.
|
|
9
|
+
|
|
10
|
+
- **No SDK dependencies** – pure `fetch` adapters for OpenAI, Anthropic, Ollama, and Gemini
|
|
11
|
+
- **Guardrails** – input, output, and tool call validation with retry support
|
|
12
|
+
- **Multi-agent orchestration** – parallel, sequential, and supervisor patterns
|
|
13
|
+
- **Cost tracking** – per-call token usage with pricing constants for every provider
|
|
14
|
+
- **Streaming** – async iterable streams with backpressure and streaming guardrails
|
|
15
|
+
- **Provider adapters** – swap providers by changing one import, not your codebase
|
|
4
16
|
|
|
5
17
|
## Install
|
|
6
18
|
|
|
@@ -8,89 +20,59 @@ AI agent orchestration, guardrails, and multi-agent coordination for Directive.
|
|
|
8
20
|
npm install @directive-run/core @directive-run/ai
|
|
9
21
|
```
|
|
10
22
|
|
|
11
|
-
Provider adapters are
|
|
23
|
+
Provider adapters are subpath exports – no extra packages needed.
|
|
12
24
|
|
|
13
|
-
##
|
|
25
|
+
## Quick Start
|
|
14
26
|
|
|
15
27
|
```typescript
|
|
16
|
-
import {
|
|
28
|
+
import { createAgentOrchestrator } from "@directive-run/ai";
|
|
17
29
|
import { createOpenAIRunner } from "@directive-run/ai/openai";
|
|
18
30
|
|
|
19
31
|
const runner = createOpenAIRunner({ apiKey: process.env.OPENAI_API_KEY! });
|
|
20
32
|
|
|
21
|
-
const
|
|
33
|
+
const orchestrator = createAgentOrchestrator({
|
|
22
34
|
runner,
|
|
23
|
-
agents: {
|
|
24
|
-
assistant: { instructions: "You are a helpful assistant." },
|
|
25
|
-
},
|
|
26
35
|
guardrails: {
|
|
27
36
|
input: [async (data) => ({ passed: data.input.length < 10000 })],
|
|
28
37
|
},
|
|
29
38
|
});
|
|
30
39
|
|
|
31
|
-
const result = await
|
|
40
|
+
const result = await orchestrator.run(
|
|
41
|
+
{ name: "assistant", instructions: "You are a helpful assistant." },
|
|
42
|
+
"Hello!",
|
|
43
|
+
);
|
|
32
44
|
console.log(result.output);
|
|
33
45
|
```
|
|
34
46
|
|
|
35
|
-
##
|
|
36
|
-
|
|
37
|
-
Directive adapters are intentionally thin – they map `createRunner()` config to each provider's HTTP API. This gives you:
|
|
38
|
-
|
|
39
|
-
- **No SDK dependencies** – pure `fetch`, no `openai` or `@anthropic-ai/sdk` to install
|
|
40
|
-
- **Uniform interface** – swap providers by changing one import, not your entire codebase
|
|
41
|
-
- **Built-in observability** – lifecycle hooks (`onBeforeCall`, `onAfterCall`, `onError`) on every adapter
|
|
42
|
-
- **Cost tracking** – `tokenUsage` breakdown (input/output) and pricing constants for every provider
|
|
43
|
-
- **Tree-shakeable** – each adapter is a separate entry point; unused adapters never enter your bundle
|
|
47
|
+
## Provider Adapters
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
Adapters are thin wrappers around each provider's HTTP API. No SDK dependencies – pure `fetch`.
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## Subpath Exports
|
|
58
|
-
|
|
59
|
-
| Import | Purpose |
|
|
60
|
-
|--------|---------|
|
|
61
|
-
| `@directive-run/ai` | Orchestrator, guardrails, multi-agent, streaming |
|
|
62
|
-
| `@directive-run/ai/testing` | Mock runners, test helpers |
|
|
63
|
-
| `@directive-run/ai/openai` | OpenAI, Azure, Together adapter |
|
|
64
|
-
| `@directive-run/ai/anthropic` | Anthropic Claude adapter |
|
|
65
|
-
| `@directive-run/ai/ollama` | Local Ollama inference adapter |
|
|
66
|
-
|
|
67
|
-
## Provider Comparison
|
|
68
|
-
|
|
69
|
-
| | OpenAI | Anthropic | Ollama |
|
|
70
|
-
|---|--------|-----------|--------|
|
|
71
|
-
| Import | `@directive-run/ai/openai` | `@directive-run/ai/anthropic` | `@directive-run/ai/ollama` |
|
|
72
|
-
| Default model | `gpt-4o` | `claude-sonnet-4-5-20250929` | `llama3` |
|
|
73
|
-
| API key required | Yes | Yes | No |
|
|
74
|
-
| Streaming runner | `createOpenAIStreamingRunner` | `createAnthropicStreamingRunner` | – |
|
|
75
|
-
| Embedder | `createOpenAIEmbedder` | – | – |
|
|
76
|
-
| Pricing constants | `OPENAI_PRICING` | `ANTHROPIC_PRICING` | – |
|
|
77
|
-
| Compatible APIs | Azure, Together, any OpenAI-compatible | – | – |
|
|
51
|
+
| | OpenAI | Anthropic | Ollama | Gemini |
|
|
52
|
+
|---|--------|-----------|--------|--------|
|
|
53
|
+
| Import | `@directive-run/ai/openai` | `@directive-run/ai/anthropic` | `@directive-run/ai/ollama` | `@directive-run/ai/gemini` |
|
|
54
|
+
| Default model | `gpt-4o` | `claude-sonnet-4-5-20250929` | `llama3` | `gemini-2.0-flash` |
|
|
55
|
+
| API key required | Yes | Yes | No | Yes |
|
|
56
|
+
| Streaming runner | `createOpenAIStreamingRunner` | `createAnthropicStreamingRunner` | – | `createGeminiStreamingRunner` |
|
|
57
|
+
| Embedder | `createOpenAIEmbedder` | – | – | – |
|
|
58
|
+
| Pricing constants | `OPENAI_PRICING` | `ANTHROPIC_PRICING` | – | `GEMINI_PRICING` |
|
|
59
|
+
| Compatible APIs | Azure, Together, any OpenAI-compatible | – | – | – |
|
|
78
60
|
|
|
79
61
|
## Cost Tracking
|
|
80
62
|
|
|
81
63
|
Every adapter returns `tokenUsage` with input/output breakdown:
|
|
82
64
|
|
|
83
65
|
```typescript
|
|
84
|
-
import { estimateCost } from
|
|
85
|
-
import { createOpenAIRunner, OPENAI_PRICING } from
|
|
66
|
+
import { estimateCost } from "@directive-run/ai";
|
|
67
|
+
import { createOpenAIRunner, OPENAI_PRICING } from "@directive-run/ai/openai";
|
|
86
68
|
|
|
87
69
|
const runner = createOpenAIRunner({ apiKey: process.env.OPENAI_API_KEY! });
|
|
88
|
-
const result = await runner(agent,
|
|
70
|
+
const result = await runner(agent, "Hello");
|
|
89
71
|
|
|
90
72
|
const { inputTokens, outputTokens } = result.tokenUsage!;
|
|
91
73
|
const cost =
|
|
92
|
-
estimateCost(inputTokens, OPENAI_PRICING[
|
|
93
|
-
estimateCost(outputTokens, OPENAI_PRICING[
|
|
74
|
+
estimateCost(inputTokens, OPENAI_PRICING["gpt-4o"].input) +
|
|
75
|
+
estimateCost(outputTokens, OPENAI_PRICING["gpt-4o"].output);
|
|
94
76
|
```
|
|
95
77
|
|
|
96
78
|
## Lifecycle Hooks
|
|
@@ -98,49 +80,91 @@ const cost =
|
|
|
98
80
|
Attach hooks to any adapter for observability:
|
|
99
81
|
|
|
100
82
|
```typescript
|
|
101
|
-
import { createAnthropicRunner } from
|
|
83
|
+
import { createAnthropicRunner } from "@directive-run/ai/anthropic";
|
|
102
84
|
|
|
103
85
|
const runner = createAnthropicRunner({
|
|
104
86
|
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
105
87
|
hooks: {
|
|
106
|
-
onBeforeCall: ({ agent, input }) => console.log(
|
|
88
|
+
onBeforeCall: ({ agent, input }) => console.log(`Calling ${agent.name}`),
|
|
107
89
|
onAfterCall: ({ durationMs, tokenUsage }) => {
|
|
108
|
-
metrics.track(
|
|
90
|
+
metrics.track("llm_call", { durationMs, ...tokenUsage });
|
|
109
91
|
},
|
|
110
92
|
onError: ({ error }) => Sentry.captureException(error),
|
|
111
93
|
},
|
|
112
94
|
});
|
|
113
95
|
```
|
|
114
96
|
|
|
115
|
-
##
|
|
97
|
+
## Multi-Agent Orchestration
|
|
116
98
|
|
|
117
|
-
|
|
99
|
+
Coordinate multiple agents with built-in execution patterns:
|
|
118
100
|
|
|
119
101
|
```typescript
|
|
120
|
-
import {
|
|
121
|
-
import {
|
|
102
|
+
import { createMultiAgentOrchestrator, parallel } from "@directive-run/ai";
|
|
103
|
+
import { createOpenAIRunner } from "@directive-run/ai/openai";
|
|
122
104
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
105
|
+
const runner = createOpenAIRunner({ apiKey: process.env.OPENAI_API_KEY! });
|
|
106
|
+
|
|
107
|
+
const researchAgent = { name: "researcher", instructions: "Research the topic thoroughly." };
|
|
108
|
+
const writerAgent = { name: "writer", instructions: "Write a clear summary." };
|
|
109
|
+
|
|
110
|
+
const orchestrator = createMultiAgentOrchestrator({
|
|
111
|
+
runner,
|
|
112
|
+
agents: {
|
|
113
|
+
researcher: { agent: researchAgent, maxConcurrent: 3 },
|
|
114
|
+
writer: { agent: writerAgent, maxConcurrent: 1 },
|
|
115
|
+
},
|
|
116
|
+
patterns: {
|
|
117
|
+
researchAndWrite: parallel(
|
|
118
|
+
["researcher", "writer"],
|
|
119
|
+
(results) => results.map((r) => r.output).join("\n\n"),
|
|
120
|
+
),
|
|
126
121
|
},
|
|
127
122
|
});
|
|
128
123
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
124
|
+
// Run the pattern
|
|
125
|
+
const result = await orchestrator.runPattern("researchAndWrite", "Quantum computing basics");
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Subpath Exports
|
|
129
|
+
|
|
130
|
+
| Import | Purpose |
|
|
131
|
+
|--------|---------|
|
|
132
|
+
| `@directive-run/ai` | Orchestrator, guardrails, multi-agent, streaming, memory |
|
|
133
|
+
| `@directive-run/ai/testing` | Mock runners, test helpers |
|
|
134
|
+
| `@directive-run/ai/openai` | OpenAI / Azure / Together adapter |
|
|
135
|
+
| `@directive-run/ai/anthropic` | Anthropic Claude adapter |
|
|
136
|
+
| `@directive-run/ai/ollama` | Local Ollama inference adapter |
|
|
137
|
+
| `@directive-run/ai/gemini` | Google Gemini adapter |
|
|
138
|
+
|
|
139
|
+
## Testing
|
|
140
|
+
|
|
141
|
+
Mock runners for unit testing without real LLM calls:
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { createAgentOrchestrator } from "@directive-run/ai";
|
|
145
|
+
import { createMockAgentRunner } from "@directive-run/ai/testing";
|
|
146
|
+
|
|
147
|
+
const mock = createMockAgentRunner({
|
|
148
|
+
responses: {
|
|
149
|
+
assistant: { output: "This is a mock response." },
|
|
133
150
|
},
|
|
134
151
|
});
|
|
135
152
|
|
|
136
|
-
const
|
|
153
|
+
const orchestrator = createAgentOrchestrator({ runner: mock.run });
|
|
154
|
+
|
|
155
|
+
const result = await orchestrator.run(
|
|
156
|
+
{ name: "assistant", instructions: "You are a helpful assistant." },
|
|
157
|
+
"Hello!",
|
|
158
|
+
);
|
|
137
159
|
// result.output === "This is a mock response."
|
|
138
160
|
```
|
|
139
161
|
|
|
140
|
-
|
|
162
|
+
## Documentation
|
|
163
|
+
|
|
164
|
+
- [AI Guide](https://directive.run/docs/ai)
|
|
165
|
+
- [API Reference](https://directive.run/docs/api)
|
|
166
|
+
- [GitHub](https://github.com/directive-run/directive)
|
|
141
167
|
|
|
142
168
|
## License
|
|
143
169
|
|
|
144
170
|
MIT
|
|
145
|
-
|
|
146
|
-
[Full documentation](https://directive.run/docs)
|
package/dist/anthropic.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var E=new Set(["http:","https:"]);function x(u){try{let o=new URL(u);if(!E.has(o.protocol))throw new Error(`[Directive] Invalid baseURL protocol "${o.protocol}" \u2013 only http: and https: are allowed`)}catch(o){throw o instanceof Error&&o.message.startsWith("[Directive]")?o:new Error(`[Directive] Invalid baseURL "${u}" \u2013 must be a valid URL (e.g. "https://api.openai.com/v1")`)}}function S(u){let{fetch:o=globalThis.fetch,buildRequest:
|
|
1
|
+
'use strict';var E=new Set(["http:","https:"]);function x(u){try{let o=new URL(u);if(!E.has(o.protocol))throw new Error(`[Directive] Invalid baseURL protocol "${o.protocol}" \u2013 only http: and https: are allowed`)}catch(o){throw o instanceof Error&&o.message.startsWith("[Directive]")?o:new Error(`[Directive] Invalid baseURL "${u}" \u2013 must be a valid URL (e.g. "https://api.openai.com/v1")`)}}function S(u){let{fetch:o=globalThis.fetch,buildRequest:y,parseResponse:T,parseOutput:m,hooks:g}=u,c=m??(e=>{try{return JSON.parse(e)}catch{return e}});return async(e,s,r)=>{let t=Date.now();g?.onBeforeCall?.({agent:e,input:s,timestamp:t});let i=[{role:"user",content:s}];try{let{url:d,init:l}=y(e,s,i),h=r?.signal?{...l,signal:r.signal}:l,p=await o(d,h);if(!p.ok){let b=await p.text().catch(()=>"");throw new Error(`[Directive] AgentRunner request failed: ${p.status} ${p.statusText}${b?` \u2013 ${b.slice(0,300)}`:""}`)}let a=await T(p,i),R={inputTokens:a.inputTokens??0,outputTokens:a.outputTokens??0},w={role:"assistant",content:a.text},A=[...i,w];r?.onMessage?.(w);let k=Date.now()-t;return g?.onAfterCall?.({agent:e,input:s,output:a.text,totalTokens:a.totalTokens,tokenUsage:R,durationMs:k,timestamp:Date.now()}),{output:c(a.text),messages:A,toolCalls:[],totalTokens:a.totalTokens,tokenUsage:R}}catch(d){let l=Date.now()-t;throw d instanceof Error&&g?.onError?.({agent:e,input:s,error:d,durationMs:l,timestamp:Date.now()}),d}}}var C={"claude-sonnet-4-5-20250929":{input:3,output:15},"claude-haiku-3-5-20241022":{input:.8,output:4},"claude-opus-4-20250514":{input:15,output:75}};function L(u){let{apiKey:o,model:y="claude-sonnet-4-5-20250929",maxTokens:T=4096,baseURL:m="https://api.anthropic.com/v1",fetch:g=globalThis.fetch,timeoutMs:f,hooks:c}=u;return x(m),typeof process<"u"&&process.env?.NODE_ENV!=="production"&&!o&&console.warn("[Directive] createAnthropicRunner: apiKey is empty. API calls will fail."),S({fetch:g,hooks:c,buildRequest:(e,s,r)=>({url:`${m}/messages`,init:{method:"POST",headers:{"Content-Type":"application/json","x-api-key":o,"anthropic-version":"2023-06-01"},body:JSON.stringify({model:e.model??y,max_tokens:T,system:e.instructions??"",messages:r.map(t=>({role:t.role,content:t.content}))}),...f!=null?{signal:AbortSignal.timeout(f)}:{}}}),parseResponse:async e=>{let s=await e.json(),r=s.content?.[0]?.text??"",t=s.usage?.input_tokens??0,i=s.usage?.output_tokens??0;return {text:r,totalTokens:t+i,inputTokens:t,outputTokens:i}}})}function U(u){let{apiKey:o,model:y="claude-sonnet-4-5-20250929",maxTokens:T=4096,baseURL:m="https://api.anthropic.com/v1",fetch:g=globalThis.fetch,hooks:f}=u;return x(m),typeof process<"u"&&process.env?.NODE_ENV!=="production"&&!o&&console.warn("[Directive] createAnthropicStreamingRunner: apiKey is empty. API calls will fail."),async(c,e,s)=>{let r=Date.now();f?.onBeforeCall?.({agent:c,input:e,timestamp:r});try{let t=await g(`${m}/messages`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":o,"anthropic-version":"2023-06-01"},body:JSON.stringify({model:c.model??y,max_tokens:T,system:c.instructions??"",messages:[{role:"user",content:e}],stream:!0}),signal:s.signal});if(!t.ok){let k=await t.text().catch(()=>"");throw new Error(`[Directive] Anthropic streaming error ${t.status}${k?` \u2013 ${k.slice(0,200)}`:""}`)}let i=t.body?.getReader();if(!i)throw new Error("[Directive] No response body");let d=new TextDecoder,l="",h="",p=0,a=0;try{for(;;){let{done:k,value:b}=await i.read();if(k)break;l+=d.decode(b,{stream:!0});let v=l.split(`
|
|
2
2
|
`);l=v.pop()??"";for(let D of v){if(!D.startsWith("data: "))continue;let O=D.slice(6).trim();try{let n=JSON.parse(O);if(n.type==="error")throw new Error(`[Directive] Anthropic stream error: ${n.error?.message??JSON.stringify(n.error)}`);n.type==="content_block_delta"&&n.delta?.type==="text_delta"&&(h+=n.delta.text,s.onToken?.(n.delta.text)),n.type==="message_delta"&&n.usage&&(a=n.usage.output_tokens??0),n.type==="message_start"&&n.message?.usage&&(p=n.message.usage.input_tokens??0);}catch(n){if(n instanceof SyntaxError)typeof process<"u"&&process.env?.NODE_ENV==="development"&&console.warn("[Directive] Malformed SSE event from Anthropic:",O);else throw n}}}}finally{i.cancel().catch(()=>{});}let R={role:"assistant",content:h};s.onMessage?.(R);let w={inputTokens:p,outputTokens:a},A=p+a;return f?.onAfterCall?.({agent:c,input:e,output:h,totalTokens:A,tokenUsage:w,durationMs:Date.now()-r,timestamp:Date.now()}),{output:h,messages:[{role:"user",content:e},R],toolCalls:[],totalTokens:A,tokenUsage:w}}catch(t){throw t instanceof Error&&f?.onError?.({agent:c,input:e,error:t,durationMs:Date.now()-r,timestamp:Date.now()}),t}}}exports.ANTHROPIC_PRICING=C;exports.createAnthropicRunner=L;exports.createAnthropicStreamingRunner=U;//# sourceMappingURL=anthropic.cjs.map
|
|
3
3
|
//# sourceMappingURL=anthropic.cjs.map
|
package/dist/anthropic.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/helpers.ts","../src/adapters/anthropic.ts"],"names":["ALLOWED_PROTOCOLS","validateBaseURL","baseURL","url","err","createRunner","options","fetchFn","buildRequest","parseResponse","parseOutput","hooks","parse","text","agent","input","runOptions","startTime","messages","init","fetchInit","response","errBody","parsed","tokenUsage","assistantMessage","allMessages","durationMs","ANTHROPIC_PRICING","createAnthropicRunner","apiKey","model","maxTokens","timeoutMs","_input","m","res","data","inputTokens","outputTokens","createAnthropicStreamingRunner","callbacks","reader","decoder","buf","fullText","done","value","lines","line","event","parseErr","assistantMsg","totalTokens"],"mappings":"aAoDA,IAAMA,EAAoB,IAAI,GAAA,CAAI,CAAC,OAAA,CAAS,QAAQ,CAAC,CAAA,CAM9C,SAASC,CAAAA,CAAgBC,EAAuB,CACrD,GAAI,CACF,IAAMC,CAAAA,CAAM,IAAI,GAAA,CAAID,CAAO,CAAA,CAC3B,GAAI,CAACF,CAAAA,CAAkB,GAAA,CAAIG,EAAI,QAAQ,CAAA,CACrC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,CAAAG,CAAAA,CACA,KAAA,CAAAC,CAAAA,CACA,KAAA,CAAOX,CAAAA,CACP,WAAY,IAAA,CAAK,GAAA,EAAI,CAAIa,CAAAA,CACzB,SAAA,CAAW,IAAA,CAAK,GAAA,EACjB,CAAC,CAAA,CAGIb,CACP,CACD,CACD","file":"anthropic.cjs","sourcesContent":["/**\n * Helper functions for AI adapter — createRunner, estimateCost, state queries, validation.\n */\n\nimport type {\n AdapterHooks,\n AgentLike,\n AgentRunner,\n RunResult,\n RunOptions,\n Message,\n TokenUsage,\n AgentState,\n ApprovalState,\n} from \"./types.js\";\n\n// ============================================================================\n// State Query Helpers\n// ============================================================================\n\n/** Check if agent is currently running. */\nexport function isAgentRunning(state: AgentState): boolean {\n return state.status === \"running\";\n}\n\n/** Check if there are pending approvals. */\nexport function hasPendingApprovals(state: ApprovalState): boolean {\n return state.pending.length > 0;\n}\n\n// ============================================================================\n// Cost Estimation\n// ============================================================================\n\n/**\n * Get total cost estimate based on token usage.\n *\n * @param tokenUsage - Total token count\n * @param ratePerMillionTokens - Cost per million tokens (required, no default to avoid stale pricing)\n * @returns Estimated cost in dollars\n */\nexport function estimateCost(\n tokenUsage: number,\n ratePerMillionTokens: number\n): number {\n return (tokenUsage / 1_000_000) * ratePerMillionTokens;\n}\n\n// ============================================================================\n// Validation Helpers\n// ============================================================================\n\nconst ALLOWED_PROTOCOLS = new Set([\"http:\", \"https:\"]);\n\n/**\n * Validate that a baseURL uses http or https.\n * Throws immediately at adapter creation time (not at call time) to catch config errors early.\n */\nexport function validateBaseURL(baseURL: string): void {\n try {\n const url = new URL(baseURL);\n if (!ALLOWED_PROTOCOLS.has(url.protocol)) {\n throw new Error(\n `[Directive] Invalid baseURL protocol \"${url.protocol}\" – only http: and https: are allowed`,\n );\n }\n } catch (err) {\n if (err instanceof Error && err.message.startsWith(\"[Directive]\")) {\n throw err;\n }\n\n throw new Error(\n `[Directive] Invalid baseURL \"${baseURL}\" – must be a valid URL (e.g. \"https://api.openai.com/v1\")`,\n );\n }\n}\n\n// ============================================================================\n// createRunner Helper\n// ============================================================================\n\n/** Parsed response from an LLM provider */\nexport interface ParsedResponse {\n text: string;\n totalTokens: number;\n /** Input token count, when available from the provider */\n inputTokens?: number;\n /** Output token count, when available from the provider */\n outputTokens?: number;\n}\n\n/** Options for creating an AgentRunner from buildRequest/parseResponse */\nexport interface CreateRunnerOptions {\n fetch?: typeof globalThis.fetch;\n buildRequest: (\n agent: AgentLike,\n input: string,\n messages: Message[]\n ) => { url: string; init: RequestInit };\n parseResponse: (\n response: Response,\n messages: Message[]\n ) => Promise<ParsedResponse>;\n parseOutput?: <T>(text: string) => T;\n /** Lifecycle hooks for tracing, logging, and metrics */\n hooks?: AdapterHooks;\n}\n\n/**\n * Create an AgentRunner from buildRequest/parseResponse helpers.\n * Reduces ~50 lines of fetch boilerplate to ~20 lines of configuration.\n *\n * Supports lifecycle hooks for observability:\n * - `onBeforeCall` fires before each API request\n * - `onAfterCall` fires after a successful response (includes token breakdown)\n * - `onError` fires when the request fails\n *\n * @example\n * ```typescript\n * const runClaude = createRunner({\n * buildRequest: (agent, input) => ({\n * url: \"/api/claude\",\n * init: {\n * method: \"POST\",\n * headers: { \"Content-Type\": \"application/json\" },\n * body: JSON.stringify({\n * model: agent.model ?? \"claude-haiku-4-5-20251001\",\n * system: agent.instructions ?? \"\",\n * messages: [{ role: \"user\", content: input }],\n * }),\n * },\n * }),\n * parseResponse: async (res) => {\n * const data = await res.json();\n * const inputTokens = data.usage?.input_tokens ?? 0;\n * const outputTokens = data.usage?.output_tokens ?? 0;\n * return {\n * text: data.content?.[0]?.text ?? \"\",\n * totalTokens: inputTokens + outputTokens,\n * inputTokens,\n * outputTokens,\n * };\n * },\n * hooks: {\n * onAfterCall: ({ durationMs, tokenUsage }) => {\n * console.log(`LLM call: ${durationMs}ms, ${tokenUsage.inputTokens}in/${tokenUsage.outputTokens}out`);\n * },\n * },\n * });\n * ```\n */\nexport function createRunner(options: CreateRunnerOptions): AgentRunner {\n const {\n fetch: fetchFn = globalThis.fetch,\n buildRequest,\n parseResponse,\n parseOutput,\n hooks,\n } = options;\n\n const defaultParseOutput = <T>(text: string): T => {\n try {\n return JSON.parse(text) as T;\n } catch {\n return text as unknown as T;\n }\n };\n\n const parse = parseOutput ?? defaultParseOutput;\n\n return async <T = unknown>(\n agent: AgentLike,\n input: string,\n runOptions?: RunOptions\n ): Promise<RunResult<T>> => {\n const startTime = Date.now();\n hooks?.onBeforeCall?.({ agent, input, timestamp: startTime });\n\n const messages: Message[] = [{ role: \"user\", content: input }];\n\n try {\n const { url, init } = buildRequest(agent, input, messages);\n\n const fetchInit: RequestInit = runOptions?.signal\n ? { ...init, signal: runOptions.signal }\n : init;\n\n const response = await fetchFn(url, fetchInit);\n\n if (!response.ok) {\n const errBody = await response.text().catch(() => \"\");\n\n throw new Error(\n `[Directive] AgentRunner request failed: ${response.status} ${response.statusText}${errBody ? ` – ${errBody.slice(0, 300)}` : \"\"}`,\n );\n }\n\n const parsed = await parseResponse(response, messages);\n const tokenUsage: TokenUsage = {\n inputTokens: parsed.inputTokens ?? 0,\n outputTokens: parsed.outputTokens ?? 0,\n };\n\n const assistantMessage: Message = { role: \"assistant\", content: parsed.text };\n const allMessages: Message[] = [...messages, assistantMessage];\n\n runOptions?.onMessage?.(assistantMessage);\n\n const durationMs = Date.now() - startTime;\n hooks?.onAfterCall?.({\n agent,\n input,\n output: parsed.text,\n totalTokens: parsed.totalTokens,\n tokenUsage,\n durationMs,\n timestamp: Date.now(),\n });\n\n return {\n output: parse<T>(parsed.text),\n messages: allMessages,\n toolCalls: [],\n totalTokens: parsed.totalTokens,\n tokenUsage,\n };\n } catch (err) {\n const durationMs = Date.now() - startTime;\n if (err instanceof Error) {\n hooks?.onError?.({\n agent,\n input,\n error: err,\n durationMs,\n timestamp: Date.now(),\n });\n }\n\n throw err;\n }\n };\n}\n","/**\n * @directive-run/ai/anthropic\n *\n * Anthropic adapter for Directive AI. Provides runners for the\n * Anthropic Messages API, including streaming support.\n *\n * @example\n * ```typescript\n * import { createAnthropicRunner, createAnthropicStreamingRunner } from '@directive-run/ai/anthropic';\n *\n * const runner = createAnthropicRunner({ apiKey: process.env.ANTHROPIC_API_KEY! });\n * ```\n */\n\nimport { createRunner, validateBaseURL } from \"../helpers.js\";\nimport type { AdapterHooks, AgentRunner, Message, TokenUsage } from \"../types.js\";\nimport type { StreamingCallbackRunner } from \"../stack.js\";\n\n// ============================================================================\n// Pricing Constants\n// ============================================================================\n\n/**\n * Anthropic model pricing (USD per million tokens).\n *\n * Use with `estimateCost()` for per-call cost tracking:\n * ```typescript\n * import { estimateCost } from '@directive-run/ai';\n * import { ANTHROPIC_PRICING } from '@directive-run/ai/anthropic';\n *\n * const cost =\n * estimateCost(result.tokenUsage!.inputTokens, ANTHROPIC_PRICING[\"claude-sonnet-4-5-20250929\"].input) +\n * estimateCost(result.tokenUsage!.outputTokens, ANTHROPIC_PRICING[\"claude-sonnet-4-5-20250929\"].output);\n * ```\n *\n * **Note:** Pricing changes over time. These values are provided as a convenience\n * and may not reflect the latest rates. Always verify at https://anthropic.com/pricing\n */\nexport const ANTHROPIC_PRICING: Record<string, { input: number; output: number }> = {\n\t\"claude-sonnet-4-5-20250929\": { input: 3, output: 15 },\n\t\"claude-haiku-3-5-20241022\": { input: 0.8, output: 4 },\n\t\"claude-opus-4-20250514\": { input: 15, output: 75 },\n};\n\n// ============================================================================\n// Anthropic Runner\n// ============================================================================\n\n/** Options for createAnthropicRunner */\nexport interface AnthropicRunnerOptions {\n\tapiKey: string;\n\tmodel?: string;\n\t/** @default 4096 */\n\tmaxTokens?: number;\n\tbaseURL?: string;\n\tfetch?: typeof globalThis.fetch;\n\t/** @default undefined */\n\ttimeoutMs?: number;\n\t/** Lifecycle hooks for tracing, logging, and metrics */\n\thooks?: AdapterHooks;\n}\n\n/**\n * Create an AgentRunner for the Anthropic Messages API.\n *\n * Returns `tokenUsage` with input/output breakdown for cost tracking.\n *\n * @example\n * ```typescript\n * const runner = createAnthropicRunner({\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * hooks: {\n * onAfterCall: ({ durationMs, tokenUsage }) => {\n * console.log(`${durationMs}ms – ${tokenUsage.inputTokens}in/${tokenUsage.outputTokens}out`);\n * },\n * },\n * });\n * const stack = createAgentStack({ runner, agents: { ... } });\n * ```\n */\nexport function createAnthropicRunner(\n\toptions: AnthropicRunnerOptions,\n): AgentRunner {\n\tconst {\n\t\tapiKey,\n\t\tmodel = \"claude-sonnet-4-5-20250929\",\n\t\tmaxTokens = 4096,\n\t\tbaseURL = \"https://api.anthropic.com/v1\",\n\t\tfetch: fetchFn = globalThis.fetch,\n\t\ttimeoutMs,\n\t\thooks,\n\t} = options;\n\n\tvalidateBaseURL(baseURL);\n\n\tif (typeof process !== \"undefined\" && process.env?.NODE_ENV !== \"production\" && !apiKey) {\n\t\tconsole.warn(\"[Directive] createAnthropicRunner: apiKey is empty. API calls will fail.\");\n\t}\n\n\treturn createRunner({\n\t\tfetch: fetchFn,\n\t\thooks,\n\t\tbuildRequest: (agent, _input, messages) => ({\n\t\t\turl: `${baseURL}/messages`,\n\t\t\tinit: {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"x-api-key\": apiKey,\n\t\t\t\t\t\"anthropic-version\": \"2023-06-01\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel: agent.model ?? model,\n\t\t\t\t\tmax_tokens: maxTokens,\n\t\t\t\t\tsystem: agent.instructions ?? \"\",\n\t\t\t\t\tmessages: messages.map((m) => ({\n\t\t\t\t\t\trole: m.role,\n\t\t\t\t\t\tcontent: m.content,\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t\t\t...(timeoutMs != null ? { signal: AbortSignal.timeout(timeoutMs) } : {}),\n\t\t\t},\n\t\t}),\n\t\tparseResponse: async (res) => {\n\t\t\tconst data = await res.json();\n\t\t\tconst text = data.content?.[0]?.text ?? \"\";\n\t\t\tconst inputTokens = data.usage?.input_tokens ?? 0;\n\t\t\tconst outputTokens = data.usage?.output_tokens ?? 0;\n\n\t\t\treturn {\n\t\t\t\ttext,\n\t\t\t\ttotalTokens: inputTokens + outputTokens,\n\t\t\t\tinputTokens,\n\t\t\t\toutputTokens,\n\t\t\t};\n\t\t},\n\t});\n}\n\n// ============================================================================\n// Anthropic Streaming Runner\n// ============================================================================\n\n/** Options for createAnthropicStreamingRunner */\nexport interface AnthropicStreamingRunnerOptions {\n\tapiKey: string;\n\tmodel?: string;\n\t/** @default 4096 */\n\tmaxTokens?: number;\n\tbaseURL?: string;\n\tfetch?: typeof globalThis.fetch;\n\t/** Lifecycle hooks for tracing, logging, and metrics */\n\thooks?: AdapterHooks;\n}\n\n/**\n * Create a StreamingCallbackRunner for the Anthropic Messages API with\n * server-sent events. Can be used standalone or paired with `createAnthropicRunner`.\n *\n * Returns `tokenUsage` with input/output breakdown for cost tracking.\n *\n * @example\n * ```typescript\n * const streamingRunner = createAnthropicStreamingRunner({\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * });\n * const stack = createAgentStack({\n * runner: createAnthropicRunner({ apiKey }),\n * streaming: { runner: streamingRunner },\n * agents: { ... },\n * });\n * ```\n */\nexport function createAnthropicStreamingRunner(\n\toptions: AnthropicStreamingRunnerOptions,\n): StreamingCallbackRunner {\n\tconst {\n\t\tapiKey,\n\t\tmodel = \"claude-sonnet-4-5-20250929\",\n\t\tmaxTokens = 4096,\n\t\tbaseURL = \"https://api.anthropic.com/v1\",\n\t\tfetch: fetchFn = globalThis.fetch,\n\t\thooks,\n\t} = options;\n\n\tvalidateBaseURL(baseURL);\n\n\tif (typeof process !== \"undefined\" && process.env?.NODE_ENV !== \"production\" && !apiKey) {\n\t\tconsole.warn(\"[Directive] createAnthropicStreamingRunner: apiKey is empty. API calls will fail.\");\n\t}\n\n\treturn async (agent, input, callbacks) => {\n\t\tconst startTime = Date.now();\n\t\thooks?.onBeforeCall?.({ agent, input, timestamp: startTime });\n\n\t\ttry {\n\t\t\tconst response = await fetchFn(`${baseURL}/messages`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"x-api-key\": apiKey,\n\t\t\t\t\t\"anthropic-version\": \"2023-06-01\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel: agent.model ?? model,\n\t\t\t\t\tmax_tokens: maxTokens,\n\t\t\t\t\tsystem: agent.instructions ?? \"\",\n\t\t\t\t\tmessages: [{ role: \"user\", content: input }],\n\t\t\t\t\tstream: true,\n\t\t\t\t}),\n\t\t\t\tsignal: callbacks.signal,\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errBody = await response.text().catch(() => \"\");\n\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`[Directive] Anthropic streaming error ${response.status}${errBody ? ` – ${errBody.slice(0, 200)}` : \"\"}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst reader = response.body?.getReader();\n\t\t\tif (!reader) {\n\t\t\t\tthrow new Error(\"[Directive] No response body\");\n\t\t\t}\n\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buf = \"\";\n\t\t\tlet fullText = \"\";\n\t\t\tlet inputTokens = 0;\n\t\t\tlet outputTokens = 0;\n\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\t\tif (done) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tbuf += decoder.decode(value, { stream: true });\n\t\t\t\t\tconst lines = buf.split(\"\\n\");\n\t\t\t\t\tbuf = lines.pop() ?? \"\";\n\n\t\t\t\t\tfor (const line of lines) {\n\t\t\t\t\t\tif (!line.startsWith(\"data: \")) {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst data = line.slice(6).trim();\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst event = JSON.parse(data);\n\t\t\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\t`[Directive] Anthropic stream error: ${event.error?.message ?? JSON.stringify(event.error)}`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tevent.type === \"content_block_delta\" &&\n\t\t\t\t\t\t\t\tevent.delta?.type === \"text_delta\"\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tfullText += event.delta.text;\n\t\t\t\t\t\t\t\tcallbacks.onToken?.(event.delta.text);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"message_delta\" && event.usage) {\n\t\t\t\t\t\t\t\toutputTokens = event.usage.output_tokens ?? 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"message_start\" && event.message?.usage) {\n\t\t\t\t\t\t\t\tinputTokens = event.message.usage.input_tokens ?? 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch (parseErr) {\n\t\t\t\t\t\t\tif (parseErr instanceof SyntaxError) {\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\ttypeof process !== \"undefined\" &&\n\t\t\t\t\t\t\t\t\tprocess.env?.NODE_ENV === \"development\"\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t\t\t\t\"[Directive] Malformed SSE event from Anthropic:\",\n\t\t\t\t\t\t\t\t\t\tdata,\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tthrow parseErr;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\treader.cancel().catch(() => {});\n\t\t\t}\n\n\t\t\tconst assistantMsg: Message = { role: \"assistant\", content: fullText };\n\t\t\tcallbacks.onMessage?.(assistantMsg);\n\n\t\t\tconst tokenUsage: TokenUsage = { inputTokens, outputTokens };\n\t\t\tconst totalTokens = inputTokens + outputTokens;\n\n\t\t\thooks?.onAfterCall?.({\n\t\t\t\tagent,\n\t\t\t\tinput,\n\t\t\t\toutput: fullText,\n\t\t\t\ttotalTokens,\n\t\t\t\ttokenUsage,\n\t\t\t\tdurationMs: Date.now() - startTime,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\toutput: fullText,\n\t\t\t\tmessages: [{ role: \"user\" as const, content: input }, assistantMsg],\n\t\t\t\ttoolCalls: [],\n\t\t\t\ttotalTokens,\n\t\t\t\ttokenUsage,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tif (err instanceof Error) {\n\t\t\t\thooks?.onError?.({\n\t\t\t\t\tagent,\n\t\t\t\t\tinput,\n\t\t\t\t\terror: err,\n\t\t\t\t\tdurationMs: Date.now() - startTime,\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow err;\n\t\t}\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/agent-utils.ts","../src/adapters/anthropic.ts"],"names":["ALLOWED_PROTOCOLS","validateBaseURL","baseURL","url","err","createRunner","options","fetchFn","buildRequest","parseResponse","parseOutput","hooks","parse","text","agent","input","runOptions","startTime","messages","init","fetchInit","response","errBody","parsed","tokenUsage","assistantMessage","allMessages","durationMs","ANTHROPIC_PRICING","createAnthropicRunner","apiKey","model","maxTokens","timeoutMs","_input","m","res","data","inputTokens","outputTokens","createAnthropicStreamingRunner","callbacks","reader","decoder","buf","fullText","done","value","lines","line","event","parseErr","assistantMsg","totalTokens"],"mappings":"aAoDA,IAAMA,EAAoB,IAAI,GAAA,CAAI,CAAC,OAAA,CAAS,QAAQ,CAAC,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C,CAAY,CAAA,CAClE,SAAA,CAAW,GACX,WAAA,CAAAC,CAAAA,CACA,UAAA,CAAA7B,CACD,CACD,CAAA,MAASpB,EAAK,CACb,MAAIA,CAAAA,YAAe,KAAA,EAClBO,CAAAA,EAAO,OAAA,GAAU,CAChB,KAAA,CAAAG,CAAAA,CACA,KAAA,CAAAC,CAAAA,CACA,KAAA,CAAOX,CAAAA,CACP,WAAY,IAAA,CAAK,GAAA,EAAI,CAAIa,CAAAA,CACzB,SAAA,CAAW,IAAA,CAAK,GAAA,EACjB,CAAC,CAAA,CAGIb,CACP,CACD,CACD","file":"anthropic.cjs","sourcesContent":["/**\n * Agent utilities — createRunner, estimateCost, state queries, URL validation.\n */\n\nimport type {\n AdapterHooks,\n AgentLike,\n AgentRunner,\n RunResult,\n RunOptions,\n Message,\n TokenUsage,\n AgentState,\n ApprovalState,\n} from \"./types.js\";\n\n// ============================================================================\n// State Query Helpers\n// ============================================================================\n\n/** Check if agent is currently running. */\nexport function isAgentRunning(state: AgentState): boolean {\n return state.status === \"running\";\n}\n\n/** Check if there are pending approvals. */\nexport function hasPendingApprovals(state: ApprovalState): boolean {\n return state.pending.length > 0;\n}\n\n// ============================================================================\n// Cost Estimation\n// ============================================================================\n\n/**\n * Get total cost estimate based on token usage.\n *\n * @param tokenUsage - Total token count\n * @param ratePerMillionTokens - Cost per million tokens (required, no default to avoid stale pricing)\n * @returns Estimated cost in dollars\n */\nexport function estimateCost(\n tokenUsage: number,\n ratePerMillionTokens: number\n): number {\n return (tokenUsage / 1_000_000) * ratePerMillionTokens;\n}\n\n// ============================================================================\n// Validation Helpers\n// ============================================================================\n\nconst ALLOWED_PROTOCOLS = new Set([\"http:\", \"https:\"]);\n\n/**\n * Validate that a baseURL uses http or https.\n * Throws immediately at adapter creation time (not at call time) to catch config errors early.\n */\nexport function validateBaseURL(baseURL: string): void {\n try {\n const url = new URL(baseURL);\n if (!ALLOWED_PROTOCOLS.has(url.protocol)) {\n throw new Error(\n `[Directive] Invalid baseURL protocol \"${url.protocol}\" – only http: and https: are allowed`,\n );\n }\n } catch (err) {\n if (err instanceof Error && err.message.startsWith(\"[Directive]\")) {\n throw err;\n }\n\n throw new Error(\n `[Directive] Invalid baseURL \"${baseURL}\" – must be a valid URL (e.g. \"https://api.openai.com/v1\")`,\n );\n }\n}\n\n// ============================================================================\n// createRunner Helper\n// ============================================================================\n\n/** Parsed response from an LLM provider */\nexport interface ParsedResponse {\n text: string;\n totalTokens: number;\n /** Input token count, when available from the provider */\n inputTokens?: number;\n /** Output token count, when available from the provider */\n outputTokens?: number;\n}\n\n/** Options for creating an AgentRunner from buildRequest/parseResponse */\nexport interface CreateRunnerOptions {\n fetch?: typeof globalThis.fetch;\n buildRequest: (\n agent: AgentLike,\n input: string,\n messages: Message[]\n ) => { url: string; init: RequestInit };\n parseResponse: (\n response: Response,\n messages: Message[]\n ) => Promise<ParsedResponse>;\n parseOutput?: <T>(text: string) => T;\n /** Lifecycle hooks for tracing, logging, and metrics */\n hooks?: AdapterHooks;\n}\n\n/**\n * Create an AgentRunner from buildRequest/parseResponse helpers.\n * Reduces ~50 lines of fetch boilerplate to ~20 lines of configuration.\n *\n * Supports lifecycle hooks for observability:\n * - `onBeforeCall` fires before each API request\n * - `onAfterCall` fires after a successful response (includes token breakdown)\n * - `onError` fires when the request fails\n *\n * @example\n * ```typescript\n * const runClaude = createRunner({\n * buildRequest: (agent, input) => ({\n * url: \"/api/claude\",\n * init: {\n * method: \"POST\",\n * headers: { \"Content-Type\": \"application/json\" },\n * body: JSON.stringify({\n * model: agent.model ?? \"claude-haiku-4-5-20251001\",\n * system: agent.instructions ?? \"\",\n * messages: [{ role: \"user\", content: input }],\n * }),\n * },\n * }),\n * parseResponse: async (res) => {\n * const data = await res.json();\n * const inputTokens = data.usage?.input_tokens ?? 0;\n * const outputTokens = data.usage?.output_tokens ?? 0;\n * return {\n * text: data.content?.[0]?.text ?? \"\",\n * totalTokens: inputTokens + outputTokens,\n * inputTokens,\n * outputTokens,\n * };\n * },\n * hooks: {\n * onAfterCall: ({ durationMs, tokenUsage }) => {\n * console.log(`LLM call: ${durationMs}ms, ${tokenUsage.inputTokens}in/${tokenUsage.outputTokens}out`);\n * },\n * },\n * });\n * ```\n */\nexport function createRunner(options: CreateRunnerOptions): AgentRunner {\n const {\n fetch: fetchFn = globalThis.fetch,\n buildRequest,\n parseResponse,\n parseOutput,\n hooks,\n } = options;\n\n const defaultParseOutput = <T>(text: string): T => {\n try {\n return JSON.parse(text) as T;\n } catch {\n return text as unknown as T;\n }\n };\n\n const parse = parseOutput ?? defaultParseOutput;\n\n return async <T = unknown>(\n agent: AgentLike,\n input: string,\n runOptions?: RunOptions\n ): Promise<RunResult<T>> => {\n const startTime = Date.now();\n hooks?.onBeforeCall?.({ agent, input, timestamp: startTime });\n\n const messages: Message[] = [{ role: \"user\", content: input }];\n\n try {\n const { url, init } = buildRequest(agent, input, messages);\n\n const fetchInit: RequestInit = runOptions?.signal\n ? { ...init, signal: runOptions.signal }\n : init;\n\n const response = await fetchFn(url, fetchInit);\n\n if (!response.ok) {\n const errBody = await response.text().catch(() => \"\");\n\n throw new Error(\n `[Directive] AgentRunner request failed: ${response.status} ${response.statusText}${errBody ? ` – ${errBody.slice(0, 300)}` : \"\"}`,\n );\n }\n\n const parsed = await parseResponse(response, messages);\n const tokenUsage: TokenUsage = {\n inputTokens: parsed.inputTokens ?? 0,\n outputTokens: parsed.outputTokens ?? 0,\n };\n\n const assistantMessage: Message = { role: \"assistant\", content: parsed.text };\n const allMessages: Message[] = [...messages, assistantMessage];\n\n runOptions?.onMessage?.(assistantMessage);\n\n const durationMs = Date.now() - startTime;\n hooks?.onAfterCall?.({\n agent,\n input,\n output: parsed.text,\n totalTokens: parsed.totalTokens,\n tokenUsage,\n durationMs,\n timestamp: Date.now(),\n });\n\n return {\n output: parse<T>(parsed.text),\n messages: allMessages,\n toolCalls: [],\n totalTokens: parsed.totalTokens,\n tokenUsage,\n };\n } catch (err) {\n const durationMs = Date.now() - startTime;\n if (err instanceof Error) {\n hooks?.onError?.({\n agent,\n input,\n error: err,\n durationMs,\n timestamp: Date.now(),\n });\n }\n\n throw err;\n }\n };\n}\n","/**\n * @directive-run/ai/anthropic\n *\n * Anthropic adapter for Directive AI. Provides runners for the\n * Anthropic Messages API, including streaming support.\n *\n * @example\n * ```typescript\n * import { createAnthropicRunner, createAnthropicStreamingRunner } from '@directive-run/ai/anthropic';\n *\n * const runner = createAnthropicRunner({ apiKey: process.env.ANTHROPIC_API_KEY! });\n * ```\n */\n\nimport { createRunner, validateBaseURL } from \"../agent-utils.js\";\nimport type { AdapterHooks, AgentRunner, Message, TokenUsage } from \"../types.js\";\nimport type { StreamingCallbackRunner } from \"../types.js\";\n\n// ============================================================================\n// Pricing Constants\n// ============================================================================\n\n/**\n * Anthropic model pricing (USD per million tokens).\n *\n * Use with `estimateCost()` for per-call cost tracking:\n * ```typescript\n * import { estimateCost } from '@directive-run/ai';\n * import { ANTHROPIC_PRICING } from '@directive-run/ai/anthropic';\n *\n * const cost =\n * estimateCost(result.tokenUsage!.inputTokens, ANTHROPIC_PRICING[\"claude-sonnet-4-5-20250929\"].input) +\n * estimateCost(result.tokenUsage!.outputTokens, ANTHROPIC_PRICING[\"claude-sonnet-4-5-20250929\"].output);\n * ```\n *\n * **Note:** Pricing changes over time. These values are provided as a convenience\n * and may not reflect the latest rates. Always verify at https://anthropic.com/pricing\n */\nexport const ANTHROPIC_PRICING: Record<string, { input: number; output: number }> = {\n\t\"claude-sonnet-4-5-20250929\": { input: 3, output: 15 },\n\t\"claude-haiku-3-5-20241022\": { input: 0.8, output: 4 },\n\t\"claude-opus-4-20250514\": { input: 15, output: 75 },\n};\n\n// ============================================================================\n// Anthropic Runner\n// ============================================================================\n\n/** Options for createAnthropicRunner */\nexport interface AnthropicRunnerOptions {\n\tapiKey: string;\n\tmodel?: string;\n\t/** @default 4096 */\n\tmaxTokens?: number;\n\tbaseURL?: string;\n\tfetch?: typeof globalThis.fetch;\n\t/** @default undefined */\n\ttimeoutMs?: number;\n\t/** Lifecycle hooks for tracing, logging, and metrics */\n\thooks?: AdapterHooks;\n}\n\n/**\n * Create an AgentRunner for the Anthropic Messages API.\n *\n * Returns `tokenUsage` with input/output breakdown for cost tracking.\n *\n * @example\n * ```typescript\n * const runner = createAnthropicRunner({\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * hooks: {\n * onAfterCall: ({ durationMs, tokenUsage }) => {\n * console.log(`${durationMs}ms – ${tokenUsage.inputTokens}in/${tokenUsage.outputTokens}out`);\n * },\n * },\n * });\n * const orchestrator = createAgentOrchestrator({ runner });\n * const result = await orchestrator.run(agent, input);\n * ```\n */\nexport function createAnthropicRunner(\n\toptions: AnthropicRunnerOptions,\n): AgentRunner {\n\tconst {\n\t\tapiKey,\n\t\tmodel = \"claude-sonnet-4-5-20250929\",\n\t\tmaxTokens = 4096,\n\t\tbaseURL = \"https://api.anthropic.com/v1\",\n\t\tfetch: fetchFn = globalThis.fetch,\n\t\ttimeoutMs,\n\t\thooks,\n\t} = options;\n\n\tvalidateBaseURL(baseURL);\n\n\tif (typeof process !== \"undefined\" && process.env?.NODE_ENV !== \"production\" && !apiKey) {\n\t\tconsole.warn(\"[Directive] createAnthropicRunner: apiKey is empty. API calls will fail.\");\n\t}\n\n\treturn createRunner({\n\t\tfetch: fetchFn,\n\t\thooks,\n\t\tbuildRequest: (agent, _input, messages) => ({\n\t\t\turl: `${baseURL}/messages`,\n\t\t\tinit: {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"x-api-key\": apiKey,\n\t\t\t\t\t\"anthropic-version\": \"2023-06-01\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel: agent.model ?? model,\n\t\t\t\t\tmax_tokens: maxTokens,\n\t\t\t\t\tsystem: agent.instructions ?? \"\",\n\t\t\t\t\tmessages: messages.map((m) => ({\n\t\t\t\t\t\trole: m.role,\n\t\t\t\t\t\tcontent: m.content,\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t\t\t...(timeoutMs != null ? { signal: AbortSignal.timeout(timeoutMs) } : {}),\n\t\t\t},\n\t\t}),\n\t\tparseResponse: async (res) => {\n\t\t\tconst data = await res.json();\n\t\t\tconst text = data.content?.[0]?.text ?? \"\";\n\t\t\tconst inputTokens = data.usage?.input_tokens ?? 0;\n\t\t\tconst outputTokens = data.usage?.output_tokens ?? 0;\n\n\t\t\treturn {\n\t\t\t\ttext,\n\t\t\t\ttotalTokens: inputTokens + outputTokens,\n\t\t\t\tinputTokens,\n\t\t\t\toutputTokens,\n\t\t\t};\n\t\t},\n\t});\n}\n\n// ============================================================================\n// Anthropic Streaming Runner\n// ============================================================================\n\n/** Options for createAnthropicStreamingRunner */\nexport interface AnthropicStreamingRunnerOptions {\n\tapiKey: string;\n\tmodel?: string;\n\t/** @default 4096 */\n\tmaxTokens?: number;\n\tbaseURL?: string;\n\tfetch?: typeof globalThis.fetch;\n\t/** Lifecycle hooks for tracing, logging, and metrics */\n\thooks?: AdapterHooks;\n}\n\n/**\n * Create a StreamingCallbackRunner for the Anthropic Messages API with\n * server-sent events. Can be used standalone or paired with `createAnthropicRunner`.\n *\n * Returns `tokenUsage` with input/output breakdown for cost tracking.\n *\n * @example\n * ```typescript\n * const streamingRunner = createAnthropicStreamingRunner({\n * apiKey: process.env.ANTHROPIC_API_KEY!,\n * });\n * const streamRunner = createStreamingRunner(streamingRunner);\n * const { stream, result } = streamRunner(agent, input);\n * ```\n */\nexport function createAnthropicStreamingRunner(\n\toptions: AnthropicStreamingRunnerOptions,\n): StreamingCallbackRunner {\n\tconst {\n\t\tapiKey,\n\t\tmodel = \"claude-sonnet-4-5-20250929\",\n\t\tmaxTokens = 4096,\n\t\tbaseURL = \"https://api.anthropic.com/v1\",\n\t\tfetch: fetchFn = globalThis.fetch,\n\t\thooks,\n\t} = options;\n\n\tvalidateBaseURL(baseURL);\n\n\tif (typeof process !== \"undefined\" && process.env?.NODE_ENV !== \"production\" && !apiKey) {\n\t\tconsole.warn(\"[Directive] createAnthropicStreamingRunner: apiKey is empty. API calls will fail.\");\n\t}\n\n\treturn async (agent, input, callbacks) => {\n\t\tconst startTime = Date.now();\n\t\thooks?.onBeforeCall?.({ agent, input, timestamp: startTime });\n\n\t\ttry {\n\t\t\tconst response = await fetchFn(`${baseURL}/messages`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"x-api-key\": apiKey,\n\t\t\t\t\t\"anthropic-version\": \"2023-06-01\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel: agent.model ?? model,\n\t\t\t\t\tmax_tokens: maxTokens,\n\t\t\t\t\tsystem: agent.instructions ?? \"\",\n\t\t\t\t\tmessages: [{ role: \"user\", content: input }],\n\t\t\t\t\tstream: true,\n\t\t\t\t}),\n\t\t\t\tsignal: callbacks.signal,\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errBody = await response.text().catch(() => \"\");\n\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`[Directive] Anthropic streaming error ${response.status}${errBody ? ` – ${errBody.slice(0, 200)}` : \"\"}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst reader = response.body?.getReader();\n\t\t\tif (!reader) {\n\t\t\t\tthrow new Error(\"[Directive] No response body\");\n\t\t\t}\n\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buf = \"\";\n\t\t\tlet fullText = \"\";\n\t\t\tlet inputTokens = 0;\n\t\t\tlet outputTokens = 0;\n\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\t\tif (done) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tbuf += decoder.decode(value, { stream: true });\n\t\t\t\t\tconst lines = buf.split(\"\\n\");\n\t\t\t\t\tbuf = lines.pop() ?? \"\";\n\n\t\t\t\t\tfor (const line of lines) {\n\t\t\t\t\t\tif (!line.startsWith(\"data: \")) {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst data = line.slice(6).trim();\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst event = JSON.parse(data);\n\t\t\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\t`[Directive] Anthropic stream error: ${event.error?.message ?? JSON.stringify(event.error)}`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tevent.type === \"content_block_delta\" &&\n\t\t\t\t\t\t\t\tevent.delta?.type === \"text_delta\"\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tfullText += event.delta.text;\n\t\t\t\t\t\t\t\tcallbacks.onToken?.(event.delta.text);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"message_delta\" && event.usage) {\n\t\t\t\t\t\t\t\toutputTokens = event.usage.output_tokens ?? 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"message_start\" && event.message?.usage) {\n\t\t\t\t\t\t\t\tinputTokens = event.message.usage.input_tokens ?? 0;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch (parseErr) {\n\t\t\t\t\t\t\tif (parseErr instanceof SyntaxError) {\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\ttypeof process !== \"undefined\" &&\n\t\t\t\t\t\t\t\t\tprocess.env?.NODE_ENV === \"development\"\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t\t\t\t\"[Directive] Malformed SSE event from Anthropic:\",\n\t\t\t\t\t\t\t\t\t\tdata,\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tthrow parseErr;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\treader.cancel().catch(() => {});\n\t\t\t}\n\n\t\t\tconst assistantMsg: Message = { role: \"assistant\", content: fullText };\n\t\t\tcallbacks.onMessage?.(assistantMsg);\n\n\t\t\tconst tokenUsage: TokenUsage = { inputTokens, outputTokens };\n\t\t\tconst totalTokens = inputTokens + outputTokens;\n\n\t\t\thooks?.onAfterCall?.({\n\t\t\t\tagent,\n\t\t\t\tinput,\n\t\t\t\toutput: fullText,\n\t\t\t\ttotalTokens,\n\t\t\t\ttokenUsage,\n\t\t\t\tdurationMs: Date.now() - startTime,\n\t\t\t\ttimestamp: Date.now(),\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\toutput: fullText,\n\t\t\t\tmessages: [{ role: \"user\" as const, content: input }, assistantMsg],\n\t\t\t\ttoolCalls: [],\n\t\t\t\ttotalTokens,\n\t\t\t\ttokenUsage,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tif (err instanceof Error) {\n\t\t\t\thooks?.onError?.({\n\t\t\t\t\tagent,\n\t\t\t\t\tinput,\n\t\t\t\t\terror: err,\n\t\t\t\t\tdurationMs: Date.now() - startTime,\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow err;\n\t\t}\n\t};\n}\n"]}
|
package/dist/anthropic.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { StreamingCallbackRunner } from './index.cjs';
|
|
1
|
+
import { a as AdapterHooks, b as AgentRunner, aX as StreamingCallbackRunner } from './types-Co4BzMiH.cjs';
|
|
3
2
|
import '@directive-run/core';
|
|
4
|
-
import '@directive-run/core/plugins';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* @directive-run/ai/anthropic
|
|
@@ -65,7 +63,8 @@ interface AnthropicRunnerOptions {
|
|
|
65
63
|
* },
|
|
66
64
|
* },
|
|
67
65
|
* });
|
|
68
|
-
* const
|
|
66
|
+
* const orchestrator = createAgentOrchestrator({ runner });
|
|
67
|
+
* const result = await orchestrator.run(agent, input);
|
|
69
68
|
* ```
|
|
70
69
|
*/
|
|
71
70
|
declare function createAnthropicRunner(options: AnthropicRunnerOptions): AgentRunner;
|
|
@@ -91,11 +90,8 @@ interface AnthropicStreamingRunnerOptions {
|
|
|
91
90
|
* const streamingRunner = createAnthropicStreamingRunner({
|
|
92
91
|
* apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
93
92
|
* });
|
|
94
|
-
* const
|
|
95
|
-
*
|
|
96
|
-
* streaming: { runner: streamingRunner },
|
|
97
|
-
* agents: { ... },
|
|
98
|
-
* });
|
|
93
|
+
* const streamRunner = createStreamingRunner(streamingRunner);
|
|
94
|
+
* const { stream, result } = streamRunner(agent, input);
|
|
99
95
|
* ```
|
|
100
96
|
*/
|
|
101
97
|
declare function createAnthropicStreamingRunner(options: AnthropicStreamingRunnerOptions): StreamingCallbackRunner;
|
package/dist/anthropic.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { StreamingCallbackRunner } from './index.js';
|
|
1
|
+
import { a as AdapterHooks, b as AgentRunner, aX as StreamingCallbackRunner } from './types-Co4BzMiH.js';
|
|
3
2
|
import '@directive-run/core';
|
|
4
|
-
import '@directive-run/core/plugins';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* @directive-run/ai/anthropic
|
|
@@ -65,7 +63,8 @@ interface AnthropicRunnerOptions {
|
|
|
65
63
|
* },
|
|
66
64
|
* },
|
|
67
65
|
* });
|
|
68
|
-
* const
|
|
66
|
+
* const orchestrator = createAgentOrchestrator({ runner });
|
|
67
|
+
* const result = await orchestrator.run(agent, input);
|
|
69
68
|
* ```
|
|
70
69
|
*/
|
|
71
70
|
declare function createAnthropicRunner(options: AnthropicRunnerOptions): AgentRunner;
|
|
@@ -91,11 +90,8 @@ interface AnthropicStreamingRunnerOptions {
|
|
|
91
90
|
* const streamingRunner = createAnthropicStreamingRunner({
|
|
92
91
|
* apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
93
92
|
* });
|
|
94
|
-
* const
|
|
95
|
-
*
|
|
96
|
-
* streaming: { runner: streamingRunner },
|
|
97
|
-
* agents: { ... },
|
|
98
|
-
* });
|
|
93
|
+
* const streamRunner = createStreamingRunner(streamingRunner);
|
|
94
|
+
* const { stream, result } = streamRunner(agent, input);
|
|
99
95
|
* ```
|
|
100
96
|
*/
|
|
101
97
|
declare function createAnthropicStreamingRunner(options: AnthropicStreamingRunnerOptions): StreamingCallbackRunner;
|
package/dist/anthropic.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var E=new Set(["http:","https:"]);function x(u){try{let o=new URL(u);if(!E.has(o.protocol))throw new Error(`[Directive] Invalid baseURL protocol "${o.protocol}" \u2013 only http: and https: are allowed`)}catch(o){throw o instanceof Error&&o.message.startsWith("[Directive]")?o:new Error(`[Directive] Invalid baseURL "${u}" \u2013 must be a valid URL (e.g. "https://api.openai.com/v1")`)}}function S(u){let{fetch:o=globalThis.fetch,buildRequest:
|
|
1
|
+
var E=new Set(["http:","https:"]);function x(u){try{let o=new URL(u);if(!E.has(o.protocol))throw new Error(`[Directive] Invalid baseURL protocol "${o.protocol}" \u2013 only http: and https: are allowed`)}catch(o){throw o instanceof Error&&o.message.startsWith("[Directive]")?o:new Error(`[Directive] Invalid baseURL "${u}" \u2013 must be a valid URL (e.g. "https://api.openai.com/v1")`)}}function S(u){let{fetch:o=globalThis.fetch,buildRequest:y,parseResponse:T,parseOutput:m,hooks:g}=u,c=m??(e=>{try{return JSON.parse(e)}catch{return e}});return async(e,s,r)=>{let t=Date.now();g?.onBeforeCall?.({agent:e,input:s,timestamp:t});let i=[{role:"user",content:s}];try{let{url:d,init:l}=y(e,s,i),h=r?.signal?{...l,signal:r.signal}:l,p=await o(d,h);if(!p.ok){let b=await p.text().catch(()=>"");throw new Error(`[Directive] AgentRunner request failed: ${p.status} ${p.statusText}${b?` \u2013 ${b.slice(0,300)}`:""}`)}let a=await T(p,i),R={inputTokens:a.inputTokens??0,outputTokens:a.outputTokens??0},w={role:"assistant",content:a.text},A=[...i,w];r?.onMessage?.(w);let k=Date.now()-t;return g?.onAfterCall?.({agent:e,input:s,output:a.text,totalTokens:a.totalTokens,tokenUsage:R,durationMs:k,timestamp:Date.now()}),{output:c(a.text),messages:A,toolCalls:[],totalTokens:a.totalTokens,tokenUsage:R}}catch(d){let l=Date.now()-t;throw d instanceof Error&&g?.onError?.({agent:e,input:s,error:d,durationMs:l,timestamp:Date.now()}),d}}}var C={"claude-sonnet-4-5-20250929":{input:3,output:15},"claude-haiku-3-5-20241022":{input:.8,output:4},"claude-opus-4-20250514":{input:15,output:75}};function L(u){let{apiKey:o,model:y="claude-sonnet-4-5-20250929",maxTokens:T=4096,baseURL:m="https://api.anthropic.com/v1",fetch:g=globalThis.fetch,timeoutMs:f,hooks:c}=u;return x(m),typeof process<"u"&&process.env?.NODE_ENV!=="production"&&!o&&console.warn("[Directive] createAnthropicRunner: apiKey is empty. API calls will fail."),S({fetch:g,hooks:c,buildRequest:(e,s,r)=>({url:`${m}/messages`,init:{method:"POST",headers:{"Content-Type":"application/json","x-api-key":o,"anthropic-version":"2023-06-01"},body:JSON.stringify({model:e.model??y,max_tokens:T,system:e.instructions??"",messages:r.map(t=>({role:t.role,content:t.content}))}),...f!=null?{signal:AbortSignal.timeout(f)}:{}}}),parseResponse:async e=>{let s=await e.json(),r=s.content?.[0]?.text??"",t=s.usage?.input_tokens??0,i=s.usage?.output_tokens??0;return {text:r,totalTokens:t+i,inputTokens:t,outputTokens:i}}})}function U(u){let{apiKey:o,model:y="claude-sonnet-4-5-20250929",maxTokens:T=4096,baseURL:m="https://api.anthropic.com/v1",fetch:g=globalThis.fetch,hooks:f}=u;return x(m),typeof process<"u"&&process.env?.NODE_ENV!=="production"&&!o&&console.warn("[Directive] createAnthropicStreamingRunner: apiKey is empty. API calls will fail."),async(c,e,s)=>{let r=Date.now();f?.onBeforeCall?.({agent:c,input:e,timestamp:r});try{let t=await g(`${m}/messages`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":o,"anthropic-version":"2023-06-01"},body:JSON.stringify({model:c.model??y,max_tokens:T,system:c.instructions??"",messages:[{role:"user",content:e}],stream:!0}),signal:s.signal});if(!t.ok){let k=await t.text().catch(()=>"");throw new Error(`[Directive] Anthropic streaming error ${t.status}${k?` \u2013 ${k.slice(0,200)}`:""}`)}let i=t.body?.getReader();if(!i)throw new Error("[Directive] No response body");let d=new TextDecoder,l="",h="",p=0,a=0;try{for(;;){let{done:k,value:b}=await i.read();if(k)break;l+=d.decode(b,{stream:!0});let v=l.split(`
|
|
2
2
|
`);l=v.pop()??"";for(let D of v){if(!D.startsWith("data: "))continue;let O=D.slice(6).trim();try{let n=JSON.parse(O);if(n.type==="error")throw new Error(`[Directive] Anthropic stream error: ${n.error?.message??JSON.stringify(n.error)}`);n.type==="content_block_delta"&&n.delta?.type==="text_delta"&&(h+=n.delta.text,s.onToken?.(n.delta.text)),n.type==="message_delta"&&n.usage&&(a=n.usage.output_tokens??0),n.type==="message_start"&&n.message?.usage&&(p=n.message.usage.input_tokens??0);}catch(n){if(n instanceof SyntaxError)typeof process<"u"&&process.env?.NODE_ENV==="development"&&console.warn("[Directive] Malformed SSE event from Anthropic:",O);else throw n}}}}finally{i.cancel().catch(()=>{});}let R={role:"assistant",content:h};s.onMessage?.(R);let w={inputTokens:p,outputTokens:a},A=p+a;return f?.onAfterCall?.({agent:c,input:e,output:h,totalTokens:A,tokenUsage:w,durationMs:Date.now()-r,timestamp:Date.now()}),{output:h,messages:[{role:"user",content:e},R],toolCalls:[],totalTokens:A,tokenUsage:w}}catch(t){throw t instanceof Error&&f?.onError?.({agent:c,input:e,error:t,durationMs:Date.now()-r,timestamp:Date.now()}),t}}}export{C as ANTHROPIC_PRICING,L as createAnthropicRunner,U as createAnthropicStreamingRunner};//# sourceMappingURL=anthropic.js.map
|
|
3
3
|
//# sourceMappingURL=anthropic.js.map
|