@argentic-ai/client 0.7.0 → 0.9.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/README.md +9 -2
- package/dist/argentic.d.ts +20 -6
- package/dist/argentic.js +54 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ To vendor it instead: clone this repo and copy `argentic.ts`, `ai-sdk.ts` and `m
|
|
|
30
30
|
```ts
|
|
31
31
|
import { tool } from 'ai'
|
|
32
32
|
import { z } from 'zod'
|
|
33
|
-
import { Argentic, run } from '@argentic-ai/client'
|
|
33
|
+
import { Argentic, modelFromEnv, run } from '@argentic-ai/client'
|
|
34
34
|
import { fromAiSdkTools } from '@argentic-ai/client/ai-sdk'
|
|
35
35
|
|
|
36
36
|
const tools = { refund_order: tool({
|
|
@@ -46,12 +46,19 @@ const out = await run({
|
|
|
46
46
|
client, agent: 'my-agent', runId: 'order-1182', ledgerPath: '.argentic-ledger.json',
|
|
47
47
|
task: 'Refund order 1182.', system: 'Call one tool per turn.',
|
|
48
48
|
tools: await fromAiSdkTools(tools),
|
|
49
|
+
model: modelFromEnv(), // or { provider: 'openai', id: '...', prices: { in: 0.25, out: 2 } }
|
|
49
50
|
})
|
|
50
51
|
console.log(out.reply)
|
|
51
52
|
```
|
|
52
53
|
|
|
54
|
+
This example also uses the AI SDK and zod (`npm i ai zod`), and `refund` stands for your own billing code. Save it as a `.mjs` file. Before you run it, set `ARGENTIC_API_KEY`, `MODEL_KEY` and the model variables under [Your model](#your-model).
|
|
55
|
+
|
|
53
56
|
`task` is the new event: what happened since the agent last ran, never the earlier messages. Keep `agent` and `runId` the same when a run is retried (the id of the event that woke the agent is a good `runId`). `ledgerPath` is optional: with it, finished actions are written to that file so a killed process does not run them twice; without it they are kept in memory for the call, so make your tools safe to repeat with the `ctx.idempotencyKey` your `execute` receives. A request that takes longer than `timeoutMs` (default 300000, 0 for no limit) counts as a network failure and is retried with the same key. If the ledger file is not valid JSON, `run` warns on stderr, sets the file aside as `<path>.corrupt` and starts empty, so finished actions may run again. `client.replay(agent, wakeId)` re-renders a wake's prompts from the journal and compares them to what the model saw; it calls no model and spends nothing.
|
|
54
57
|
|
|
58
|
+
## Your model
|
|
59
|
+
|
|
60
|
+
You always name the model; there is no default. `model` is `{ provider, id, prices }` with `provider` a named endpoint (`openai`, `anthropic`, `openrouter`, ...), or `{ base_url, id, prices }` with `base_url` any public https OpenAI-compatible endpoint on port 443 (give one of the two, not both). Prices are dollars per million tokens. `modelFromEnv()` reads `ARGENTIC_MODEL_ID`, `ARGENTIC_MODEL_PROVIDER` or `ARGENTIC_MODEL_BASE_URL`, `ARGENTIC_PRICE_IN` and `ARGENTIC_PRICE_OUT`, and throws if the model is not named. A price is never guessed. With neither price variable set, no `prices` are sent: the API uses its own price for a model it has one for (today, Anthropic's models on Anthropic's API) and refuses any other with `prices_required`. One price without the other throws, and so does a price that is not a number. A free model sets both to `0`. This is new in 0.9.0: up to 0.8.0 an unset price counted as 0. `ARGENTIC_BASE_URL` is Argentic's own API, not your model's.
|
|
61
|
+
|
|
55
62
|
## Once per operation (`once_per`, 0.1.7)
|
|
56
63
|
|
|
57
64
|
A tool can declare `once_per`, a key such as `refund:{order_id}` filled in from the call's arguments. Argentic then refuses a second call with the same key, in this wake or any later one. Set it on a `Tool` directly, or pass `{ oncePer: { refund_order: 'refund:{order_id}' } }` as the second argument to `fromAiSdkTools` or `fromMastraTools`. The template is checked before the request is sent. See [TOOLS.md](TOOLS.md#declaring-a-tool-once_per). 0.1.6 and earlier dropped the field.
|
|
@@ -62,7 +69,7 @@ Declare an agent's tools once with `client.putTools(agent, tools)` and leave `to
|
|
|
62
69
|
|
|
63
70
|
```ts
|
|
64
71
|
await client.putTools('support-bot', [{ name: 'book_slot', description: 'Book a slot.', parameters: { type: 'object', properties: { day: { type: 'string' } }, required: ['day'] } }])
|
|
65
|
-
const out = await client.wake('support-bot', { message: 'Book Tuesday.', model:
|
|
72
|
+
const out = await client.wake('support-bot', { message: 'Book Tuesday.', model: modelFromEnv(), context: 'Plan: pro' }, 'book-1')
|
|
66
73
|
```
|
|
67
74
|
|
|
68
75
|
## Structured tool results (0.1.11)
|
package/dist/argentic.d.ts
CHANGED
|
@@ -2,17 +2,30 @@ export declare const defaultBaseUrl = "https://api.getargentic.com";
|
|
|
2
2
|
/** A wake can run a long time, so a try waits five minutes, as Python's client does. */
|
|
3
3
|
export declare const defaultTimeoutMs = 300000;
|
|
4
4
|
export interface ModelSpec {
|
|
5
|
-
|
|
5
|
+
/** A named endpoint (`openai`, `anthropic`, `openrouter`, `nvidia`, ...). Give this or `base_url`, not both. */
|
|
6
|
+
provider?: string;
|
|
7
|
+
/** Any public https OpenAI-compatible endpoint on port 443, for a provider with no preset. Give this or `provider`, not both. */
|
|
8
|
+
base_url?: string;
|
|
6
9
|
id: string;
|
|
7
|
-
/**
|
|
8
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Dollars per million tokens. `cache_hit` is what a cached input token costs, if your provider has a cache price. Leave `prices` out only
|
|
12
|
+
* for a model the API has its own price for (today, Anthropic's models on Anthropic's API); any other model is refused with
|
|
13
|
+
* `prices_required` until you send them. A free model declares `{ in: 0, out: 0 }`.
|
|
14
|
+
*/
|
|
15
|
+
prices?: {
|
|
9
16
|
in: number;
|
|
10
17
|
out: number;
|
|
11
18
|
cache_hit?: number;
|
|
12
19
|
};
|
|
13
20
|
}
|
|
14
|
-
/**
|
|
15
|
-
|
|
21
|
+
/**
|
|
22
|
+
* The model from `ARGENTIC_MODEL_ID` plus `ARGENTIC_MODEL_PROVIDER` (a named endpoint) or `ARGENTIC_MODEL_BASE_URL` (any public https
|
|
23
|
+
* OpenAI-compatible endpoint), and `ARGENTIC_PRICE_IN` / `ARGENTIC_PRICE_OUT` in dollars per million tokens. There is no default model:
|
|
24
|
+
* without `ARGENTIC_MODEL_ID` and one of the two it throws. A price is never guessed either: with neither price set the model carries no
|
|
25
|
+
* `prices` (the API then uses its own price if it has one for that model and refuses the call if it does not), one price without the other
|
|
26
|
+
* throws, and so does a price that is not a number. A free model sets both to 0. `ARGENTIC_BASE_URL` is Argentic's own API, not the model's.
|
|
27
|
+
*/
|
|
28
|
+
export declare function modelFromEnv(e?: Record<string, string | undefined>): ModelSpec;
|
|
16
29
|
/** What Argentic is told about a tool. It is declared, never executed by Argentic. */
|
|
17
30
|
export interface ToolDecl {
|
|
18
31
|
name: string;
|
|
@@ -259,7 +272,8 @@ export interface RunOptions {
|
|
|
259
272
|
* they are kept in memory for this call only (no file is written); then make your tools safe to repeat, with `ctx.idempotencyKey`.
|
|
260
273
|
*/
|
|
261
274
|
ledgerPath?: string;
|
|
262
|
-
model
|
|
275
|
+
/** The model to run, named by you: `{ provider | base_url, id, prices }`. There is no default. */
|
|
276
|
+
model: ModelSpec;
|
|
263
277
|
maxSteps?: number;
|
|
264
278
|
limits?: WakeBody['limits'];
|
|
265
279
|
/** Called just before each wake is sent, so a caller can say it is waiting on the model (the first answer can take several seconds). */
|
package/dist/argentic.js
CHANGED
|
@@ -5,12 +5,42 @@ import { dirname } from 'node:path';
|
|
|
5
5
|
export const defaultBaseUrl = 'https://api.getargentic.com';
|
|
6
6
|
/** A wake can run a long time, so a try waits five minutes, as Python's client does. */
|
|
7
7
|
export const defaultTimeoutMs = 300_000;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The model from `ARGENTIC_MODEL_ID` plus `ARGENTIC_MODEL_PROVIDER` (a named endpoint) or `ARGENTIC_MODEL_BASE_URL` (any public https
|
|
10
|
+
* OpenAI-compatible endpoint), and `ARGENTIC_PRICE_IN` / `ARGENTIC_PRICE_OUT` in dollars per million tokens. There is no default model:
|
|
11
|
+
* without `ARGENTIC_MODEL_ID` and one of the two it throws. A price is never guessed either: with neither price set the model carries no
|
|
12
|
+
* `prices` (the API then uses its own price if it has one for that model and refuses the call if it does not), one price without the other
|
|
13
|
+
* throws, and so does a price that is not a number. A free model sets both to 0. `ARGENTIC_BASE_URL` is Argentic's own API, not the model's.
|
|
14
|
+
*/
|
|
15
|
+
export function modelFromEnv(e = process.env) {
|
|
16
|
+
if (!e.ARGENTIC_MODEL_ID)
|
|
17
|
+
throw new Error('Set ARGENTIC_MODEL_ID to the model you want, with ARGENTIC_MODEL_PROVIDER or ARGENTIC_MODEL_BASE_URL.');
|
|
18
|
+
if (e.ARGENTIC_MODEL_PROVIDER && e.ARGENTIC_MODEL_BASE_URL)
|
|
19
|
+
throw new Error('Set ARGENTIC_MODEL_PROVIDER or ARGENTIC_MODEL_BASE_URL, not both.');
|
|
20
|
+
if (!e.ARGENTIC_MODEL_PROVIDER && !e.ARGENTIC_MODEL_BASE_URL)
|
|
21
|
+
throw new Error('Set ARGENTIC_MODEL_PROVIDER (a named endpoint) or ARGENTIC_MODEL_BASE_URL (a public https OpenAI-compatible endpoint).');
|
|
22
|
+
const where = e.ARGENTIC_MODEL_BASE_URL ? { base_url: e.ARGENTIC_MODEL_BASE_URL } : { provider: e.ARGENTIC_MODEL_PROVIDER };
|
|
23
|
+
const prices = pricesFromEnv(e);
|
|
24
|
+
return { ...where, id: e.ARGENTIC_MODEL_ID, ...(prices && { prices }) };
|
|
25
|
+
}
|
|
26
|
+
/** ARGENTIC_PRICE_IN and ARGENTIC_PRICE_OUT as numbers, or undefined when neither is set. One without the other, or a value that is not a price, throws. */
|
|
27
|
+
function pricesFromEnv(e) {
|
|
28
|
+
const read = (name) => {
|
|
29
|
+
const raw = e[name]?.trim();
|
|
30
|
+
if (!raw)
|
|
31
|
+
return undefined;
|
|
32
|
+
const n = Number(raw);
|
|
33
|
+
if (!Number.isFinite(n) || n < 0)
|
|
34
|
+
throw new Error(`${name} must be a number: the model's price in dollars per million tokens, such as 0.25 (0 for a free model). It is set to "${raw}".`);
|
|
35
|
+
return n;
|
|
36
|
+
};
|
|
37
|
+
const input = read('ARGENTIC_PRICE_IN'), output = read('ARGENTIC_PRICE_OUT');
|
|
38
|
+
if (input === undefined && output === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
if (input === undefined || output === undefined)
|
|
41
|
+
throw new Error(`Set both ARGENTIC_PRICE_IN and ARGENTIC_PRICE_OUT (dollars per million tokens, 0 for a free model): ${input === undefined ? 'ARGENTIC_PRICE_IN' : 'ARGENTIC_PRICE_OUT'} is not set.`);
|
|
42
|
+
return { in: input, out: output };
|
|
43
|
+
}
|
|
14
44
|
const propKeys = ['type', 'description', 'enum'];
|
|
15
45
|
/** True when any top-level property is an object, or an array of objects (or of anything that is not a plain scalar). */
|
|
16
46
|
function isNested(schema) {
|
|
@@ -114,17 +144,30 @@ function refusesResults(e) {
|
|
|
114
144
|
export function hintFor(e) {
|
|
115
145
|
if (!(e instanceof ArgenticError))
|
|
116
146
|
return undefined;
|
|
117
|
-
const
|
|
147
|
+
const body = e.body;
|
|
148
|
+
const code = body?.error;
|
|
149
|
+
// What the API said went wrong, in its own words: the hint names the fix, and this names the cause (which provider, which model id, which field).
|
|
150
|
+
const said = body?.detail === undefined ? '' : ` The API said: ${(typeof body.detail === 'string' ? body.detail : JSON.stringify(body.detail)).slice(0, 300)}`;
|
|
118
151
|
if (e.status === 401)
|
|
119
152
|
return 'Check ARGENTIC_API_KEY. A key only works on the API it was made for, so if you set ARGENTIC_BASE_URL, make the key in that dashboard.';
|
|
153
|
+
if (code === 'model_key_required')
|
|
154
|
+
return 'No model key was sent. Set MODEL_KEY to the key your model provider gave you.';
|
|
120
155
|
if (code === 'model_key_rejected')
|
|
121
|
-
return 'Your model provider refused MODEL_KEY. Check it is a key for the provider in the error
|
|
156
|
+
return 'Your model provider refused MODEL_KEY. Check it is a key for the provider in the error.';
|
|
122
157
|
if (code === 'bad_embed_key')
|
|
123
158
|
return 'The API cannot use EMBED_KEY as sent: EMBED_PROVIDER is cohere (the default) or bedrock, EMBED_REGION goes with bedrock only, and the key has no spaces. Unset EMBED_KEY to recall by words alone.';
|
|
124
159
|
if (code === 'bad_model')
|
|
125
|
-
return '
|
|
160
|
+
return 'The model was refused. Check the provider name and the model id you gave: ARGENTIC_MODEL_PROVIDER or ARGENTIC_MODEL_BASE_URL, and ARGENTIC_MODEL_ID.' + said;
|
|
161
|
+
if (code === 'prices_required')
|
|
162
|
+
return 'Argentic has no price for this model, and it does not guess one. Set ARGENTIC_PRICE_IN and ARGENTIC_PRICE_OUT to its prices in dollars per million tokens, or give prices in the model you pass. A free model is 0 and 0.';
|
|
163
|
+
if (code === 'idempotency_key_reused')
|
|
164
|
+
return 'This run id (the idempotency key) was already used for a different request. To retry, send the same request again. For a new event, or after changing the request, use a new run id.';
|
|
165
|
+
if (code === 'history_resent')
|
|
166
|
+
return 'Send only the new event. Argentic already holds what this agent said and did, so a task that repeats it is refused.' + said;
|
|
167
|
+
if (code === 'bad_request' && said.includes('model.prices'))
|
|
168
|
+
return 'Prices are numbers, in dollars per million tokens, such as 0.25 (0 for a free model), not text.' + said;
|
|
126
169
|
if (e.status === 402)
|
|
127
|
-
return 'This
|
|
170
|
+
return 'This organization has no agent steps left to run a wake. Open Billing in the dashboard at https://getargentic.com/platform/ to see what is left and add more.';
|
|
128
171
|
if (e.status === 429)
|
|
129
172
|
return 'Too many requests a minute for this key. Wait for the time in the error and run the same command again.';
|
|
130
173
|
if (e.status === undefined || e.status >= 500)
|
|
@@ -371,7 +414,7 @@ export async function run(opts) {
|
|
|
371
414
|
throw new Error('run: pass `task` (the new event) or `fireDue: true`, not both and not neither');
|
|
372
415
|
const log = opts.log ?? ((line) => console.log(line));
|
|
373
416
|
const maxSteps = opts.maxSteps ?? 8;
|
|
374
|
-
const model = opts.model
|
|
417
|
+
const model = opts.model;
|
|
375
418
|
const ledger = new Ledger(opts.ledgerPath);
|
|
376
419
|
const byName = new Map(tools.map((t) => [t.name, t]));
|
|
377
420
|
const decls = tools.map(({ name, description, parameters, once_per }) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@argentic-ai/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Argentic client for TypeScript. Argentic is an engine that keeps AI agents right for hours while prompt size is bounded. Adapters for the Vercel AI SDK and Mastra.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|