@kigathi/ai-agents 0.1.1 → 0.1.2
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/README.md +345 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @kigathi/ai-agents
|
|
2
2
|
|
|
3
|
-
Unified Agents/Bots SDK for Responses API.
|
|
3
|
+
Unified Agents/Bots SDK for the OpenAI Responses API.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -8,7 +8,50 @@ Unified Agents/Bots SDK for Responses API.
|
|
|
8
8
|
npm install @kigathi/ai-agents
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
For direct mode, also install:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install openai
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick API shape
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
21
|
+
|
|
22
|
+
const sdk = createClient(config);
|
|
23
|
+
sdk.registerTool(tool);
|
|
24
|
+
sdk.createAgent(agent);
|
|
25
|
+
const result = await sdk.run(params);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Minimal vs full examples
|
|
29
|
+
|
|
30
|
+
The sections below show the smallest valid call for each API, followed by a more realistic production-style example.
|
|
31
|
+
|
|
32
|
+
### 1. Create a client
|
|
33
|
+
|
|
34
|
+
Minimal direct mode:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
38
|
+
|
|
39
|
+
const sdk = createClient({
|
|
40
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Minimal proxy mode:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
48
|
+
|
|
49
|
+
const sdk = createClient({
|
|
50
|
+
backendUrl: 'https://api.example.com',
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Full example:
|
|
12
55
|
|
|
13
56
|
```js
|
|
14
57
|
import { createClient } from '@kigathi/ai-agents';
|
|
@@ -17,45 +60,279 @@ const sdk = createClient({
|
|
|
17
60
|
apiKey: process.env.OPENAI_API_KEY,
|
|
18
61
|
orgId: process.env.OPENAI_ORG_ID,
|
|
19
62
|
projectId: process.env.OPENAI_PROJECT_ID,
|
|
63
|
+
backendUrl: 'https://api.example.com',
|
|
64
|
+
mode: 'direct',
|
|
65
|
+
pricing: {
|
|
66
|
+
'gpt-4.1-mini': {
|
|
67
|
+
prompt_per_million: 0.4,
|
|
68
|
+
completion_per_million: 1.6,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Required parameters:
|
|
75
|
+
|
|
76
|
+
- Direct mode: `apiKey`
|
|
77
|
+
- Proxy mode: `backendUrl`
|
|
78
|
+
|
|
79
|
+
Optional parameters:
|
|
80
|
+
|
|
81
|
+
- `mode`
|
|
82
|
+
- `orgId`
|
|
83
|
+
- `projectId`
|
|
84
|
+
- `pricing`
|
|
85
|
+
|
|
86
|
+
### Direct OpenAI + backend persistence mode
|
|
87
|
+
|
|
88
|
+
You can also run the model directly with OpenAI while still sending conversation events to your backend for persistence, analytics, or post-processing.
|
|
89
|
+
|
|
90
|
+
Use `apiKey` and `backendUrl` together:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
94
|
+
|
|
95
|
+
const sdk = createClient({
|
|
96
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
97
|
+
backendUrl: 'https://api.example.com',
|
|
98
|
+
mode: 'direct',
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
What this mode does:
|
|
103
|
+
|
|
104
|
+
- Runs the actual model request against OpenAI directly
|
|
105
|
+
- Persists user and assistant messages to your backend
|
|
106
|
+
- Syncs tool-call events to your backend on a best-effort basis
|
|
107
|
+
|
|
108
|
+
Useful request fields in this mode:
|
|
109
|
+
|
|
110
|
+
- `conversation_id`
|
|
111
|
+
- `user_id`
|
|
112
|
+
- `metadata`
|
|
113
|
+
- `client_message_id`
|
|
114
|
+
- `idempotency_key`
|
|
115
|
+
- `idempotency_key_response`
|
|
116
|
+
|
|
117
|
+
### 2. Register a tool
|
|
118
|
+
|
|
119
|
+
Minimal example:
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
sdk.registerTool({
|
|
123
|
+
name: 'lookup_order',
|
|
20
124
|
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Full example:
|
|
21
128
|
|
|
129
|
+
```js
|
|
22
130
|
sdk.registerTool({
|
|
23
131
|
name: 'lookup_order',
|
|
24
132
|
type: 'function',
|
|
25
|
-
description: 'Find order by order number',
|
|
133
|
+
description: 'Find order details by order number.',
|
|
26
134
|
parameters_schema: {
|
|
27
135
|
type: 'object',
|
|
28
|
-
properties: {
|
|
136
|
+
properties: {
|
|
137
|
+
order_number: { type: 'string' },
|
|
138
|
+
},
|
|
29
139
|
required: ['order_number'],
|
|
30
140
|
},
|
|
31
|
-
handler: async ({ order_number }) =>
|
|
141
|
+
handler: async ({ order_number }, context) => {
|
|
142
|
+
return {
|
|
143
|
+
order_number,
|
|
144
|
+
status: 'processing',
|
|
145
|
+
requested_by: context.userId ?? null,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Required parameters:
|
|
152
|
+
|
|
153
|
+
- `name`
|
|
154
|
+
|
|
155
|
+
Useful optional parameters:
|
|
156
|
+
|
|
157
|
+
- `description`
|
|
158
|
+
- `parameters_schema`
|
|
159
|
+
- `handler`
|
|
160
|
+
|
|
161
|
+
Notes:
|
|
162
|
+
|
|
163
|
+
- If you omit `handler`, tool calls will fail gracefully with `Tool not registered: <name>`.
|
|
164
|
+
- If an agent does not list specific tools, it can use all registered tools.
|
|
165
|
+
|
|
166
|
+
### 3. Register an agent
|
|
167
|
+
|
|
168
|
+
Minimal example:
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
sdk.createAgent({
|
|
172
|
+
name: 'support-bot',
|
|
173
|
+
model: 'gpt-4.1-mini',
|
|
32
174
|
});
|
|
175
|
+
```
|
|
33
176
|
|
|
177
|
+
Full example:
|
|
178
|
+
|
|
179
|
+
```js
|
|
34
180
|
sdk.createAgent({
|
|
181
|
+
id: 'support-bot-v1',
|
|
35
182
|
name: 'support-bot',
|
|
36
183
|
model: 'gpt-4.1-mini',
|
|
37
|
-
instructions: 'You are a concise support assistant.',
|
|
184
|
+
instructions: 'You are a concise support assistant. Use tools when needed.',
|
|
185
|
+
temperature: 0.3,
|
|
186
|
+
max_output_tokens: 400,
|
|
38
187
|
tools: ['lookup_order'],
|
|
188
|
+
metadata: {
|
|
189
|
+
team: 'support',
|
|
190
|
+
channel: 'web',
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Required parameters:
|
|
196
|
+
|
|
197
|
+
- `name`
|
|
198
|
+
- `model`
|
|
199
|
+
|
|
200
|
+
Useful optional parameters:
|
|
201
|
+
|
|
202
|
+
- `id`
|
|
203
|
+
- `instructions`
|
|
204
|
+
- `temperature`
|
|
205
|
+
- `max_output_tokens`
|
|
206
|
+
- `tools`
|
|
207
|
+
- `metadata`
|
|
208
|
+
|
|
209
|
+
### 4. Run an agent
|
|
210
|
+
|
|
211
|
+
Minimal example:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
const result = await sdk.run({
|
|
215
|
+
agent: 'support-bot',
|
|
216
|
+
message: 'Where is order AX-4420?',
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
console.log(result.output_text);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Full example:
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
const result = await sdk.run({
|
|
226
|
+
agent: 'support-bot',
|
|
227
|
+
message: 'Check order AX-4420 and summarize the current status.',
|
|
228
|
+
conversation_id: 1234,
|
|
229
|
+
conversation_key: 'support:customer-42',
|
|
230
|
+
user_id: 42,
|
|
231
|
+
context: {
|
|
232
|
+
userId: 42,
|
|
233
|
+
accountId: 'acc_123',
|
|
234
|
+
},
|
|
235
|
+
messages: [
|
|
236
|
+
{ role: 'user', content: 'Hi' },
|
|
237
|
+
{ role: 'assistant', content: 'How can I help?' },
|
|
238
|
+
],
|
|
239
|
+
max_history_messages: 20,
|
|
240
|
+
maxToolIterations: 8,
|
|
39
241
|
});
|
|
40
242
|
|
|
41
|
-
const result = await sdk.run({ agent: 'support-bot', message: 'Check order AX-4420' });
|
|
42
243
|
console.log(result.output_text);
|
|
244
|
+
console.log(result.usage);
|
|
245
|
+
console.log(result.cost_usd);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Required parameters:
|
|
249
|
+
|
|
250
|
+
- `agent`
|
|
251
|
+
- `message`
|
|
252
|
+
|
|
253
|
+
Useful optional parameters:
|
|
254
|
+
|
|
255
|
+
- `conversation_id`
|
|
256
|
+
- `conversation_key`
|
|
257
|
+
- `user_id`
|
|
258
|
+
- `context`
|
|
259
|
+
- `messages`
|
|
260
|
+
- `max_history_messages`
|
|
261
|
+
- `maxToolIterations`
|
|
262
|
+
- `previous_response_id`
|
|
263
|
+
- `replying_to`
|
|
264
|
+
|
|
265
|
+
### 5. Stream an agent response
|
|
266
|
+
|
|
267
|
+
Minimal example:
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
for await (const delta of sdk.runStream({
|
|
271
|
+
agent: 'support-bot',
|
|
272
|
+
message: 'Give me a short update on order AX-4420.',
|
|
273
|
+
})) {
|
|
274
|
+
process.stdout.write(delta);
|
|
275
|
+
}
|
|
43
276
|
```
|
|
44
277
|
|
|
45
|
-
|
|
278
|
+
Full example:
|
|
46
279
|
|
|
47
280
|
```js
|
|
48
281
|
for await (const delta of sdk.runStream({
|
|
49
282
|
agent: 'support-bot',
|
|
50
|
-
message: '
|
|
283
|
+
message: 'Summarize the latest ticket updates.',
|
|
284
|
+
conversation_key: 'support:customer-42',
|
|
285
|
+
user_id: 42,
|
|
51
286
|
})) {
|
|
52
287
|
process.stdout.write(delta);
|
|
53
288
|
}
|
|
54
289
|
```
|
|
55
290
|
|
|
56
|
-
##
|
|
291
|
+
## Complete minimal working example
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
295
|
+
|
|
296
|
+
const sdk = createClient({
|
|
297
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
sdk.registerTool({
|
|
301
|
+
name: 'lookup_order',
|
|
302
|
+
description: 'Return a fake order status.',
|
|
303
|
+
parameters_schema: {
|
|
304
|
+
type: 'object',
|
|
305
|
+
properties: {
|
|
306
|
+
order_number: { type: 'string' },
|
|
307
|
+
},
|
|
308
|
+
required: ['order_number'],
|
|
309
|
+
},
|
|
310
|
+
handler: async ({ order_number }) => ({
|
|
311
|
+
order_number,
|
|
312
|
+
status: 'processing',
|
|
313
|
+
}),
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
sdk.createAgent({
|
|
317
|
+
name: 'support-bot',
|
|
318
|
+
model: 'gpt-4.1-mini',
|
|
319
|
+
instructions: 'You are a concise support assistant.',
|
|
320
|
+
tools: ['lookup_order'],
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const result = await sdk.run({
|
|
324
|
+
agent: 'support-bot',
|
|
325
|
+
message: 'Check order AX-4420',
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
console.log(result.output_text);
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## Complete proxy example
|
|
57
332
|
|
|
58
333
|
```js
|
|
334
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
335
|
+
|
|
59
336
|
const sdk = createClient({
|
|
60
337
|
backendUrl: 'https://api.example.com',
|
|
61
338
|
mode: 'proxy',
|
|
@@ -66,6 +343,57 @@ const result = await sdk.run({
|
|
|
66
343
|
message: 'Start claim #99',
|
|
67
344
|
conversation_id: 1234,
|
|
68
345
|
});
|
|
346
|
+
|
|
347
|
+
console.log(result.output_text);
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Complete direct + backend persistence example
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
import { createClient } from '@kigathi/ai-agents';
|
|
354
|
+
|
|
355
|
+
const sdk = createClient({
|
|
356
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
357
|
+
backendUrl: 'https://api.example.com',
|
|
358
|
+
mode: 'direct',
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
sdk.registerTool({
|
|
362
|
+
name: 'lookup_order',
|
|
363
|
+
description: 'Find order by order number.',
|
|
364
|
+
parameters_schema: {
|
|
365
|
+
type: 'object',
|
|
366
|
+
properties: {
|
|
367
|
+
order_number: { type: 'string' },
|
|
368
|
+
},
|
|
369
|
+
required: ['order_number'],
|
|
370
|
+
},
|
|
371
|
+
handler: async ({ order_number }) => ({
|
|
372
|
+
order_number,
|
|
373
|
+
status: 'processing',
|
|
374
|
+
}),
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
sdk.createAgent({
|
|
378
|
+
name: 'support-bot',
|
|
379
|
+
model: 'gpt-4.1-mini',
|
|
380
|
+
instructions: 'You are a concise support assistant.',
|
|
381
|
+
tools: ['lookup_order'],
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const result = await sdk.run({
|
|
385
|
+
agent: 'support-bot',
|
|
386
|
+
message: 'Check order AX-4420',
|
|
387
|
+
conversation_id: 1234,
|
|
388
|
+
user_id: 42,
|
|
389
|
+
client_message_id: 'msg_123',
|
|
390
|
+
metadata: {
|
|
391
|
+
source: 'dashboard',
|
|
392
|
+
account_id: 'acc_123',
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
console.log(result.output_text);
|
|
69
397
|
```
|
|
70
398
|
|
|
71
399
|
## Full sample apps
|
|
@@ -74,18 +402,12 @@ const result = await sdk.run({
|
|
|
74
402
|
- `../examples/lyre-ai-agents-node/nuxt-chat` - Nuxt 3 app + server API route + Tailwind
|
|
75
403
|
- `../examples/lyre-ai-agents-node/sveltekit-chat` - SvelteKit app + server endpoint + Tailwind
|
|
76
404
|
|
|
77
|
-
All three use `@kigathi/ai-agents` in `proxy` mode against Axis backend so conversation
|
|
78
|
-
|
|
79
|
-
## Publishing to npm
|
|
80
|
-
|
|
81
|
-
This package is configured for public publishing as a scoped package via:
|
|
82
|
-
|
|
83
|
-
- `name: @kigathi/ai-agents`
|
|
84
|
-
- `publishConfig.access: public`
|
|
405
|
+
All three use `@kigathi/ai-agents` in `proxy` mode against Axis backend so conversation, message, and cost metadata stay in Axis.
|
|
85
406
|
|
|
86
|
-
|
|
407
|
+
## Notes
|
|
87
408
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
409
|
+
- `createClient()` auto-selects `proxy` mode when `backendUrl` is provided without `apiKey`. Otherwise it defaults to `direct`.
|
|
410
|
+
- In direct mode, the consuming app must have the `openai` package installed.
|
|
411
|
+
- In direct mode with `backendUrl`, the SDK still calls OpenAI directly, then asynchronously persists messages and tool events to your backend.
|
|
412
|
+
- You can pass either an agent name/id or a full agent object to `run()`.
|
|
413
|
+
- If proxy mode cannot find a local agent, it will try to resolve the agent from the backend.
|