@thoughtminers/agent-runtime-anthropic 0.1.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/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/render.d.ts +14 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +89 -0
- package/dist/render.js.map +1 -0
- package/dist/stream.d.ts +15 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +85 -0
- package/dist/stream.js.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 thoughtminers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @thoughtminers/agent-runtime-anthropic
|
|
2
|
+
|
|
3
|
+
Claude adapter for `@thoughtminers/agent-runtime-core`. Wraps `@anthropic-ai/sdk`
|
|
4
|
+
(peer dependency) `messages.stream()` into the harness's normalized
|
|
5
|
+
`ProviderEvent` stream.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createAnthropicProvider } from '@thoughtminers/agent-runtime-anthropic';
|
|
9
|
+
|
|
10
|
+
const provider = createAnthropicProvider({
|
|
11
|
+
// all optional:
|
|
12
|
+
apiKey: process.env.ANTHROPIC_API_KEY, // or ambient env / your own client
|
|
13
|
+
contextWindows: { '<model-id>': 200_000 }, // model ids are config, never hardcoded
|
|
14
|
+
defaultContextWindow: 200_000,
|
|
15
|
+
extraParams: { thinking: { type: 'adaptive' } }, // merged verbatim into every request
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
What it does for you:
|
|
20
|
+
|
|
21
|
+
- **Prompt caching**: `cache_control: { type: 'ephemeral' }` on the last system
|
|
22
|
+
block, every request.
|
|
23
|
+
- **Parallel-tool contract**: consecutive `tool_result` turns render into ONE
|
|
24
|
+
user message (the API rejects orphans).
|
|
25
|
+
- **Abort**: the caller's `AbortSignal` aborts the underlying request; errors
|
|
26
|
+
normalize to the harness `AbortError` contract.
|
|
27
|
+
- **Stop reasons**: `end_turn / tool_use / max_tokens / stop_sequence / refusal`
|
|
28
|
+
map 1:1; anything else (e.g. `pause_turn`) becomes `other`.
|
|
29
|
+
- **Usage**: input/output + cache read/write tokens surfaced on `turn_end`.
|
|
30
|
+
|
|
31
|
+
Testing: unit tests run against a fake SDK stream (no network). The smoke test
|
|
32
|
+
is env-gated: `ANTHROPIC_API_KEY=... SMOKE_MODEL=<model-id> pnpm test`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import type { Provider } from '@thoughtminers/agent-runtime-core';
|
|
3
|
+
export interface AnthropicProviderOptions {
|
|
4
|
+
/** Bring your own client (also how tests inject a fake). */
|
|
5
|
+
client?: Anthropic;
|
|
6
|
+
/** Used only when no client is given; falls back to ANTHROPIC_API_KEY. */
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
/** Per-model context windows (tokens). Model ids are CONFIG, never hardcoded. */
|
|
9
|
+
contextWindows?: Record<string, number>;
|
|
10
|
+
/** Fallback window for models not in the map. Default 200_000. */
|
|
11
|
+
defaultContextWindow?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Provider-specific request params merged verbatim into every call
|
|
14
|
+
* (e.g. thinking/effort knobs) — keeps the core seam provider-neutral.
|
|
15
|
+
*/
|
|
16
|
+
extraParams?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export declare const createAnthropicProvider: (options?: AnthropicProviderOptions) => Provider;
|
|
19
|
+
export { normalizeStream, type AnthropicMessageStreamLike } from './stream.js';
|
|
20
|
+
export { renderSystem, renderToolChoice, renderTools, renderTurns } from './render.js';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAmB,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAInF,MAAM,WAAW,wBAAwB;IACvC,4DAA4D;IAC5D,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,0EAA0E;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,kEAAkE;IAClE,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED,eAAO,MAAM,uBAAuB,GAAI,UAAS,wBAA6B,KAAG,QAqChF,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,KAAK,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import { renderSystem, renderToolChoice, renderTools, renderTurns } from './render.js';
|
|
3
|
+
import { normalizeStream } from './stream.js';
|
|
4
|
+
export const createAnthropicProvider = (options = {}) => {
|
|
5
|
+
// Lazy client: importing/configuring the provider must not require the key.
|
|
6
|
+
let client = options.client;
|
|
7
|
+
const getClient = () => {
|
|
8
|
+
if (!client) {
|
|
9
|
+
client = new Anthropic(options.apiKey !== undefined ? { apiKey: options.apiKey } : {});
|
|
10
|
+
}
|
|
11
|
+
return client;
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
name: 'anthropic',
|
|
15
|
+
contextWindow: (model) => options.contextWindows?.[model] ?? options.defaultContextWindow ?? 200_000,
|
|
16
|
+
stream: (req, opts) => {
|
|
17
|
+
const system = renderSystem(req.system);
|
|
18
|
+
const tools = renderTools(req.tools);
|
|
19
|
+
const toolChoice = tools ? renderToolChoice(req.toolChoice) : undefined;
|
|
20
|
+
const params = {
|
|
21
|
+
model: req.model,
|
|
22
|
+
max_tokens: req.maxOutputTokens,
|
|
23
|
+
messages: renderTurns(req.turns),
|
|
24
|
+
...(system ? { system } : {}),
|
|
25
|
+
...(tools ? { tools } : {}),
|
|
26
|
+
...(toolChoice ? { tool_choice: toolChoice } : {}),
|
|
27
|
+
...(options.extraParams ?? {}),
|
|
28
|
+
};
|
|
29
|
+
const stream = getClient().messages.stream(params, opts?.signal ? { signal: opts.signal } : {});
|
|
30
|
+
return normalizeStream(stream, opts?.signal);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export { normalizeStream } from './stream.js';
|
|
35
|
+
export { renderSystem, renderToolChoice, renderTools, renderTurns } from './render.js';
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,eAAe,EAAmC,MAAM,aAAa,CAAC;AAkB/E,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,UAAoC,EAAE,EAAY,EAAE;IAC1F,4EAA4E;IAC5E,IAAI,MAAM,GAA0B,OAAO,CAAC,MAAM,CAAC;IACnD,MAAM,SAAS,GAAG,GAAc,EAAE;QAChC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,GAAG,IAAI,SAAS,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzF,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE,CACvB,OAAO,CAAC,cAAc,EAAE,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,oBAAoB,IAAI,OAAO;QAC5E,MAAM,EAAE,CAAC,GAAoB,EAAE,IAAI,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CA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-ai/sdk';
|
|
2
|
+
import type { ToolSpec, Turn } from '@thoughtminers/agent-runtime-core';
|
|
3
|
+
/**
|
|
4
|
+
* Render provider-neutral turns to the Anthropic Messages wire format.
|
|
5
|
+
* Contracts:
|
|
6
|
+
* - prompt caching: cache_control ephemeral goes on the LAST system block;
|
|
7
|
+
* - consecutive tool_result turns are grouped into ONE user message
|
|
8
|
+
* (the parallel-tool answer contract — the API rejects orphans).
|
|
9
|
+
*/
|
|
10
|
+
export declare const renderSystem: (system: string | undefined) => Anthropic.TextBlockParam[] | undefined;
|
|
11
|
+
export declare const renderTools: (tools: ToolSpec[] | undefined) => Anthropic.Tool[] | undefined;
|
|
12
|
+
export declare const renderToolChoice: (choice: "auto" | "any" | "none" | undefined) => Anthropic.ToolChoice | undefined;
|
|
13
|
+
export declare const renderTurns: (turns: Turn[]) => Anthropic.MessageParam[];
|
|
14
|
+
//# sourceMappingURL=render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAe,QAAQ,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AAErF;;;;;;GAMG;AAEH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,GAAG,SAAS,KAAG,SAAS,CAAC,cAAc,EAAE,GAAG,SAGX,CAAC;AAE7E,eAAO,MAAM,WAAW,GAAI,OAAO,QAAQ,EAAE,GAAG,SAAS,KAAG,SAAS,CAAC,IAAI,EAAE,GAAG,SAOtE,CAAC;AAEV,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,KAC1C,SAAS,CAAC,UAAU,GAAG,SAAkE,CAAC;AA+B7F,eAAO,MAAM,WAAW,GAAI,OAAO,IAAI,EAAE,KAAG,SAAS,CAAC,YAAY,EAgDjE,CAAC"}
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render provider-neutral turns to the Anthropic Messages wire format.
|
|
3
|
+
* Contracts:
|
|
4
|
+
* - prompt caching: cache_control ephemeral goes on the LAST system block;
|
|
5
|
+
* - consecutive tool_result turns are grouped into ONE user message
|
|
6
|
+
* (the parallel-tool answer contract — the API rejects orphans).
|
|
7
|
+
*/
|
|
8
|
+
export const renderSystem = (system) => system === undefined || system === ''
|
|
9
|
+
? undefined
|
|
10
|
+
: [{ type: 'text', text: system, cache_control: { type: 'ephemeral' } }];
|
|
11
|
+
export const renderTools = (tools) => tools === undefined || tools.length === 0
|
|
12
|
+
? undefined
|
|
13
|
+
: tools.map((t) => ({
|
|
14
|
+
name: t.name,
|
|
15
|
+
description: t.description,
|
|
16
|
+
input_schema: t.inputSchema,
|
|
17
|
+
}));
|
|
18
|
+
export const renderToolChoice = (choice) => (choice === undefined ? undefined : { type: choice });
|
|
19
|
+
const renderContentPart = (part) => {
|
|
20
|
+
if ('type' in part && part.type === 'text')
|
|
21
|
+
return { type: 'text', text: part.text };
|
|
22
|
+
if ('type' in part && part.type === 'image') {
|
|
23
|
+
const p = part;
|
|
24
|
+
return {
|
|
25
|
+
type: 'image',
|
|
26
|
+
source: { type: 'base64', media_type: p.mediaType, data: p.data },
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return part; // pass-through escape hatch
|
|
30
|
+
};
|
|
31
|
+
const renderUserContent = (content) => typeof content === 'string'
|
|
32
|
+
? content
|
|
33
|
+
: content.map(renderContentPart);
|
|
34
|
+
const toolResultBlock = (turn) => ({
|
|
35
|
+
type: 'tool_result',
|
|
36
|
+
tool_use_id: turn.toolUseId,
|
|
37
|
+
content: typeof turn.content === 'string'
|
|
38
|
+
? turn.content
|
|
39
|
+
: turn.content.map(renderContentPart),
|
|
40
|
+
...(turn.isError ? { is_error: true } : {}),
|
|
41
|
+
});
|
|
42
|
+
export const renderTurns = (turns) => {
|
|
43
|
+
const messages = [];
|
|
44
|
+
let i = 0;
|
|
45
|
+
while (i < turns.length) {
|
|
46
|
+
const turn = turns[i];
|
|
47
|
+
switch (turn.role) {
|
|
48
|
+
case 'system':
|
|
49
|
+
// System never travels in turns for this adapter — skip defensively.
|
|
50
|
+
i++;
|
|
51
|
+
break;
|
|
52
|
+
case 'user':
|
|
53
|
+
messages.push({ role: 'user', content: renderUserContent(turn.content) });
|
|
54
|
+
i++;
|
|
55
|
+
break;
|
|
56
|
+
case 'assistant': {
|
|
57
|
+
const blocks = [];
|
|
58
|
+
if (turn.content !== '')
|
|
59
|
+
blocks.push({ type: 'text', text: turn.content });
|
|
60
|
+
for (const call of turn.toolCalls ?? []) {
|
|
61
|
+
blocks.push({
|
|
62
|
+
type: 'tool_use',
|
|
63
|
+
id: call.id,
|
|
64
|
+
name: call.name,
|
|
65
|
+
input: call.input ?? {},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// An assistant turn with no text and no tool calls is unrepresentable
|
|
69
|
+
// on the wire — skip it.
|
|
70
|
+
if (blocks.length > 0)
|
|
71
|
+
messages.push({ role: 'assistant', content: blocks });
|
|
72
|
+
i++;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case 'tool_result': {
|
|
76
|
+
// Group the whole consecutive run into ONE user message.
|
|
77
|
+
const group = [];
|
|
78
|
+
while (i < turns.length && turns[i].role === 'tool_result') {
|
|
79
|
+
group.push(toolResultBlock(turns[i]));
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
messages.push({ role: 'user', content: group });
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return messages;
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AAEH,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AAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import type { ProviderEvent } from '@thoughtminers/agent-runtime-core';
|
|
3
|
+
/**
|
|
4
|
+
* The minimal structural surface this adapter needs from the SDK stream —
|
|
5
|
+
* lets tests inject a fake without depending on SDK internals.
|
|
6
|
+
*/
|
|
7
|
+
export interface AnthropicMessageStreamLike extends AsyncIterable<Anthropic.MessageStreamEvent> {
|
|
8
|
+
finalMessage(): Promise<Anthropic.Message>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Normalize the Anthropic SSE event stream to ProviderEvents.
|
|
12
|
+
* A plain pull-based async generator suffices — the harness owns the loop.
|
|
13
|
+
*/
|
|
14
|
+
export declare function normalizeStream(stream: AnthropicMessageStreamLike, signal?: AbortSignal): AsyncGenerator<ProviderEvent>;
|
|
15
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAqB,MAAM,mCAAmC,CAAC;AAoC1F;;;GAGG;AACH,MAAM,WAAW,0BAA2B,SAAQ,aAAa,CAAC,SAAS,CAAC,kBAAkB,CAAC;IAC7F,YAAY,IAAI,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;CAC5C;AAED;;;GAGG;AACH,wBAAuB,eAAe,CACpC,MAAM,EAAE,0BAA0B,EAClC,MAAM,CAAC,EAAE,WAAW,GACnB,cAAc,CAAC,aAAa,CAAC,CA8C/B"}
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const mapStopReason = (reason) => {
|
|
2
|
+
switch (reason) {
|
|
3
|
+
case 'end_turn':
|
|
4
|
+
return 'end_turn';
|
|
5
|
+
case 'tool_use':
|
|
6
|
+
return 'tool_use';
|
|
7
|
+
case 'max_tokens':
|
|
8
|
+
return 'max_tokens';
|
|
9
|
+
case 'stop_sequence':
|
|
10
|
+
return 'stop_sequence';
|
|
11
|
+
case 'refusal':
|
|
12
|
+
return 'refusal';
|
|
13
|
+
default:
|
|
14
|
+
return 'other';
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const mapUsage = (usage) => ({
|
|
18
|
+
inputTokens: usage.input_tokens,
|
|
19
|
+
outputTokens: usage.output_tokens,
|
|
20
|
+
...(usage.cache_read_input_tokens != null
|
|
21
|
+
? { cacheReadInputTokens: usage.cache_read_input_tokens }
|
|
22
|
+
: {}),
|
|
23
|
+
...(usage.cache_creation_input_tokens != null
|
|
24
|
+
? { cacheCreationInputTokens: usage.cache_creation_input_tokens }
|
|
25
|
+
: {}),
|
|
26
|
+
});
|
|
27
|
+
const abortError = () => {
|
|
28
|
+
const err = new Error('The operation was aborted');
|
|
29
|
+
err.name = 'AbortError';
|
|
30
|
+
return err;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Normalize the Anthropic SSE event stream to ProviderEvents.
|
|
34
|
+
* A plain pull-based async generator suffices — the harness owns the loop.
|
|
35
|
+
*/
|
|
36
|
+
export async function* normalizeStream(stream, signal) {
|
|
37
|
+
let inToolBlock = false;
|
|
38
|
+
try {
|
|
39
|
+
for await (const event of stream) {
|
|
40
|
+
switch (event.type) {
|
|
41
|
+
case 'content_block_start':
|
|
42
|
+
if (event.content_block.type === 'tool_use') {
|
|
43
|
+
inToolBlock = true;
|
|
44
|
+
yield {
|
|
45
|
+
type: 'tool_use_start',
|
|
46
|
+
id: event.content_block.id,
|
|
47
|
+
name: event.content_block.name,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
case 'content_block_delta':
|
|
52
|
+
if (event.delta.type === 'text_delta') {
|
|
53
|
+
yield { type: 'text_delta', text: event.delta.text };
|
|
54
|
+
}
|
|
55
|
+
else if (event.delta.type === 'input_json_delta') {
|
|
56
|
+
yield { type: 'tool_input_delta', json: event.delta.partial_json };
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'content_block_stop':
|
|
60
|
+
if (inToolBlock) {
|
|
61
|
+
inToolBlock = false;
|
|
62
|
+
yield { type: 'tool_use_end' };
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
default:
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const final = await stream.finalMessage();
|
|
70
|
+
yield {
|
|
71
|
+
type: 'message_done',
|
|
72
|
+
stopReason: mapStopReason(final.stop_reason),
|
|
73
|
+
usage: mapUsage(final.usage),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
// Normalize the SDK's APIUserAbortError (or any error observed after the
|
|
78
|
+
// caller aborted) to the harness AbortError contract.
|
|
79
|
+
if (signal?.aborted || (err instanceof Error && err.name === 'APIUserAbortError')) {
|
|
80
|
+
throw abortError();
|
|
81
|
+
}
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAGA,MAAM,aAAa,GAAG,CAAC,MAAqB,EAAc,EAAE;IAC1D,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,UAAU;YACb,OAAO,UAAU,CAAC;QACpB,KAAK,UAAU;YACb,OAAO,UAAU,CAAC;QACpB,KAAK,YAAY;YACf,OAAO,YAAY,CAAC;QACtB,KAAK,eAAe;YAClB,OAAO,eAAe,CAAC;QACzB,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;QACnB;YACE,OAAO,OAAO,CAAC;IACnB,CAAC;AACH,CAAC,C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AAC,IAAI,KAAK,YAAY,EAAE,CAAC;wBACtC,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;oBACvD,CAAC;yBAAM,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;wBACnD,MAAM,EAAE,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC;oBACrE,CAAC;oBACD,MAAM;gBACR,KAAK,oBAAoB;oBACvB,IAAI,WAAW,EAAE,CAAC;wBAChB,WAAW,GAAG,KAAK,CAAC;wBACpB,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;oBACjC,CAAC;oBACD,MAAM;gBACR;oBACE,MAAM;YACV,CAAC;QACH,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,YAAY,EAAE,CAAC;QAC1C,MAAM;YACJ,IAAI,EAAE,cAAc;YACpB,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,WAAW,CAAC;YAC5C,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC;SAC7B,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,yEAAyE;QACzE,sDAAsD;QACtD,IAAI,MAAM,EAAE,OAAO,IAAI,CAAC,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,IAAI,KAAK,mBAAmB,CAAC,EAAE,CAAC;YAClF,MAAM,UAAU,EAAE,CAAC;QACrB,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thoughtminers/agent-runtime-anthropic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Anthropic (Claude) provider adapter for @thoughtminers/agent-runtime-core.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/thoughtminers/agent-runtime-core.git",
|
|
9
|
+
"directory": "packages/provider-anthropic"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/thoughtminers/agent-runtime-core#readme",
|
|
12
|
+
"bugs": "https://github.com/thoughtminers/agent-runtime-core/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=24"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@thoughtminers/agent-runtime-core": "0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@anthropic-ai/sdk": ">=0.39.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@anthropic-ai/sdk": "^0.115.0",
|
|
37
|
+
"@types/node": "^24.0.0",
|
|
38
|
+
"typescript": "^5.8.0"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc --build",
|
|
42
|
+
"test": "node --test 'test/**/*.test.ts'"
|
|
43
|
+
}
|
|
44
|
+
}
|