@netbirdio/explain 0.1.3 → 0.1.6
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/dist/server/handler.d.ts +2 -1
- package/dist/server/handler.d.ts.map +1 -1
- package/dist/server/handler.js +1 -1
- package/dist/server/handler.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/providers/anthropic.d.ts +2 -2
- package/dist/server/providers/anthropic.d.ts.map +1 -1
- package/dist/server/providers/anthropic.js +76 -27
- package/dist/server/providers/anthropic.js.map +1 -1
- package/dist/server/providers/types.d.ts +7 -1
- package/dist/server/providers/types.d.ts.map +1 -1
- package/package.json +13 -5
- package/src/server/handler.ts +4 -2
- package/src/server/index.ts +1 -1
- package/src/server/providers/anthropic.ts +128 -27
- package/src/server/providers/types.ts +15 -1
package/dist/server/handler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Message } from "../types";
|
|
2
|
-
import type { LLMProvider, Middleware } from "./providers/types";
|
|
2
|
+
import type { LLMProvider, Middleware, Tool } from "./providers/types";
|
|
3
3
|
export type AssistantConfig = {
|
|
4
4
|
provider: "anthropic" | "openai" | "dify" | LLMProvider;
|
|
5
5
|
apiKey?: string;
|
|
@@ -7,6 +7,7 @@ export type AssistantConfig = {
|
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
systemPrompt?: string;
|
|
9
9
|
middleware?: Middleware[];
|
|
10
|
+
tools?: Tool[];
|
|
10
11
|
};
|
|
11
12
|
type HandlerOptions = {
|
|
12
13
|
apiKey?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAIxC,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAA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AI,CAAC,gBAAgB,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9C,SAAS,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;CACxE;AA2ED,wBAAgB,eAAe,CAAC,MAAM,EAAE,eAAe;gBAI5B,WAAW,KAAG,OAAO,CAAC,YAAY,CAAC;qBAkBpC,cAAc,MACtB,KAAK,eAAe,EAAE,KAAK,gBAAgB,KAAG,OAAO,CAAC,IAAI,CAAC;EAwC5E"}
|
package/dist/server/handler.js
CHANGED
|
@@ -87,7 +87,7 @@ export function createAssistant(config) {
|
|
|
87
87
|
messages = result.messages;
|
|
88
88
|
systemPrompt = result.systemPrompt;
|
|
89
89
|
}
|
|
90
|
-
const reply = await provider.chat(messages, systemPrompt);
|
|
90
|
+
const reply = await provider.chat(messages, systemPrompt, config.tools);
|
|
91
91
|
return { reply };
|
|
92
92
|
}
|
|
93
93
|
function handler(opts) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,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AO,EAAE,CAAC,CAAC;YACzC,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC3B,CAAC"}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export type { AssistantConfig } from "./handler";
|
|
|
3
3
|
export { AnthropicProvider } from "./providers/anthropic";
|
|
4
4
|
export { DifyProvider } from "./providers/dify";
|
|
5
5
|
export { OpenAIProvider } from "./providers/openai";
|
|
6
|
-
export type { LLMProvider, ProviderConfig, Middleware } from "./providers/types";
|
|
6
|
+
export type { LLMProvider, ProviderConfig, Middleware, Tool } from "./providers/types";
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC5C,YAAY,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Message } from "../../types";
|
|
2
|
-
import type { LLMProvider, ProviderConfig } from "./types";
|
|
2
|
+
import type { LLMProvider, ProviderConfig, Tool } from "./types";
|
|
3
3
|
export declare class AnthropicProvider implements LLMProvider {
|
|
4
4
|
private apiKey;
|
|
5
5
|
private model;
|
|
6
6
|
constructor(config: ProviderConfig);
|
|
7
|
-
chat(messages: Message[], systemPrompt?: string): Promise<string>;
|
|
7
|
+
chat(messages: Message[], systemPrompt?: string, tools?: Tool[]): Promise<string>;
|
|
8
8
|
}
|
|
9
9
|
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/server/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/server/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAYjE,qBAAa,iBAAkB,YAAW,WAAW;IACnD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAS;gBAEV,MAAM,EAAE,cAAc;IAK5B,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;CAoGxF"}
|
|
@@ -3,38 +3,87 @@ export class AnthropicProvider {
|
|
|
3
3
|
this.apiKey = config.apiKey;
|
|
4
4
|
this.model = config.model || "claude-sonnet-4-20250514";
|
|
5
5
|
}
|
|
6
|
-
async chat(messages, systemPrompt) {
|
|
6
|
+
async chat(messages, systemPrompt, tools) {
|
|
7
7
|
const anthropicMessages = messages.map((m) => ({
|
|
8
8
|
role: m.role === "context" || m.role === "system" ? "user" : m.role,
|
|
9
9
|
content: m.role === "context" ? `[Context]: ${m.content}` : m.content,
|
|
10
10
|
}));
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
11
|
+
const toolDefs = tools?.map((t) => ({
|
|
12
|
+
name: t.name,
|
|
13
|
+
description: t.description,
|
|
14
|
+
input_schema: t.input_schema,
|
|
15
|
+
}));
|
|
16
|
+
const toolsByName = new Map(tools?.map((t) => [t.name, t]));
|
|
17
|
+
let lastTextResponse = "";
|
|
18
|
+
// eslint-disable-next-line no-constant-condition
|
|
19
|
+
while (true) {
|
|
20
|
+
const body = {
|
|
21
|
+
model: this.model,
|
|
22
|
+
max_tokens: 4096,
|
|
23
|
+
messages: anthropicMessages,
|
|
24
|
+
};
|
|
25
|
+
if (systemPrompt) {
|
|
26
|
+
body.system = systemPrompt;
|
|
27
|
+
}
|
|
28
|
+
if (toolDefs && toolDefs.length > 0) {
|
|
29
|
+
body.tools = toolDefs;
|
|
30
|
+
}
|
|
31
|
+
const response = await fetch("https://api.anthropic.com/v1/messages", {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: {
|
|
34
|
+
"Content-Type": "application/json",
|
|
35
|
+
"x-api-key": this.apiKey,
|
|
36
|
+
"anthropic-version": "2023-06-01",
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify(body),
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const text = await response.text();
|
|
42
|
+
throw new Error(`Anthropic API error ${response.status}: ${text}`);
|
|
43
|
+
}
|
|
44
|
+
const data = await response.json();
|
|
45
|
+
const textBlock = data.content?.find((block) => block.type === "text");
|
|
46
|
+
if (textBlock) {
|
|
47
|
+
lastTextResponse = textBlock.text;
|
|
48
|
+
}
|
|
49
|
+
if (data.stop_reason !== "tool_use") {
|
|
50
|
+
if (!lastTextResponse) {
|
|
51
|
+
throw new Error("No text content in Anthropic response");
|
|
52
|
+
}
|
|
53
|
+
return lastTextResponse;
|
|
54
|
+
}
|
|
55
|
+
// Handle tool calls
|
|
56
|
+
const toolUseBlocks = data.content.filter((block) => block.type === "tool_use");
|
|
57
|
+
anthropicMessages.push({ role: "assistant", content: data.content });
|
|
58
|
+
const toolResults = [];
|
|
59
|
+
for (const toolUse of toolUseBlocks) {
|
|
60
|
+
const tool = toolsByName.get(toolUse.name);
|
|
61
|
+
if (!tool) {
|
|
62
|
+
toolResults.push({
|
|
63
|
+
type: "tool_result",
|
|
64
|
+
tool_use_id: toolUse.id,
|
|
65
|
+
content: `Error: unknown tool "${toolUse.name}"`,
|
|
66
|
+
});
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const result = await tool.execute(toolUse.input);
|
|
71
|
+
toolResults.push({
|
|
72
|
+
type: "tool_result",
|
|
73
|
+
tool_use_id: toolUse.id,
|
|
74
|
+
content: result,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
toolResults.push({
|
|
79
|
+
type: "tool_result",
|
|
80
|
+
tool_use_id: toolUse.id,
|
|
81
|
+
content: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
anthropicMessages.push({ role: "user", content: toolResults });
|
|
36
86
|
}
|
|
37
|
-
return textBlock.text;
|
|
38
87
|
}
|
|
39
88
|
}
|
|
40
89
|
//# sourceMappingURL=anthropic.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/server/providers/anthropic.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/server/providers/anthropic.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,iBAAiB;IAI5B,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,0BAA0B,CAAC;IAC1D,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,QAAmB,EAAE,YAAqB,EAAE,KAAc;QACnE,MAAM,iBAAiB,GAAuB,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YACjE,IAAI,EAAE,CAAC,CAAC,IAAI,KAAK,SAAS,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAE,MAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;YAC9E,OAAO,EAAE,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO;SACtE,CAAC,CAAC,CAAC;QAEJ,MAAM,QAAQ,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAClC,IAAI,EAAE,CAAC,CAAC,IAAI;YACZ,WAAW,EAAE,CAAC,CAAC,WAAW;YAC1B,YAAY,EAAE,CAAC,CAAC,YAAY;SAC7B,CAAC,CAAC,CAAC;QAEJ,MAAM,WAAW,GAAG,IAAI,GAA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I,CAAC;wBACf,IAAI,EAAE,aAAa;wBACnB,WAAW,EAAE,OAAO,CAAC,EAAE;wBACvB,OAAO,EAAE,UAAU,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;qBACtE,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YAED,iBAAiB,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Message } from "../../types";
|
|
2
|
+
export type Tool = {
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
input_schema: Record<string, unknown>;
|
|
6
|
+
execute: (input: Record<string, unknown>) => Promise<string>;
|
|
7
|
+
};
|
|
2
8
|
export interface LLMProvider {
|
|
3
|
-
chat(messages: Message[], systemPrompt?: string): Promise<string>;
|
|
9
|
+
chat(messages: Message[], systemPrompt?: string, tools?: Tool[]): Promise<string>;
|
|
4
10
|
}
|
|
5
11
|
export type ProviderConfig = {
|
|
6
12
|
apiKey: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/server/providers/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/server/providers/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACtC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC9D,CAAC;AAEF,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACnF;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,CACvB,QAAQ,EAAE,OAAO,EAAE,EACnB,YAAY,CAAC,EAAE,MAAM,KAClB,OAAO,CAAC;IAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netbirdio/explain",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Full-stack AI assistant library with React frontend components and Node.js backend handler",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"main": "./dist/client/index.js",
|
|
@@ -17,11 +17,18 @@
|
|
|
17
17
|
},
|
|
18
18
|
"typesVersions": {
|
|
19
19
|
"*": {
|
|
20
|
-
"client": [
|
|
21
|
-
|
|
20
|
+
"client": [
|
|
21
|
+
"./dist/client/index.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"server": [
|
|
24
|
+
"./dist/server/index.d.ts"
|
|
25
|
+
]
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
|
-
"files": [
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
25
32
|
"scripts": {
|
|
26
33
|
"build": "tsc",
|
|
27
34
|
"typecheck": "tsc --noEmit"
|
|
@@ -31,6 +38,7 @@
|
|
|
31
38
|
"react-dom": ">=18.0.0"
|
|
32
39
|
},
|
|
33
40
|
"dependencies": {
|
|
41
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
34
42
|
"lucide-react": ">=0.300.0"
|
|
35
43
|
},
|
|
36
44
|
"repository": {
|
|
@@ -42,4 +50,4 @@
|
|
|
42
50
|
"@types/react-dom": "^18.0.0",
|
|
43
51
|
"typescript": "^5.0.0"
|
|
44
52
|
}
|
|
45
|
-
}
|
|
53
|
+
}
|
package/src/server/handler.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { Message } from "../types";
|
|
|
2
2
|
import { AnthropicProvider } from "./providers/anthropic";
|
|
3
3
|
import { DifyProvider } from "./providers/dify";
|
|
4
4
|
import { OpenAIProvider } from "./providers/openai";
|
|
5
|
-
import type { LLMProvider, Middleware } from "./providers/types";
|
|
5
|
+
import type { LLMProvider, McpServer, Middleware, Tool } from "./providers/types";
|
|
6
6
|
|
|
7
7
|
export type AssistantConfig = {
|
|
8
8
|
provider: "anthropic" | "openai" | "dify" | LLMProvider;
|
|
@@ -11,6 +11,8 @@ export type AssistantConfig = {
|
|
|
11
11
|
baseUrl?: string;
|
|
12
12
|
systemPrompt?: string;
|
|
13
13
|
middleware?: Middleware[];
|
|
14
|
+
tools?: Tool[];
|
|
15
|
+
mcpServers?: McpServer[];
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
type HandlerOptions = {
|
|
@@ -132,7 +134,7 @@ export function createAssistant(config: AssistantConfig) {
|
|
|
132
134
|
systemPrompt = result.systemPrompt;
|
|
133
135
|
}
|
|
134
136
|
|
|
135
|
-
const reply = await provider.chat(messages, systemPrompt);
|
|
137
|
+
const reply = await provider.chat(messages, systemPrompt, config.tools, config.mcpServers);
|
|
136
138
|
return { reply };
|
|
137
139
|
}
|
|
138
140
|
|
package/src/server/index.ts
CHANGED
|
@@ -3,4 +3,4 @@ export type { AssistantConfig } from "./handler";
|
|
|
3
3
|
export { AnthropicProvider } from "./providers/anthropic";
|
|
4
4
|
export { DifyProvider } from "./providers/dify";
|
|
5
5
|
export { OpenAIProvider } from "./providers/openai";
|
|
6
|
-
export type { LLMProvider, ProviderConfig, Middleware } from "./providers/types";
|
|
6
|
+
export type { LLMProvider, McpServer, ProviderConfig, Middleware, Tool } from "./providers/types";
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { Message } from "../../types";
|
|
2
|
-
import type { LLMProvider, ProviderConfig } from "./types";
|
|
2
|
+
import type { LLMProvider, McpServer, ProviderConfig, Tool } from "./types";
|
|
3
|
+
|
|
4
|
+
type AnthropicMessage = {
|
|
5
|
+
role: "user" | "assistant";
|
|
6
|
+
content: string | AnthropicContentBlock[];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type AnthropicContentBlock =
|
|
10
|
+
| { type: "text"; text: string }
|
|
11
|
+
| { type: "tool_use"; id: string; name: string; input: Record<string, unknown> }
|
|
12
|
+
| { type: "tool_result"; tool_use_id: string; content: string };
|
|
3
13
|
|
|
4
14
|
export class AnthropicProvider implements LLMProvider {
|
|
5
15
|
private apiKey: string;
|
|
@@ -10,44 +20,135 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
10
20
|
this.model = config.model || "claude-sonnet-4-20250514";
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
async chat(messages: Message[], systemPrompt?: string): Promise<string> {
|
|
14
|
-
const anthropicMessages = messages.map((m) => ({
|
|
23
|
+
async chat(messages: Message[], systemPrompt?: string, tools?: Tool[], mcpServers?: McpServer[]): Promise<string> {
|
|
24
|
+
const anthropicMessages: AnthropicMessage[] = messages.map((m) => ({
|
|
15
25
|
role: m.role === "context" || m.role === "system" ? ("user" as const) : m.role,
|
|
16
26
|
content: m.role === "context" ? `[Context]: ${m.content}` : m.content,
|
|
17
27
|
}));
|
|
18
28
|
|
|
19
|
-
const
|
|
20
|
-
model: this.model,
|
|
21
|
-
max_tokens: 4096,
|
|
22
|
-
messages: anthropicMessages,
|
|
23
|
-
};
|
|
29
|
+
const toolDefs: Record<string, unknown>[] = [];
|
|
24
30
|
|
|
25
|
-
if (
|
|
26
|
-
|
|
31
|
+
if (tools?.length) {
|
|
32
|
+
for (const t of tools) {
|
|
33
|
+
toolDefs.push({
|
|
34
|
+
name: t.name,
|
|
35
|
+
description: t.description,
|
|
36
|
+
input_schema: t.input_schema,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
27
39
|
}
|
|
28
40
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
41
|
+
if (mcpServers?.length) {
|
|
42
|
+
for (const mcp of mcpServers) {
|
|
43
|
+
const mcpTool: Record<string, unknown> = {
|
|
44
|
+
type: "mcp",
|
|
45
|
+
server_label: mcp.server_label,
|
|
46
|
+
server_url: mcp.server_url,
|
|
47
|
+
};
|
|
48
|
+
if (mcp.headers) mcpTool.headers = mcp.headers;
|
|
49
|
+
if (mcp.allowed_tools) mcpTool.allowed_tools = mcp.allowed_tools;
|
|
50
|
+
toolDefs.push(mcpTool);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const toolsByName = new Map(tools?.map((t) => [t.name, t]));
|
|
55
|
+
|
|
56
|
+
let lastTextResponse = "";
|
|
57
|
+
|
|
58
|
+
// eslint-disable-next-line no-constant-condition
|
|
59
|
+
while (true) {
|
|
60
|
+
const body: Record<string, unknown> = {
|
|
61
|
+
model: this.model,
|
|
62
|
+
max_tokens: 4096,
|
|
63
|
+
messages: anthropicMessages,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if (systemPrompt) {
|
|
67
|
+
body.system = systemPrompt;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (toolDefs && toolDefs.length > 0) {
|
|
71
|
+
body.tools = toolDefs;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const headers: Record<string, string> = {
|
|
32
75
|
"Content-Type": "application/json",
|
|
33
76
|
"x-api-key": this.apiKey,
|
|
34
77
|
"anthropic-version": "2023-06-01",
|
|
35
|
-
}
|
|
36
|
-
body: JSON.stringify(body),
|
|
37
|
-
});
|
|
78
|
+
};
|
|
38
79
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
80
|
+
if (mcpServers?.length) {
|
|
81
|
+
headers["anthropic-beta"] = "mcp-client-2025-04-04";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const response = await fetch("https://api.anthropic.com/v1/messages", {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers,
|
|
87
|
+
body: JSON.stringify(body),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
const text = await response.text();
|
|
92
|
+
throw new Error(`Anthropic API error ${response.status}: ${text}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const data = await response.json();
|
|
96
|
+
|
|
97
|
+
const textBlock = data.content?.find(
|
|
98
|
+
(block: { type: string }) => block.type === "text",
|
|
99
|
+
);
|
|
100
|
+
if (textBlock) {
|
|
101
|
+
lastTextResponse = textBlock.text;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// MCP server-side tools hit iteration limit — re-send to continue
|
|
105
|
+
if (data.stop_reason === "pause_turn") {
|
|
106
|
+
anthropicMessages.push({ role: "assistant", content: data.content });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (data.stop_reason !== "tool_use") {
|
|
111
|
+
if (!lastTextResponse) {
|
|
112
|
+
throw new Error("No text content in Anthropic response");
|
|
113
|
+
}
|
|
114
|
+
return lastTextResponse;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Handle client-side tool calls
|
|
118
|
+
const toolUseBlocks = data.content.filter(
|
|
119
|
+
(block: { type: string }) => block.type === "tool_use",
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
anthropicMessages.push({ role: "assistant", content: data.content });
|
|
123
|
+
|
|
124
|
+
const toolResults: AnthropicContentBlock[] = [];
|
|
125
|
+
for (const toolUse of toolUseBlocks) {
|
|
126
|
+
const tool = toolsByName.get(toolUse.name);
|
|
127
|
+
if (!tool) {
|
|
128
|
+
toolResults.push({
|
|
129
|
+
type: "tool_result",
|
|
130
|
+
tool_use_id: toolUse.id,
|
|
131
|
+
content: `Error: unknown tool "${toolUse.name}"`,
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
const result = await tool.execute(toolUse.input);
|
|
137
|
+
toolResults.push({
|
|
138
|
+
type: "tool_result",
|
|
139
|
+
tool_use_id: toolUse.id,
|
|
140
|
+
content: result,
|
|
141
|
+
});
|
|
142
|
+
} catch (err) {
|
|
143
|
+
toolResults.push({
|
|
144
|
+
type: "tool_result",
|
|
145
|
+
tool_use_id: toolUse.id,
|
|
146
|
+
content: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
43
150
|
|
|
44
|
-
|
|
45
|
-
const textBlock = data.content?.find(
|
|
46
|
-
(block: { type: string }) => block.type === "text",
|
|
47
|
-
);
|
|
48
|
-
if (!textBlock) {
|
|
49
|
-
throw new Error("No text content in Anthropic response");
|
|
151
|
+
anthropicMessages.push({ role: "user", content: toolResults });
|
|
50
152
|
}
|
|
51
|
-
return textBlock.text;
|
|
52
153
|
}
|
|
53
154
|
}
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import type { Message } from "../../types";
|
|
2
2
|
|
|
3
|
+
export type Tool = {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
input_schema: Record<string, unknown>;
|
|
7
|
+
execute: (input: Record<string, unknown>) => Promise<string>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type McpServer = {
|
|
11
|
+
server_label: string;
|
|
12
|
+
server_url: string;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
allowed_tools?: string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
3
17
|
export interface LLMProvider {
|
|
4
|
-
chat(messages: Message[], systemPrompt?: string): Promise<string>;
|
|
18
|
+
chat(messages: Message[], systemPrompt?: string, tools?: Tool[], mcpServers?: McpServer[]): Promise<string>;
|
|
5
19
|
}
|
|
6
20
|
|
|
7
21
|
export type ProviderConfig = {
|