actient 1.0.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 +7 -0
- package/README.md +165 -0
- package/dist/chunk-UNGR3PLJ.js +72 -0
- package/dist/chunk-UNGR3PLJ.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +86 -0
- package/dist/index.js.map +1 -0
- package/dist/intent-parser-tPkUA2YT.d.ts +16 -0
- package/dist/providers/gemini.d.ts +16 -0
- package/dist/providers/gemini.js +51 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/groq.d.ts +16 -0
- package/dist/providers/groq.js +47 -0
- package/dist/providers/groq.js.map +1 -0
- package/dist/providers/openai.d.ts +16 -0
- package/dist/providers/openai.js +46 -0
- package/dist/providers/openai.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright © 2026 <copyright holders>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Actient
|
|
2
|
+
|
|
3
|
+
Connect and run your functions with your favorite AI easier.
|
|
4
|
+
|
|
5
|
+
### In this page
|
|
6
|
+
|
|
7
|
+
- [Get Started](#get-started)
|
|
8
|
+
- [How it's work](#how-its-work)
|
|
9
|
+
- [References](#references)
|
|
10
|
+
- [Supported SDK](#our-supported-sdk)
|
|
11
|
+
- [AI Agent](#ai-agent)
|
|
12
|
+
- [registerAction()](#registeraction)
|
|
13
|
+
- [execute()](#execute)
|
|
14
|
+
- [Best Practices](#best-practices)
|
|
15
|
+
- [Unknown action](#unknown-action)
|
|
16
|
+
- [Specific rules](#specific-rules)
|
|
17
|
+
- [Typescript Support](#typescript-support)
|
|
18
|
+
|
|
19
|
+
## Get started
|
|
20
|
+
|
|
21
|
+
Install package:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install actient zod openai
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## How it's work?
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { AIAgent } from "actient";
|
|
31
|
+
import { OpenAIIntentParser } from "actient/providers/openai";
|
|
32
|
+
import { z } from "zod";
|
|
33
|
+
|
|
34
|
+
// define ai agent
|
|
35
|
+
const agent = new AIAgent({
|
|
36
|
+
ai: new OpenAIIntentParser({
|
|
37
|
+
apiKey: "your_api_key",
|
|
38
|
+
model: "ai-model",
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// register your function as action
|
|
43
|
+
agent.registerAction("increase_stock", {
|
|
44
|
+
description: "Add product stock",
|
|
45
|
+
schema: z.object({
|
|
46
|
+
name: z.string(),
|
|
47
|
+
amount: z.number().positive(),
|
|
48
|
+
}),
|
|
49
|
+
handler: async ({ name, amount }) => {
|
|
50
|
+
// you can create simple or complex logic here
|
|
51
|
+
return prisma.product.updateMany({
|
|
52
|
+
where: { name: { contains: name, mode: "insensitive" } },
|
|
53
|
+
data: { stock: { increment: amount } },
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// it will execute action to update your product stock
|
|
59
|
+
agent.execute("Can you add 5 units of stock to my product named monitor?");
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## References
|
|
63
|
+
|
|
64
|
+
This section documents the main function provided by `actient`, including the core agent, intent parser, and available AI providers.
|
|
65
|
+
|
|
66
|
+
### Our supported SDK:
|
|
67
|
+
|
|
68
|
+
You need to install the SDK before using this library. Use the intent parser importing from `actient/providers/*` to setup your AI.
|
|
69
|
+
|
|
70
|
+
| Provider | SDK | Intent Parser |
|
|
71
|
+
| -------- | --------------- | -------------------------------------------------------------- |
|
|
72
|
+
| Open AI | `openai` | `OpenAIIntentParser`, imported from `actient/providers/openai` |
|
|
73
|
+
| Groq | `grok-sdk` | `GroqIntentParser`, imported from `actient/providers/groq` |
|
|
74
|
+
| Gemini | `@google/genai` | `GeminiIntentParser`, imported from `actient/providers/gemini` |
|
|
75
|
+
|
|
76
|
+
### AI Agent
|
|
77
|
+
|
|
78
|
+
You can define and select your ai agent using `AIAgent` instance.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
const agent = new AIAgent({
|
|
82
|
+
ai: new OpenAIIntentParser({
|
|
83
|
+
apiKey: "your_api_key", // api key from ai provider
|
|
84
|
+
model: "ai-model",
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Options
|
|
90
|
+
|
|
91
|
+
| Name | Desciption |
|
|
92
|
+
| ---- | ---------------------------------------------------------------------------------------- |
|
|
93
|
+
| ai | Define your selected ai intent parser. See our supported intent parser to get the detail |
|
|
94
|
+
|
|
95
|
+
### registerAction()
|
|
96
|
+
|
|
97
|
+
Registers actions that can be executed by the agent.
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
agent.registerAction("action_identifier", {
|
|
101
|
+
description: "Action description",
|
|
102
|
+
rules: ["specific rule"],
|
|
103
|
+
schema: z.object({
|
|
104
|
+
params: z.string(),
|
|
105
|
+
}),
|
|
106
|
+
handler: async ({ params }) => {
|
|
107
|
+
// you can create simple or complex logic here
|
|
108
|
+
return `Success to execute: ${params}`;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### Options
|
|
114
|
+
|
|
115
|
+
| Name | Type | Required | Desciption |
|
|
116
|
+
| ------------ | --------------- | -------- | -------------------------------------------------------------------- |
|
|
117
|
+
| \_identifier | string | ✅ | Unique action identifier (used by AI to recognize the functions) |
|
|
118
|
+
| description | string | ✅ | Brief explanation of the action function (used in the system prompt) |
|
|
119
|
+
| rules | array of string | ❌ | Specific rules for the action |
|
|
120
|
+
| schema | ZodSchema | ✅ | Zod schema for action parameter validation |
|
|
121
|
+
| handler | function | ✅ | Async function that will be executed by AI |
|
|
122
|
+
|
|
123
|
+
### execute()
|
|
124
|
+
|
|
125
|
+
Runs agent based on user input.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
const result = await agent.execute("User prompt");
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Best Practices
|
|
132
|
+
|
|
133
|
+
This section outlines recommended best practices to help you build reliable, maintainable, and predictable AI-driven actions using this library.
|
|
134
|
+
|
|
135
|
+
### Unknown Action
|
|
136
|
+
|
|
137
|
+
The AI will only execute the appropriate action. If no appropriate action is found, it will treat it as `UNKNOWN`. You can handle this by defining a register with the identifier as `UNKNOWN`.
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
agent.registerAction("UNKNOWN", {
|
|
141
|
+
description: "Default handler for unknown intents",
|
|
142
|
+
rules: ["Handle unknown intents gracefully and inform the user."],
|
|
143
|
+
schema: z.object({
|
|
144
|
+
message: z.string(),
|
|
145
|
+
}),
|
|
146
|
+
handler: async ({ message }) => {
|
|
147
|
+
return { message };
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Specific rules
|
|
153
|
+
|
|
154
|
+
Add specific rules to ensure the AI works properly and prevent the AI from deviating from its task.
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
agent.registerAction("action", {
|
|
158
|
+
rules: ["rule 1", "rule 2"],
|
|
159
|
+
// ...
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Typescript Support
|
|
164
|
+
|
|
165
|
+
This library is completely written and maintained using typescript.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/utils/prompt.ts
|
|
2
|
+
var generateSystemPrompt = (actions) => {
|
|
3
|
+
return `
|
|
4
|
+
You are an intent parser.
|
|
5
|
+
|
|
6
|
+
Your task is to translate user input into a structured JSON intent.
|
|
7
|
+
|
|
8
|
+
Available actions:
|
|
9
|
+
${actions.map(
|
|
10
|
+
(a) => `- ${a.name}: ${a.description}
|
|
11
|
+
Parameters:
|
|
12
|
+
${Object.entries(a.parameters).map(([key, type]) => ` - ${key}: ${type}`).join("\n")}
|
|
13
|
+
Specific rules: ${a.rules?.length ? a.rules.map((r) => ` - ${r}`).join("\n") : " - None"}
|
|
14
|
+
`
|
|
15
|
+
).join("\n")}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
Rules:
|
|
19
|
+
- Respond ONLY with valid JSON
|
|
20
|
+
- Do NOT include markdown
|
|
21
|
+
- Do NOT explain anything
|
|
22
|
+
- Do NOT invent actions
|
|
23
|
+
- Only use actions listed above
|
|
24
|
+
- Parameter names MUST exactly match the schema
|
|
25
|
+
- Do NOT rename parameters
|
|
26
|
+
- Do NOT invent parameters
|
|
27
|
+
- JSON format:
|
|
28
|
+
{
|
|
29
|
+
"action": "action_name",
|
|
30
|
+
"params": { ... }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
If no action matches, return:
|
|
34
|
+
{
|
|
35
|
+
"action": "UNKNOWN",
|
|
36
|
+
"params": {}
|
|
37
|
+
}
|
|
38
|
+
`.trim();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/utils/loader.ts
|
|
42
|
+
async function loadGemini() {
|
|
43
|
+
try {
|
|
44
|
+
return await import('@google/genai');
|
|
45
|
+
} catch {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Gemini support requires `@google/genai`. Install it with:\nnpm install @google/genai"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function loadOpenAI() {
|
|
52
|
+
try {
|
|
53
|
+
return await import('openai');
|
|
54
|
+
} catch {
|
|
55
|
+
throw new Error(
|
|
56
|
+
"OpenAI support requires `openai`. Install it with:\nnpm install openai"
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function loadGroq() {
|
|
61
|
+
try {
|
|
62
|
+
return await import('groq-sdk');
|
|
63
|
+
} catch {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"Groq support requires `groq-sdk`. Install it with:\nnpm install groq-sdk"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { generateSystemPrompt, loadGemini, loadGroq, loadOpenAI };
|
|
71
|
+
//# sourceMappingURL=chunk-UNGR3PLJ.js.map
|
|
72
|
+
//# sourceMappingURL=chunk-UNGR3PLJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/prompt.ts","../src/utils/loader.ts"],"names":[],"mappings":";AAEO,IAAM,oBAAA,GAAuB,CAAC,OAAA,KAAuC;AAC1E,EAAA,OAAO;AAAA;;AAAA;;AAAA;AAAA,EAMP,OAAA,CACC,GAAA;AAAA,IACC,CAAC,CAAA,KAAM,CAAA,EAAA,EAAK,EAAE,IAAI,CAAA,EAAA,EAAK,EAAE,WAAW;AAAA;AAAA,EAEtC,OAAO,OAAA,CAAQ,CAAA,CAAE,UAAU,CAAA,CAC1B,GAAA,CAAI,CAAC,CAAC,GAAA,EAAK,IAAI,CAAA,KAAM,CAAA,MAAA,EAAS,GAAG,CAAA,EAAA,EAAK,IAAI,EAAE,CAAA,CAC5C,IAAA,CAAK,IAAI,CAAC;AAAA,kBAAA,EAET,CAAA,CAAE,KAAA,EAAO,MAAA,GAAS,CAAA,CAAE,MAAM,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,MAAA,EAAS,CAAC,CAAA,CAAE,CAAA,CAAE,IAAA,CAAK,IAAI,IAAI,YAClE;AAAA,EAAA;AAAA,GAEA,CACC,IAAA,CAAK,IAAI,CAAC;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA,CAuBX,IAAA,EAAK;AACP;;;AC7CA,eAAsB,UAAA,GAAa;AACjC,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,OAAO,eAAe,CAAA;AAAA,EACrC,CAAA,CAAA,MAAQ;AACN,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACF;AAEA,eAAsB,UAAA,GAAa;AACjC,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,OAAO,QAAQ,CAAA;AAAA,EAC9B,CAAA,CAAA,MAAQ;AACN,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACF;AAEA,eAAsB,QAAA,GAAW;AAC/B,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,OAAO,UAAU,CAAA;AAAA,EAChC,CAAA,CAAA,MAAQ;AACN,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACF","file":"chunk-UNGR3PLJ.js","sourcesContent":["import type { AvailableAction } from \"../intent/intent-parser\";\r\n\r\nexport const generateSystemPrompt = (actions: AvailableAction[]): string => {\r\n return `\r\nYou are an intent parser.\r\n\r\nYour task is to translate user input into a structured JSON intent.\r\n\r\nAvailable actions:\r\n${actions\r\n .map(\r\n (a) => `- ${a.name}: ${a.description}\r\n Parameters:\r\n${Object.entries(a.parameters)\r\n .map(([key, type]) => ` - ${key}: ${type}`)\r\n .join(\"\\n\")}\r\n Specific rules: ${\r\n a.rules?.length ? a.rules.map((r) => ` - ${r}`).join(\"\\n\") : \" - None\"\r\n }\r\n `\r\n )\r\n .join(\"\\n\")}\r\n\r\n \r\nRules:\r\n- Respond ONLY with valid JSON\r\n- Do NOT include markdown\r\n- Do NOT explain anything\r\n- Do NOT invent actions\r\n- Only use actions listed above\r\n- Parameter names MUST exactly match the schema\r\n- Do NOT rename parameters\r\n- Do NOT invent parameters\r\n- JSON format:\r\n{\r\n \"action\": \"action_name\",\r\n \"params\": { ... }\r\n}\r\n\r\nIf no action matches, return:\r\n{\r\n \"action\": \"UNKNOWN\",\r\n \"params\": {}\r\n}\r\n`.trim();\r\n};\r\n","export async function loadGemini() {\r\n try {\r\n return await import(\"@google/genai\");\r\n } catch {\r\n throw new Error(\r\n \"Gemini support requires `@google/genai`. Install it with:\\n\" +\r\n \"npm install @google/genai\"\r\n );\r\n }\r\n}\r\n\r\nexport async function loadOpenAI() {\r\n try {\r\n return await import(\"openai\");\r\n } catch {\r\n throw new Error(\r\n \"OpenAI support requires `openai`. Install it with:\\n\" +\r\n \"npm install openai\"\r\n );\r\n }\r\n}\r\n\r\nexport async function loadGroq() {\r\n try {\r\n return await import(\"groq-sdk\");\r\n } catch {\r\n throw new Error(\r\n \"Groq support requires `groq-sdk`. Install it with:\\n\" +\r\n \"npm install groq-sdk\"\r\n );\r\n }\r\n}\r\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { I as Intent, a as IntentParser } from './intent-parser-tPkUA2YT.js';
|
|
2
|
+
import { ZodSchema } from 'zod';
|
|
3
|
+
|
|
4
|
+
interface AIProvider {
|
|
5
|
+
parseIntent(prompt: string, availableActions: any[]): Promise<Intent>;
|
|
6
|
+
}
|
|
7
|
+
declare class AIAgent {
|
|
8
|
+
private registry;
|
|
9
|
+
private intentParser;
|
|
10
|
+
constructor(options: {
|
|
11
|
+
ai: IntentParser;
|
|
12
|
+
});
|
|
13
|
+
registerAction<TParams, TResult>(name: string, config: {
|
|
14
|
+
description: string;
|
|
15
|
+
rules?: string[];
|
|
16
|
+
schema: ZodSchema<TParams>;
|
|
17
|
+
handler: (params: TParams) => Promise<TResult>;
|
|
18
|
+
}): void;
|
|
19
|
+
execute(prompt: string): Promise<any>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { AIAgent, type AIProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/registry/action-registry.ts
|
|
4
|
+
var ActionRegistry = class {
|
|
5
|
+
actions = /* @__PURE__ */ new Map();
|
|
6
|
+
register(name, action) {
|
|
7
|
+
if (this.actions.has(name)) {
|
|
8
|
+
throw new Error(`Action "${name}" already registered`);
|
|
9
|
+
}
|
|
10
|
+
this.actions.set(name, action);
|
|
11
|
+
}
|
|
12
|
+
get(name) {
|
|
13
|
+
const action = this.actions.get(name);
|
|
14
|
+
if (!action) {
|
|
15
|
+
throw new Error(`Action "${name}" is not registered`);
|
|
16
|
+
}
|
|
17
|
+
return action;
|
|
18
|
+
}
|
|
19
|
+
list() {
|
|
20
|
+
return Array.from(this.actions.entries()).map(([name, action]) => ({
|
|
21
|
+
name,
|
|
22
|
+
rules: action.rules || [],
|
|
23
|
+
description: action.description,
|
|
24
|
+
parameters: this.zodToSimpleSchema(action.schema)
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
has(name) {
|
|
28
|
+
return this.actions.has(name);
|
|
29
|
+
}
|
|
30
|
+
zodToSimpleSchema(schema) {
|
|
31
|
+
if (!(schema instanceof z.ZodObject)) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
const shape = schema.shape;
|
|
35
|
+
const result = {};
|
|
36
|
+
for (const key in shape) {
|
|
37
|
+
const field = shape[key];
|
|
38
|
+
result[key] = this.mapZodType(field);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
mapZodType(field) {
|
|
43
|
+
if (field instanceof z.ZodOptional || field instanceof z.ZodNullable) {
|
|
44
|
+
return this.mapZodType(field.unwrap());
|
|
45
|
+
}
|
|
46
|
+
if (field instanceof z.ZodString) return "string";
|
|
47
|
+
if (field instanceof z.ZodNumber) return "number";
|
|
48
|
+
if (field instanceof z.ZodBoolean) return "boolean";
|
|
49
|
+
if (field instanceof z.ZodArray) return "array";
|
|
50
|
+
if (field instanceof z.ZodEnum) return "enum";
|
|
51
|
+
if (field instanceof z.ZodObject) return "object";
|
|
52
|
+
return "unknown";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/agent.ts
|
|
57
|
+
var AIAgent = class {
|
|
58
|
+
registry;
|
|
59
|
+
intentParser;
|
|
60
|
+
constructor(options) {
|
|
61
|
+
this.registry = new ActionRegistry();
|
|
62
|
+
this.intentParser = options.ai;
|
|
63
|
+
}
|
|
64
|
+
registerAction(name, config) {
|
|
65
|
+
this.registry.register(name, config);
|
|
66
|
+
}
|
|
67
|
+
async execute(prompt) {
|
|
68
|
+
const availableActions = this.registry.list();
|
|
69
|
+
const intent = await this.intentParser.parse(prompt, availableActions);
|
|
70
|
+
if (!intent?.action) {
|
|
71
|
+
throw new Error("Invalid intent: missing action");
|
|
72
|
+
}
|
|
73
|
+
const action = this.registry.get(intent.action);
|
|
74
|
+
if (intent.action === "UNKNOWN" && !action) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Unable to determine user intent, please define a default action."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const validatedParams = action.schema.parse(intent.params);
|
|
80
|
+
return action.handler(validatedParams);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { AIAgent };
|
|
85
|
+
//# sourceMapp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,IAAA,CAAK,QAAA,GAAW,IAAI,cAAA,EAAe;AACnC,IAAA,IAAA,CAAK,eAAe,OAAA,CAAQ,EAAA;AAAA,EAC9B;AAAA,EAEA,cAAA,CACE,MACA,MAAA,EAMA;AACA,IAAA,IAAA,CAAK,QAAA,CAAS,QAAA,CAAS,IAAA,EAAM,MAAM,CAAA;AAAA,EACrC;AAAA,EAEA,MAAM,QAAQ,MAAA,EAA8B;AAC1C,IAAA,MAAM,gBAAA,GAAmB,IAAA,CAAK,QAAA,CAAS,IAAA,EAAK;AAC5C,IAAA,MAAM,SAAS,MAAM,IAAA,CAAK,YAAA,CAAa,KAAA,CAAM,QAAQ,gBAAgB,CAAA;AAErE,IAAA,IAAI,CAAC,QAAQ,MAAA,EAAQ;AACnB,MAAA,MAAM,IAAI,MAAM,gCAAgC,CAAA;AAAA,IAClD;AAEA,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,QAAA,CAAS,GAAA,CAAI,OAAO,MAAM,CAAA;AAE9C,IAAA,IAAI,MAAA,CAAO,MAAA,KAAW,SAAA,IAAa,CAAC,MAAA,EAAQ;AAC1C,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAEA,IAAA,MAAM,eAAA,GAAkB,MAAA,CAAO,MAAA,CAAO,KAAA,CAAM,OAAO,MAAM,CAAA;AACzD,IAAA,OAAO,MAAA,CAAO,QAAQ,eAAe,CAAA;AAAA,EACvC;AACF","file":"index.js","sourcesContent":["import type { ZodSchema } from \"zod\";\r\nimport { z } from \"zod\";\r\n\r\nexport type ActionHandler<TParams = any, TResult = any> = (\r\n params: TParams\r\n) => Promise<TResult>;\r\n\r\nexport interface ActionDefinition<TParams = any, TResult = any> {\r\n description: string;\r\n rules?: string[];\r\n schema: ZodSchema<TParams>;\r\n handler: ActionHandler<TParams, TResult>;\r\n}\r\n\r\nexport class ActionRegistry {\r\n private actions = new Map<string, ActionDefinition>();\r\n\r\n register<TParams, TResult>(\r\n name: string,\r\n action: ActionDefinition<TParams, TResult>\r\n ) {\r\n if (this.actions.has(name)) {\r\n throw new Error(`Action \"${name}\" already registered`);\r\n }\r\n\r\n this.actions.set(name, action);\r\n }\r\n\r\n get(name: string): ActionDefinition {\r\n const action = this.actions.get(name);\r\n\r\n if (!action) {\r\n throw new Error(`Action \"${name}\" is not registered`);\r\n }\r\n\r\n return action;\r\n }\r\n\r\n list(): Array<{\r\n name: string;\r\n rules: string[];\r\n description: string;\r\n parameters: Record<string, string>;\r\n }> {\r\n return Array.from(this.actions.entries()).map(([name, action]) => ({\r\n name,\r\n rules: action.rules || [],\r\n description: action.description,\r\n parameters: this.zodToSimpleSchema(action.schema),\r\n }));\r\n }\r\n\r\n has(name: string): boolean {\r\n return this.actions.has(name);\r\n }\r\n\r\n private zodToSimpleSchema(schema: ZodSchema<any>) {\r\n if (!(schema instanceof z.ZodObject)) {\r\n return {};\r\n }\r\n\r\n const shape = schema.shape;\r\n const result: Record<string, string> = {};\r\n\r\n for (const key in shape) {\r\n const field = shape[key];\r\n\r\n result[key] = this.mapZodType(field);\r\n }\r\n\r\n return result;\r\n }\r\n\r\n private mapZodType(field: any): string {\r\n // unwrap optional / nullable\r\n if (field instanceof z.ZodOptional || field instanceof z.ZodNullable) {\r\n return this.mapZodType(field.unwrap());\r\n }\r\n\r\n if (field instanceof z.ZodString) return \"string\";\r\n if (field instanceof z.ZodNumber) return \"number\";\r\n if (field instanceof z.ZodBoolean) return \"boolean\";\r\n if (field instanceof z.ZodArray) return \"array\";\r\n if (field instanceof z.ZodEnum) return \"enum\";\r\n if (field instanceof z.ZodObject) return \"object\";\r\n\r\n return \"unknown\";\r\n }\r\n}\r\n","import {\r\n ActionRegistry,\r\n // ActionDefinition,\r\n} from \"./registry/action-registry\";\r\nimport type { IntentParser } from \"./intent/intent-parser\";\r\nimport type { ZodSchema } from \"zod\";\r\nimport type { Intent } from \"./types/intent\";\r\n\r\nexport interface AIProvider {\r\n parseIntent(prompt: string, availableActions: any[]): Promise<Intent>;\r\n}\r\n\r\nexport class AIAgent {\r\n private registry: ActionRegistry;\r\n private intentParser: IntentParser;\r\n\r\n constructor(options: { ai: IntentParser }) {\r\n this.registry = new ActionRegistry();\r\n this.intentParser = options.ai;\r\n }\r\n\r\n registerAction<TParams, TResult>(\r\n name: string,\r\n config: {\r\n description: string;\r\n rules?: string[];\r\n schema: ZodSchema<TParams>;\r\n handler: (params: TParams) => Promise<TResult>;\r\n }\r\n ) {\r\n this.registry.register(name, config);\r\n }\r\n\r\n async execute(prompt: string): Promise<any> {\r\n const availableActions = this.registry.list();\r\n const intent = await this.intentParser.parse(prompt, availableActions);\r\n\r\n if (!intent?.action) {\r\n throw new Error(\"Invalid intent: missing action\");\r\n }\r\n\r\n const action = this.registry.get(intent.action);\r\n\r\n if (intent.action === \"UNKNOWN\" && !action) {\r\n throw new Error(\r\n \"Unable to determine user intent, please define a default action.\"\r\n );\r\n }\r\n\r\n const validatedParams = action.schema.parse(intent.params);\r\n return action.handler(validatedParams);\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface Intent {
|
|
2
|
+
action: string;
|
|
3
|
+
params: Record<string, any>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface AvailableAction {
|
|
7
|
+
name: string;
|
|
8
|
+
rules?: string[];
|
|
9
|
+
description: string;
|
|
10
|
+
parameters: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
interface IntentParser {
|
|
13
|
+
parse(prompt: string, actions: AvailableAction[]): Promise<Intent>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type { AvailableAction as A, Intent as I, IntentParser as a };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { a as IntentParser, A as AvailableAction, I as Intent } from '../intent-parser-tPkUA2YT.js';
|
|
2
|
+
|
|
3
|
+
declare class GeminiIntentParser implements IntentParser {
|
|
4
|
+
private client?;
|
|
5
|
+
private readonly apiKey;
|
|
6
|
+
private readonly model;
|
|
7
|
+
constructor(options: {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
model?: string;
|
|
10
|
+
});
|
|
11
|
+
private getClient;
|
|
12
|
+
parse(prompt: string, actions: AvailableAction[]): Promise<Intent>;
|
|
13
|
+
private safeParseJSON;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { GeminiIntentParser };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { loadGemini, generateSystemPrompt } from '../chunk-UNGR3PLJ.js';
|
|
2
|
+
|
|
3
|
+
// src/providers/gemini.ts
|
|
4
|
+
var GeminiIntentParser = class {
|
|
5
|
+
client;
|
|
6
|
+
apiKey;
|
|
7
|
+
model;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.apiKey = options.apiKey;
|
|
10
|
+
this.model = options.model ?? "gemini-2.5-flash";
|
|
11
|
+
}
|
|
12
|
+
// lazy loader
|
|
13
|
+
async getClient() {
|
|
14
|
+
if (this.client) return this.client;
|
|
15
|
+
const { GoogleGenAI } = await loadGemini();
|
|
16
|
+
this.client = new GoogleGenAI({ apiKey: this.apiKey });
|
|
17
|
+
return this.client;
|
|
18
|
+
}
|
|
19
|
+
async parse(prompt, actions) {
|
|
20
|
+
const client = await this.getClient();
|
|
21
|
+
const systemPrompt = generateSystemPrompt(actions);
|
|
22
|
+
const result = await client.models.generateContent({
|
|
23
|
+
model: this.model,
|
|
24
|
+
contents: [
|
|
25
|
+
{
|
|
26
|
+
role: "user",
|
|
27
|
+
parts: [{ text: systemPrompt + "\n\nUser input:\n" + prompt }]
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
const content = result.text;
|
|
32
|
+
if (!content) {
|
|
33
|
+
throw new Error("Gemini returned empty response");
|
|
34
|
+
}
|
|
35
|
+
return this.safeParseJSON(content);
|
|
36
|
+
}
|
|
37
|
+
// json guard
|
|
38
|
+
safeParseJSON(text) {
|
|
39
|
+
try {
|
|
40
|
+
const cleaned = text.replace(/```json/g, "").replace(/```/g, "").trim();
|
|
41
|
+
return JSON.parse(cleaned);
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error(`Invalid JSON returned by Gemini:
|
|
44
|
+
${text}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { GeminiIntentParser };
|
|
50
|
+
//# sourceMappingURL=gemini.js.map
|
|
51
|
+
//# sourceMappingURL=gemini.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/providers/gemini.ts"],"names":[],"mappings":";;;AAMO,IAAM,qBAAN,MAAiD;AAAA,EAC9C,MAAA;AAAA,EACS,MAAA;AAAA,EACA,KAAA;AAAA,EAEjB,YAAY,OAAA,EAA6C;AACvD,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,KAAA,GAAQ,QAAQ,KAAA,IAAS,kBAAA;AAAA,EAChC;AAAA;AAAA,EAGA,MAAc,SAAA,GAAkC;AAC9C,IAAA,IAAI,IAAA,CAAK,MAAA,EAAQ,OAAO,IAAA,CAAK,MAAA;AAE7B,IAAA,MAAM,EAAE,WAAA,EAAY,GAAI,MAAM,UAAA,EAAW;AACzC,IAAA,IAAA,CAAK,SAAS,IAAI,WAAA,CAAY,EAAE,MAAA,EAAQ,IAAA,CAAK,QAAQ,CAAA;AAErD,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EACd;AAAA,EAEA,MAAM,KAAA,CAAM,MAAA,EAAgB,OAAA,EAA6C;AACvE,IAAA,MAAM,MAAA,GAAS,MAAM,IAAA,CAAK,SAAA,EAAU;AACpC,IAAA,MAAM,YAAA,GAAe,qBAAqB,OAAO,CAAA;AAEjD,IAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,MAAA,CAAO,eAAA,CAAgB;AAAA,MACjD,OAAO,IAAA,CAAK,KAAA;AAAA,MACZ,QAAA,EAAU;AAAA,QACR;AAAA,UACE,IAAA,EAAM,MAAA;AAAA,UACN,OAAO,CAAC,EAAE,MAAM,YAAA,GAAe,mBAAA,GAAsB,QAAQ;AAAA;AAC/D;AACF,KACD,CAAA;AAED,IAAA,MAAM,UAAU,MAAA,CAAO,IAAA;AAEvB,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA,MAAM,IAAI,MAAM,gCAAgC,CAAA;AAAA,IAClD;AAEA,IAAA,OAAO,IAAA,CAAK,cAAc,OAAO,CAAA;AAAA,EACnC;AAAA;AAAA,EAGQ,cAAc,IAAA,EAAsB;AAC1C,IAAA,IAAI;AACF,MAAA,MAAM,OAAA,GAAU,IAAA,CACb,OAAA,CAAQ,UAAA,EAAY,EAAE,EACtB,OAAA,CAAQ,MAAA,EAAQ,EAAE,CAAA,CAClB,IAAA,EAAK;AAER,MAAA,OAAO,IAAA,CAAK,MAAM,OAAO,CAAA;AAAA,IAC3B,CAAA,CAAA,MAAQ;AACN,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA;AAAA,EAAqC,IAAI,CAAA,CAAE,CAAA;AAAA,IAC7D;AAAA,EACF;AACF","file":"gemini.js","sourcesContent":["import type { Intent } from \"../types/intent\";\r\nimport type { AvailableAction, IntentParser } from \"../intent/intent-parser\";\r\nimport { generateSystemPrompt } from \"../utils/prompt\";\r\nimport { loadGemini } from \"../utils/loader\";\r\nimport type { GoogleGenAI } from \"@google/genai\"; // just for type import\r\n\r\nexport class GeminiIntentParser implements IntentParser {\r\n private client?: GoogleGenAI;\r\n private readonly apiKey: string;\r\n private readonly model: string;\r\n\r\n constructor(options: { apiKey: string; model?: string }) {\r\n this.apiKey = options.apiKey;\r\n this.model = options.model ?? \"gemini-2.5-flash\";\r\n }\r\n\r\n // lazy loader\r\n private async getClient(): Promise<GoogleGenAI> {\r\n if (this.client) return this.client;\r\n\r\n const { GoogleGenAI } = await loadGemini();\r\n this.client = new GoogleGenAI({ apiKey: this.apiKey });\r\n\r\n return this.client;\r\n }\r\n\r\n async parse(prompt: string, actions: AvailableAction[]): Promise<Intent> {\r\n const client = await this.getClient();\r\n const systemPrompt = generateSystemPrompt(actions);\r\n\r\n const result = await client.models.generateContent({\r\n model: this.model,\r\n contents: [\r\n {\r\n role: \"user\",\r\n parts: [{ text: systemPrompt + \"\\n\\nUser input:\\n\" + prompt }],\r\n },\r\n ],\r\n });\r\n\r\n const content = result.text;\r\n\r\n if (!content) {\r\n throw new Error(\"Gemini returned empty response\");\r\n }\r\n\r\n return this.safeParseJSON(content);\r\n }\r\n\r\n // json guard\r\n private safeParseJSON(text: string): Intent {\r\n try {\r\n const cleaned = text\r\n .replace(/```json/g, \"\")\r\n .replace(/```/g, \"\")\r\n .trim();\r\n\r\n return JSON.parse(cleaned);\r\n } catch {\r\n throw new Error(`Invalid JSON returned by Gemini:\\n${text}`);\r\n }\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { a as IntentParser, A as AvailableAction, I as Intent } from '../intent-parser-tPkUA2YT.js';
|
|
2
|
+
|
|
3
|
+
declare class GroqIntentParser implements IntentParser {
|
|
4
|
+
private client?;
|
|
5
|
+
private readonly apiKey;
|
|
6
|
+
private readonly model;
|
|
7
|
+
constructor(options: {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
model?: string;
|
|
10
|
+
});
|
|
11
|
+
private getClient;
|
|
12
|
+
parse(prompt: string, actions: AvailableAction[]): Promise<Intent>;
|
|
13
|
+
private safeParseJSON;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { GroqIntentParser };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { loadGroq, generateSystemPrompt } from '../chunk-UNGR3PLJ.js';
|
|
2
|
+
|
|
3
|
+
// src/providers/groq.ts
|
|
4
|
+
var GroqIntentParser = class {
|
|
5
|
+
client;
|
|
6
|
+
apiKey;
|
|
7
|
+
model;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.apiKey = options.apiKey;
|
|
10
|
+
this.model = options.model ?? "llama3-70b-8192";
|
|
11
|
+
}
|
|
12
|
+
async getClient() {
|
|
13
|
+
if (this.client) return this.client;
|
|
14
|
+
const { default: Groq } = await loadGroq();
|
|
15
|
+
this.client = new Groq({ apiKey: this.apiKey });
|
|
16
|
+
return this.client;
|
|
17
|
+
}
|
|
18
|
+
async parse(prompt, actions) {
|
|
19
|
+
const systemPrompt = generateSystemPrompt(actions);
|
|
20
|
+
const client = await this.getClient();
|
|
21
|
+
const response = await client.chat.completions.create({
|
|
22
|
+
model: this.model,
|
|
23
|
+
temperature: 0,
|
|
24
|
+
messages: [
|
|
25
|
+
{ role: "system", content: systemPrompt },
|
|
26
|
+
{ role: "user", content: prompt }
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
const content = response.choices[0]?.message?.content;
|
|
30
|
+
if (!content) {
|
|
31
|
+
throw new Error("Groq returned empty response");
|
|
32
|
+
}
|
|
33
|
+
return this.safeParseJSON(content);
|
|
34
|
+
}
|
|
35
|
+
safeParseJSON(text) {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(text);
|
|
38
|
+
} catch {
|
|
39
|
+
throw new Error(`Invalid JSON returned by Groq:
|
|
40
|
+
${text}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { GroqIntentParser };
|
|
46
|
+
//# sourceMappingURL=groq.js.map
|
|
47
|
+
//# sourceMappingURL=groq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/providers/groq.ts"],"names":[],"mappings":";;;AAMO,IAAM,mBAAN,MAA+C;AAAA,EAC5C,MAAA;AAAA,EACS,MAAA;AAAA,EACA,KAAA;AAAA,EAEjB,YAAY,OAAA,EAA6C;AACvD,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,KAAA,GAAQ,QAAQ,KAAA,IAAS,iBAAA;AAAA,EAChC;AAAA,EAEA,MAAc,SAAA,GAA2B;AACv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;AACF","file":"groq.js","sourcesContent":["import type { Intent } from \"../types/intent\";\r\nimport type { AvailableAction, IntentParser } from \"../intent/intent-parser\";\r\nimport { generateSystemPrompt } from \"../utils/prompt\";\r\nimport { loadGroq } from \"../utils/loader\";\r\nimport type Groq from \"groq-sdk\";\r\n\r\nexport class GroqIntentParser implements IntentParser {\r\n private client?: Groq;\r\n private readonly apiKey: string;\r\n private readonly model: string;\r\n\r\n constructor(options: { apiKey: string; model?: string }) {\r\n this.apiKey = options.apiKey;\r\n this.model = options.model ?? \"llama3-70b-8192\";\r\n }\r\n\r\n private async getClient(): Promise<Groq> {\r\n if (this.client) return this.client;\r\n\r\n const { default: Groq } = await loadGroq();\r\n this.client = new Groq({ apiKey: this.apiKey });\r\n\r\n return this.client;\r\n }\r\n\r\n async parse(prompt: string, actions: AvailableAction[]): Promise<Intent> {\r\n const systemPrompt = generateSystemPrompt(actions);\r\n const client = await this.getClient();\r\n\r\n const response = await client.chat.completions.create({\r\n model: this.model,\r\n temperature: 0,\r\n messages: [\r\n { role: \"system\", content: systemPrompt },\r\n { role: \"user\", content: prompt },\r\n ],\r\n });\r\n\r\n const content = response.choices[0]?.message?.content;\r\n\r\n if (!content) {\r\n throw new Error(\"Groq returned empty response\");\r\n }\r\n\r\n return this.safeParseJSON(content);\r\n }\r\n\r\n private safeParseJSON(text: string): Intent {\r\n try {\r\n return JSON.parse(text);\r\n } catch {\r\n throw new Error(`Invalid JSON returned by Groq:\\n${text}`);\r\n }\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { a as IntentParser, A as AvailableAction, I as Intent } from '../intent-parser-tPkUA2YT.js';
|
|
2
|
+
|
|
3
|
+
declare class OpenAIIntentParser implements IntentParser {
|
|
4
|
+
private client?;
|
|
5
|
+
private readonly apiKey;
|
|
6
|
+
private readonly model;
|
|
7
|
+
constructor(options: {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
model?: string;
|
|
10
|
+
});
|
|
11
|
+
private getClient;
|
|
12
|
+
parse(prompt: string, actions: AvailableAction[]): Promise<Intent>;
|
|
13
|
+
private safeParseJSON;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { OpenAIIntentParser };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { loadOpenAI, generateSystemPrompt } from '../chunk-UNGR3PLJ.js';
|
|
2
|
+
|
|
3
|
+
// src/providers/openai.ts
|
|
4
|
+
var OpenAIIntentParser = class {
|
|
5
|
+
client;
|
|
6
|
+
apiKey;
|
|
7
|
+
model;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.apiKey = options.apiKey;
|
|
10
|
+
this.model = options.model ?? "gpt-4o-mini";
|
|
11
|
+
}
|
|
12
|
+
async getClient() {
|
|
13
|
+
if (this.client) return this.client;
|
|
14
|
+
const { default: OpenAI } = await loadOpenAI();
|
|
15
|
+
this.client = new OpenAI({ apiKey: this.apiKey });
|
|
16
|
+
return this.client;
|
|
17
|
+
}
|
|
18
|
+
async parse(prompt, actions) {
|
|
19
|
+
const systemPrompt = generateSystemPrompt(actions);
|
|
20
|
+
const client = await this.getClient();
|
|
21
|
+
const response = await client.chat.completions.create({
|
|
22
|
+
model: this.model,
|
|
23
|
+
temperature: 0,
|
|
24
|
+
messages: [
|
|
25
|
+
{ role: "system", content: systemPrompt },
|
|
26
|
+
{ role: "user", content: prompt }
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
const content = response.choices[0]?.message?.content;
|
|
30
|
+
if (!content) {
|
|
31
|
+
throw new Error("AI returned empty response");
|
|
32
|
+
}
|
|
33
|
+
return this.safeParseJSON(content);
|
|
34
|
+
}
|
|
35
|
+
safeParseJSON(text) {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(text);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
throw new Error(`Invalid JSON returned by AI: ${text}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { OpenAII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OAAA,CAAQ,CAAC,GAAG,OAAA,EAAS,OAAA;AAE9C,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA,MAAM,IAAI,MAAM,4BAA4B,CAAA;AAAA,IAC9C;AAEA,IAAA,OAAO,IAAA,CAAK,cAAc,OAAO,CAAA;AAAA,EACnC;AAAA,EAEQ,cAAc,IAAA,EAAsB;AAC1C,IAAA,IAAI;AACF,MAAA,OAAO,IAAA,CAAK,MAAM,IAAI,CAAA;AAAA,IACxB,SAAS,GAAA,EAAK;AACZ,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,6BAAA,EAAgC,IAAI,CAAA,CAAE,CAAA;AAAA,IACxD;AAAA,EACF;AACF","file":"openai.js","sourcesContent":["import type { Intent } from \"../types/intent\";\r\nimport type { AvailableAction, IntentParser } from \"../intent/intent-parser\";\r\nimport { loadOpenAI } from \"../utils/loader\";\r\nimport { generateSystemPrompt } from \"../utils/prompt\";\r\nimport type OpenAI from \"openai\";\r\n\r\nexport class OpenAIIntentParser implements IntentParser {\r\n private client?: OpenAI;\r\n private readonly apiKey;\r\n private readonly model: string;\r\n\r\n constructor(options: { apiKey: string; model?: string }) {\r\n this.apiKey = options.apiKey;\r\n this.model = options.model ?? \"gpt-4o-mini\";\r\n }\r\n\r\n private async getClient(): Promise<OpenAI> {\r\n if (this.client) return this.client;\r\n\r\n const { default: OpenAI } = await loadOpenAI();\r\n this.client = new OpenAI({ apiKey: this.apiKey });\r\n\r\n return this.client;\r\n }\r\n\r\n async parse(prompt: string, actions: AvailableAction[]): Promise<Intent> {\r\n const systemPrompt = generateSystemPrompt(actions);\r\n const client = await this.getClient();\r\n\r\n const response = await client.chat.completions.create({\r\n model: this.model,\r\n temperature: 0,\r\n messages: [\r\n { role: \"system\", content: systemPrompt },\r\n { role: \"user\", content: prompt },\r\n ],\r\n });\r\n\r\n const content = response.choices[0]?.message?.content;\r\n\r\n if (!content) {\r\n throw new Error(\"AI returned empty response\");\r\n }\r\n\r\n return this.safeParseJSON(content);\r\n }\r\n\r\n private safeParseJSON(text: string): Intent {\r\n try {\r\n return JSON.parse(text);\r\n } catch (err) {\r\n throw new Error(`Invalid JSON returned by AI: ${text}`);\r\n }\r\n }\r\n}\r\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "actient",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup",
|
|
10
|
+
"dev": "tsup --watch",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"actient",
|
|
15
|
+
"action-bot",
|
|
16
|
+
"ai-action-agents"
|
|
17
|
+
],
|
|
18
|
+
"author": "Andrian004",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"zod": "^4.2.1"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"openai": "^4.0.0 || ^6.0.0",
|
|
28
|
+
"groq-sdk": "^0.37.0",
|
|
29
|
+
"@google/genai": "^1.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"openai": {
|
|
33
|
+
"optional": true
|
|
34
|
+
},
|
|
35
|
+
"groq-sdk": {
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"@google/genai": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"tsup": "^8.5.1",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"openai": "^4.0.0 || ^6.0.0",
|
|
46
|
+
"groq-sdk": "^0.37.0",
|
|
47
|
+
"@google/genai": "^1.0.0"
|
|
48
|
+
}
|
|
49
|
+
}
|