@argentic-ai/client 0.8.0 → 0.10.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 +10 -4
- package/dist/argentic.d.ts +11 -5
- package/dist/argentic.js +39 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,25 +51,31 @@ const out = await run({
|
|
|
51
51
|
console.log(out.reply)
|
|
52
52
|
```
|
|
53
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
|
+
|
|
54
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.
|
|
55
57
|
|
|
56
58
|
## Your model
|
|
57
59
|
|
|
58
|
-
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. `ARGENTIC_BASE_URL` is Argentic's own API, not your model's.
|
|
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.
|
|
59
61
|
|
|
60
62
|
## Once per operation (`once_per`, 0.1.7)
|
|
61
63
|
|
|
62
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.
|
|
63
65
|
|
|
64
|
-
## Tools,
|
|
66
|
+
## Tools, state and deferred loading (0.1.10)
|
|
65
67
|
|
|
66
|
-
Declare an agent's tools once with `client.putTools(agent, tools)` and leave `tools` out of later wakes; `client.tools(agent)` reads them back. A wake can also carry `
|
|
68
|
+
Declare an agent's tools once with `client.putTools(agent, tools)` and leave `tools` out of later wakes; `client.tools(agent)` reads them back. A wake can also carry `state` (keys and JSON values your app holds, such as the signed-in user's plan; a value the agent already has costs nothing to send again, a changed one is journaled and the agent keeps it, and `null` removes a key) and `tool_choice` (`'auto'`, `'required'` or `{ type: 'tool', name }`). On a tool, `defer_loading: true` keeps its schema out of the prompt until the agent looks it up, which pays off from about ten tools.
|
|
67
69
|
|
|
68
70
|
```ts
|
|
69
71
|
await client.putTools('support-bot', [{ name: 'book_slot', description: 'Book a slot.', parameters: { type: 'object', properties: { day: { type: 'string' } }, required: ['day'] } }])
|
|
70
|
-
const out = await client.wake('support-bot', { message: 'Book Tuesday.', model: modelFromEnv(),
|
|
72
|
+
const out = await client.wake('support-bot', { message: 'Book Tuesday.', model: modelFromEnv(), state: { 'account.plan': 'Pro' } }, 'book-1')
|
|
71
73
|
```
|
|
72
74
|
|
|
75
|
+
## `state` replaces `context` (0.10.0)
|
|
76
|
+
|
|
77
|
+
`context` is gone from `WakeBody`: the API now answers 400 and names `state` when a wake still sends it. Move what you sent as one string into keys (`context: 'Plan: pro'` becomes `state: { 'account.plan': 'Pro' }`) and send them on every wake, or only when they change; an unchanged key costs nothing. Unlike `context`, a value you send is journaled, so the agent still has it on later wakes and can find it by search. Text that came from outside your app (a web page, an email) goes in `outside`, not `state`.
|
|
78
|
+
|
|
73
79
|
## Structured tool results (0.1.11)
|
|
74
80
|
|
|
75
81
|
`run` answers the tools an agent asked for with a text message the model reads, and now also sends the same results as the `results` field of the wake: `[{ seq, result }]`, where `seq` is the action's `seq` and `result` is exactly what the tool returned (or the `{ error }` object when it threw). The API journals each as the real result of that call. The first wake of a run has none, and a result the ledger already held is sent again like the others. Pass `structuredResults: false` to send the text only. If the API you talk to does not know the field yet, it answers 400 and names `results`; the client sends that wake again once without it, under the same key, and leaves it out from then on. Calling `client.wake()` yourself, add `results` to the body next to the `message`, never with `fire_due`, at most 100 entries.
|
package/dist/argentic.d.ts
CHANGED
|
@@ -7,8 +7,12 @@ export interface ModelSpec {
|
|
|
7
7
|
/** Any public https OpenAI-compatible endpoint on port 443, for a provider with no preset. Give this or `provider`, not both. */
|
|
8
8
|
base_url?: string;
|
|
9
9
|
id: string;
|
|
10
|
-
/**
|
|
11
|
-
|
|
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?: {
|
|
12
16
|
in: number;
|
|
13
17
|
out: number;
|
|
14
18
|
cache_hit?: number;
|
|
@@ -17,7 +21,9 @@ export interface ModelSpec {
|
|
|
17
21
|
/**
|
|
18
22
|
* The model from `ARGENTIC_MODEL_ID` plus `ARGENTIC_MODEL_PROVIDER` (a named endpoint) or `ARGENTIC_MODEL_BASE_URL` (any public https
|
|
19
23
|
* OpenAI-compatible endpoint), and `ARGENTIC_PRICE_IN` / `ARGENTIC_PRICE_OUT` in dollars per million tokens. There is no default model:
|
|
20
|
-
* without `ARGENTIC_MODEL_ID` and one of the two it throws.
|
|
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.
|
|
21
27
|
*/
|
|
22
28
|
export declare function modelFromEnv(e?: Record<string, string | undefined>): ModelSpec;
|
|
23
29
|
/** What Argentic is told about a tool. It is declared, never executed by Argentic. */
|
|
@@ -62,8 +68,8 @@ export interface WakeBody {
|
|
|
62
68
|
schedule?: boolean;
|
|
63
69
|
model: ModelSpec;
|
|
64
70
|
system?: string;
|
|
65
|
-
/**
|
|
66
|
-
|
|
71
|
+
/** Keys and JSON values your app holds that the agent should know (the signed-in user's plan, what is in their cart), up to 64 keys a call, a key up to 120 characters on one line, a value up to 2,000 characters as JSON. A value the agent already has costs nothing to send again; a changed one is journaled with this call, so the agent keeps it and can search for it later. `null` removes a key; a key you leave out stays as it is. Replaces `context`, which the API no longer takes. */
|
|
72
|
+
state?: Record<string, unknown>;
|
|
67
73
|
tools?: ToolDecl[];
|
|
68
74
|
/** Whether this call must end with a call to one of your tools. `auto` (default), `required`, or `{ type: 'tool', name }`. */
|
|
69
75
|
tool_choice?: 'auto' | 'required' | {
|
package/dist/argentic.js
CHANGED
|
@@ -8,7 +8,9 @@ export const defaultTimeoutMs = 300_000;
|
|
|
8
8
|
/**
|
|
9
9
|
* The model from `ARGENTIC_MODEL_ID` plus `ARGENTIC_MODEL_PROVIDER` (a named endpoint) or `ARGENTIC_MODEL_BASE_URL` (any public https
|
|
10
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.
|
|
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.
|
|
12
14
|
*/
|
|
13
15
|
export function modelFromEnv(e = process.env) {
|
|
14
16
|
if (!e.ARGENTIC_MODEL_ID)
|
|
@@ -18,7 +20,26 @@ export function modelFromEnv(e = process.env) {
|
|
|
18
20
|
if (!e.ARGENTIC_MODEL_PROVIDER && !e.ARGENTIC_MODEL_BASE_URL)
|
|
19
21
|
throw new Error('Set ARGENTIC_MODEL_PROVIDER (a named endpoint) or ARGENTIC_MODEL_BASE_URL (a public https OpenAI-compatible endpoint).');
|
|
20
22
|
const where = e.ARGENTIC_MODEL_BASE_URL ? { base_url: e.ARGENTIC_MODEL_BASE_URL } : { provider: e.ARGENTIC_MODEL_PROVIDER };
|
|
21
|
-
|
|
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 };
|
|
22
43
|
}
|
|
23
44
|
const propKeys = ['type', 'description', 'enum'];
|
|
24
45
|
/** True when any top-level property is an object, or an array of objects (or of anything that is not a plain scalar). */
|
|
@@ -123,17 +144,30 @@ function refusesResults(e) {
|
|
|
123
144
|
export function hintFor(e) {
|
|
124
145
|
if (!(e instanceof ArgenticError))
|
|
125
146
|
return undefined;
|
|
126
|
-
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)}`;
|
|
127
151
|
if (e.status === 401)
|
|
128
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.';
|
|
129
155
|
if (code === 'model_key_rejected')
|
|
130
156
|
return 'Your model provider refused MODEL_KEY. Check it is a key for the provider in the error.';
|
|
131
157
|
if (code === 'bad_embed_key')
|
|
132
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.';
|
|
133
159
|
if (code === 'bad_model')
|
|
134
|
-
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;
|
|
135
169
|
if (e.status === 402)
|
|
136
|
-
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.';
|
|
137
171
|
if (e.status === 429)
|
|
138
172
|
return 'Too many requests a minute for this key. Wait for the time in the error and run the same command again.';
|
|
139
173
|
if (e.status === undefined || e.status >= 500)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@argentic-ai/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|