@haikit/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 +17 -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/package.json +53 -0
- package/src/index.ts +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hai contributors
|
|
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,17 @@
|
|
|
1
|
+
# @haikit/anthropic
|
|
2
|
+
|
|
3
|
+
Model adapter for Claude.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { anthropic } from "@haikit/anthropic";
|
|
7
|
+
createHai({ model: anthropic({ model: "claude-opus-5", effort: "medium" }), … });
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Note it imports only `@haikit/core`, never `@haikit/server`. Adapters implement an
|
|
11
|
+
interface the contract layer declares — that is what keeps the runtime
|
|
12
|
+
swappable, and what lets an app ship its own adapter without forking anything
|
|
13
|
+
(see `examples/*/src/server/scripted.ts`).
|
|
14
|
+
|
|
15
|
+
The agent loop is hand-written rather than using the SDK's tool runner: an
|
|
16
|
+
`elicit` turn must stop mid-turn, return an HTTP response, and resume from a
|
|
17
|
+
*different request*. An in-process iterator cannot cross that boundary.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ModelAdapter } from "@haikit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Anthropic model adapter.
|
|
4
|
+
*
|
|
5
|
+
* Note it imports only hai-core, never hai-server. Adapters implement an
|
|
6
|
+
* interface the contract layer declares; that is what keeps the runtime
|
|
7
|
+
* swappable, and what lets an app ship its own adapter (see the scripted model
|
|
8
|
+
* in apps/flights) without forking the framework.
|
|
9
|
+
*
|
|
10
|
+
* The agent loop is hand-written rather than using the SDK's tool runner: an
|
|
11
|
+
* `elicit` turn must stop mid-turn, return an HTTP response, and resume from a
|
|
12
|
+
* *different request* minutes later. An in-process iterator cannot cross that.
|
|
13
|
+
*/
|
|
14
|
+
export interface AnthropicOptions {
|
|
15
|
+
model?: string;
|
|
16
|
+
maxTokens?: number;
|
|
17
|
+
effort?: "low" | "medium" | "high" | "xhigh" | "max";
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function anthropic(options?: AnthropicOptions): ModelAdapter;
|
|
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,KAAK,EAAE,YAAY,EAA+B,MAAM,cAAc,CAAC;AAE9E;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,YAAY,CAuCtE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function anthropic(options = {}) {
|
|
2
|
+
const model = options.model ?? "claude-opus-5";
|
|
3
|
+
let client = null;
|
|
4
|
+
return {
|
|
5
|
+
id: model,
|
|
6
|
+
async generate(request) {
|
|
7
|
+
if (!client) {
|
|
8
|
+
const { default: Anthropic } = await import("@anthropic-ai/sdk");
|
|
9
|
+
client = options.apiKey ? new Anthropic({ apiKey: options.apiKey }) : new Anthropic();
|
|
10
|
+
}
|
|
11
|
+
const stream = client.messages.stream({
|
|
12
|
+
model,
|
|
13
|
+
max_tokens: options.maxTokens ?? 16000,
|
|
14
|
+
output_config: { effort: options.effort ?? "medium" },
|
|
15
|
+
// Stable prefix, cached: prompt caching is a prefix match, so nothing
|
|
16
|
+
// volatile may go in here.
|
|
17
|
+
system: [{ type: "text", text: request.system, cache_control: { type: "ephemeral" } }],
|
|
18
|
+
tools: request.tools.map((t) => ({
|
|
19
|
+
name: t.name,
|
|
20
|
+
description: t.description,
|
|
21
|
+
input_schema: t.input_schema,
|
|
22
|
+
...(t.strict ? { strict: true } : {}),
|
|
23
|
+
})),
|
|
24
|
+
messages: request.messages,
|
|
25
|
+
});
|
|
26
|
+
for await (const event of stream) {
|
|
27
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
28
|
+
request.onTextDelta(event.delta.text);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const final = await stream.finalMessage();
|
|
32
|
+
return { content: final.content, stop_reason: final.stop_reason };
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAqBA,MAAM,UAAU,SAAS,CAAC,UAA4B,EAAE;IACtD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,eAAe,CAAC;IAC/C,IAAI,MAAM,GAAQ,IAAI,CAAC;IAEvB,OAAO;QACL,EAAE,EAAE,KAAK;QAET,KAAK,CAAC,QAAQ,CAAC,OAAqB;YAClC,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,mBAAmB,CAAC,CAAC;gBACjE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC;YACxF,CAAC;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;gBACpC,KAAK;gBACL,UAAU,EAAE,OAAO,CAAC,SAAS,IAAI,KAAK;gBACtC,aAAa,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,QAAQ,EAAE;gBACrD,sEAAsE;gBACtE,2BAA2B;gBAC3B,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,CAAC;gBACtF,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;oBAC/B,IAAI,EAAE,CAAC,CAAC,IAAI;oBACZ,WAAW,EAAE,CAAC,CAAC,WAAW;oBAC1B,YAAY,EAAE,CAAC,CAAC,YAAY;oBAC5B,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;iBACtC,CAAC,CAAC;gBACH,QAAQ,EAAE,OAAO,CAAC,QAAQ;aAC3B,CAAC,CAAC;YAEH,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBACjC,IAAI,KAAK,CAAC,IAAI,KAAK,qBAAqB,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;oBAC9E,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACxC,CAAC;YACH,CAAC;YAED,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,YAAY,EAAE,CAAC;YAC1C,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC;QACpE,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haikit/anthropic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Claude model adapter for haikit. Imports only @haikit/core, never the server runtime.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"haikit",
|
|
7
|
+
"claude",
|
|
8
|
+
"anthropic",
|
|
9
|
+
"llm",
|
|
10
|
+
"agent",
|
|
11
|
+
"agentic-ui",
|
|
12
|
+
"tool-use",
|
|
13
|
+
"ai"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"prepack": "npm --prefix ../.. run build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@anthropic-ai/sdk": "^0.70.0",
|
|
38
|
+
"@haikit/core": "0.1.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"homepage": "https://github.com/wfoxd/haikit/tree/main/packages/anthropic#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/wfoxd/haikit/issues"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/wfoxd/haikit.git",
|
|
51
|
+
"directory": "packages/anthropic"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ModelAdapter, ModelRequest, ModelResponse } from "@haikit/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic model adapter.
|
|
5
|
+
*
|
|
6
|
+
* Note it imports only hai-core, never hai-server. Adapters implement an
|
|
7
|
+
* interface the contract layer declares; that is what keeps the runtime
|
|
8
|
+
* swappable, and what lets an app ship its own adapter (see the scripted model
|
|
9
|
+
* in apps/flights) without forking the framework.
|
|
10
|
+
*
|
|
11
|
+
* The agent loop is hand-written rather than using the SDK's tool runner: an
|
|
12
|
+
* `elicit` turn must stop mid-turn, return an HTTP response, and resume from a
|
|
13
|
+
* *different request* minutes later. An in-process iterator cannot cross that.
|
|
14
|
+
*/
|
|
15
|
+
export interface AnthropicOptions {
|
|
16
|
+
model?: string;
|
|
17
|
+
maxTokens?: number;
|
|
18
|
+
effort?: "low" | "medium" | "high" | "xhigh" | "max";
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function anthropic(options: AnthropicOptions = {}): ModelAdapter {
|
|
23
|
+
const model = options.model ?? "claude-opus-5";
|
|
24
|
+
let client: any = null;
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
id: model,
|
|
28
|
+
|
|
29
|
+
async generate(request: ModelRequest): Promise<ModelResponse> {
|
|
30
|
+
if (!client) {
|
|
31
|
+
const { default: Anthropic } = await import("@anthropic-ai/sdk");
|
|
32
|
+
client = options.apiKey ? new Anthropic({ apiKey: options.apiKey }) : new Anthropic();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const stream = client.messages.stream({
|
|
36
|
+
model,
|
|
37
|
+
max_tokens: options.maxTokens ?? 16000,
|
|
38
|
+
output_config: { effort: options.effort ?? "medium" },
|
|
39
|
+
// Stable prefix, cached: prompt caching is a prefix match, so nothing
|
|
40
|
+
// volatile may go in here.
|
|
41
|
+
system: [{ type: "text", text: request.system, cache_control: { type: "ephemeral" } }],
|
|
42
|
+
tools: request.tools.map((t) => ({
|
|
43
|
+
name: t.name,
|
|
44
|
+
description: t.description,
|
|
45
|
+
input_schema: t.input_schema,
|
|
46
|
+
...(t.strict ? { strict: true } : {}),
|
|
47
|
+
})),
|
|
48
|
+
messages: request.messages,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
for await (const event of stream) {
|
|
52
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
53
|
+
request.onTextDelta(event.delta.text);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const final = await stream.finalMessage();
|
|
58
|
+
return { content: final.content, stop_reason: final.stop_reason };
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|