@luanpoppe/ai 1.0.5 → 1.0.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/.env.example +10 -0
- package/dist/@types/model-names.d.ts +4 -1
- package/dist/@types/model-names.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/langchain/models.d.ts +4 -0
- package/dist/langchain/models.d.ts.map +1 -1
- package/dist/langchain/models.js +17 -0
- package/dist/langchain/models.js.map +1 -1
- package/dist/langchain/tools.d.ts +1 -1
- package/package.json +19 -9
- package/src/@types/model-names.ts +17 -1
- package/src/index.ts +10 -0
- package/src/langchain/models.ts +22 -0
- package/tests/e2e/README.md +47 -0
- package/tests/e2e/langchain.test.ts +477 -0
- package/tests/tsconfig.json +17 -0
- package/tests/unit/index.test.ts +355 -0
- package/tests/unit/langchain/messages.test.ts +101 -0
- package/tests/unit/langchain/models.test.ts +192 -0
- package/tests/unit/langchain/tools.test.ts +134 -0
- package/vitest.config.ts +24 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { LangchainTools, CreateToolParams } from "../../../src/langchain/tools";
|
|
2
|
+
import { tool } from "langchain";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
|
|
5
|
+
// Mock do langchain
|
|
6
|
+
vi.mock("langchain", () => ({
|
|
7
|
+
tool: vi.fn(),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
describe("LangchainTools", () => {
|
|
11
|
+
let langchainTools: LangchainTools;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
vi.clearAllMocks();
|
|
15
|
+
langchainTools = new LangchainTools();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("createTool", () => {
|
|
19
|
+
it("deve criar uma ferramenta com todos os parâmetros fornecidos", () => {
|
|
20
|
+
const toolFunction = async (input: { query: string }) => {
|
|
21
|
+
return `Resultado para: ${input.query}`;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const params: CreateToolParams = {
|
|
25
|
+
toolFunction,
|
|
26
|
+
name: "search_tool",
|
|
27
|
+
description: "Uma ferramenta de busca",
|
|
28
|
+
schema: z.object({
|
|
29
|
+
query: z.string(),
|
|
30
|
+
}),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const mockTool = {} as any;
|
|
34
|
+
vi.mocked(tool).mockReturnValue(mockTool);
|
|
35
|
+
|
|
36
|
+
const result = langchainTools.createTool(params);
|
|
37
|
+
|
|
38
|
+
expect(tool).toHaveBeenCalledWith(toolFunction, {
|
|
39
|
+
name: "search_tool",
|
|
40
|
+
description: "Uma ferramenta de busca",
|
|
41
|
+
schema: params.schema,
|
|
42
|
+
});
|
|
43
|
+
expect(result).toBe(mockTool);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("deve criar uma ferramenta com schema complexo", () => {
|
|
47
|
+
const toolFunction = async (input: {
|
|
48
|
+
name: string;
|
|
49
|
+
age: number;
|
|
50
|
+
email: string;
|
|
51
|
+
}) => {
|
|
52
|
+
return `Usuário: ${input.name}, ${input.age} anos, ${input.email}`;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const params: CreateToolParams = {
|
|
56
|
+
toolFunction,
|
|
57
|
+
name: "user_tool",
|
|
58
|
+
description: "Ferramenta para processar usuários",
|
|
59
|
+
schema: z.object({
|
|
60
|
+
name: z.string(),
|
|
61
|
+
age: z.number(),
|
|
62
|
+
email: z.string().email(),
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const mockTool = {} as any;
|
|
67
|
+
vi.mocked(tool).mockReturnValue(mockTool);
|
|
68
|
+
|
|
69
|
+
const result = langchainTools.createTool(params);
|
|
70
|
+
|
|
71
|
+
expect(tool).toHaveBeenCalledWith(toolFunction, {
|
|
72
|
+
name: "user_tool",
|
|
73
|
+
description: "Ferramenta para processar usuários",
|
|
74
|
+
schema: params.schema,
|
|
75
|
+
});
|
|
76
|
+
expect(result).toBe(mockTool);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("deve criar uma ferramenta com schema simples", () => {
|
|
80
|
+
const toolFunction = async () => {
|
|
81
|
+
return "Resultado simples";
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const params: CreateToolParams = {
|
|
85
|
+
toolFunction,
|
|
86
|
+
name: "simple_tool",
|
|
87
|
+
description: "Ferramenta simples",
|
|
88
|
+
schema: z.object({}),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const mockTool = {} as any;
|
|
92
|
+
vi.mocked(tool).mockReturnValue(mockTool);
|
|
93
|
+
|
|
94
|
+
const result = langchainTools.createTool(params);
|
|
95
|
+
|
|
96
|
+
expect(tool).toHaveBeenCalledWith(toolFunction, {
|
|
97
|
+
name: "simple_tool",
|
|
98
|
+
description: "Ferramenta simples",
|
|
99
|
+
schema: params.schema,
|
|
100
|
+
});
|
|
101
|
+
expect(result).toBe(mockTool);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("deve criar múltiplas ferramentas independentes", () => {
|
|
105
|
+
const toolFunction1 = async (input: { x: number }) => input.x * 2;
|
|
106
|
+
const toolFunction2 = async (input: { y: string }) => y.toUpperCase();
|
|
107
|
+
|
|
108
|
+
const params1: CreateToolParams = {
|
|
109
|
+
toolFunction: toolFunction1,
|
|
110
|
+
name: "multiply",
|
|
111
|
+
description: "Multiplica por 2",
|
|
112
|
+
schema: z.object({ x: z.number() }),
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const params2: CreateToolParams = {
|
|
116
|
+
toolFunction: toolFunction2,
|
|
117
|
+
name: "uppercase",
|
|
118
|
+
description: "Converte para maiúsculas",
|
|
119
|
+
schema: z.object({ y: z.string() }),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const mockTool1 = {} as any;
|
|
123
|
+
const mockTool2 = {} as any;
|
|
124
|
+
vi.mocked(tool).mockReturnValueOnce(mockTool1).mockReturnValueOnce(mockTool2);
|
|
125
|
+
|
|
126
|
+
const result1 = langchainTools.createTool(params1);
|
|
127
|
+
const result2 = langchainTools.createTool(params2);
|
|
128
|
+
|
|
129
|
+
expect(tool).toHaveBeenCalledTimes(2);
|
|
130
|
+
expect(result1).toBe(mockTool1);
|
|
131
|
+
expect(result2).toBe(mockTool2);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { config } from "dotenv";
|
|
4
|
+
|
|
5
|
+
// Carrega o arquivo .env da raiz do pacote
|
|
6
|
+
config({ path: resolve(__dirname, ".env") });
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
test: {
|
|
10
|
+
globals: true,
|
|
11
|
+
environment: "node",
|
|
12
|
+
include: ["tests/**/*.test.ts"],
|
|
13
|
+
coverage: {
|
|
14
|
+
provider: "v8",
|
|
15
|
+
reporter: ["text", "json", "html"],
|
|
16
|
+
exclude: [
|
|
17
|
+
"node_modules/",
|
|
18
|
+
"dist/",
|
|
19
|
+
"**/*.config.*",
|
|
20
|
+
"**/tests/**",
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
});
|