@agenttool/sdk 0.1.0 → 0.2.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 +206 -18
- package/dist/client.d.ts +5 -0
- package/dist/client.js +8 -0
- package/dist/index.d.ts +1 -0
- package/dist/tools.js +2 -2
- package/dist/traces.d.ts +131 -0
- package/dist/traces.js +167 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,32 +1,220 @@
|
|
|
1
|
-
# agenttool
|
|
1
|
+
# @agenttool/sdk · TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Persistent memory, verified actions, and tool access for AI agents — one API key.
|
|
4
4
|
|
|
5
|
+
[](https://www.npmjs.com/package/@agenttool/sdk)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @agenttool/sdk
|
|
11
|
+
# or
|
|
12
|
+
bun add @agenttool/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What is this?
|
|
16
|
+
|
|
17
|
+
AgentTool gives AI agents the infrastructure they need to operate reliably:
|
|
18
|
+
|
|
19
|
+
| Service | What it does |
|
|
20
|
+
|---------|-------------|
|
|
21
|
+
| **agent-memory** | Persistent semantic memory — store facts, retrieve by similarity |
|
|
22
|
+
| **agent-tools** | Web search, page scraping, code execution |
|
|
23
|
+
| **agent-verify** | SHA-256 proof-of-work attestations with timestamps |
|
|
24
|
+
| **agent-economy** | Wallets, credits, agent-to-agent billing |
|
|
25
|
+
|
|
26
|
+
All four services, one API key, one SDK.
|
|
27
|
+
|
|
28
|
+
## Quick start (60 seconds)
|
|
29
|
+
|
|
30
|
+
**1. Get your API key** — create a free project at [app.agenttool.dev](https://app.agenttool.dev)
|
|
31
|
+
|
|
32
|
+
**2. Set your key:**
|
|
5
33
|
```bash
|
|
6
|
-
|
|
34
|
+
export AT_API_KEY=at_your_key_here
|
|
7
35
|
```
|
|
8
36
|
|
|
37
|
+
**3. Store and retrieve a memory:**
|
|
9
38
|
```typescript
|
|
10
|
-
import { AgentTool } from "agenttool";
|
|
39
|
+
import { AgentTool } from "@agenttool/sdk";
|
|
11
40
|
|
|
12
41
|
const at = new AgentTool(); // reads AT_API_KEY from env
|
|
13
42
|
|
|
14
|
-
//
|
|
15
|
-
await at.memory.store(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
43
|
+
// Store a memory
|
|
44
|
+
const memory = await at.memory.store({
|
|
45
|
+
content: "The user prefers dark mode and concise responses",
|
|
46
|
+
agentId: "my-assistant",
|
|
47
|
+
tags: ["preference", "ui"],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Retrieve it later (semantic search)
|
|
51
|
+
const results = await at.memory.search({
|
|
52
|
+
query: "what does the user prefer?",
|
|
53
|
+
limit: 5,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
for (const result of results) {
|
|
57
|
+
console.log(`${result.score.toFixed(2)} ${result.content}`);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Memory
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { AgentTool } from "@agenttool/sdk";
|
|
67
|
+
|
|
68
|
+
const at = new AgentTool({ apiKey: "at_..." }); // or use AT_API_KEY env var
|
|
69
|
+
|
|
70
|
+
// Store
|
|
71
|
+
const mem = await at.memory.store({
|
|
72
|
+
content: "User is based in London, timezone Europe/London",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Search (semantic)
|
|
76
|
+
const results = await at.memory.search({ query: "where is the user?" });
|
|
77
|
+
|
|
78
|
+
// Retrieve by ID
|
|
79
|
+
const mem2 = await at.memory.get("mem_...");
|
|
80
|
+
|
|
81
|
+
// Delete
|
|
82
|
+
await at.memory.delete("mem_...");
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Tools
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
// Web search
|
|
89
|
+
const results = await at.tools.search({ query: "latest papers on RAG", numResults: 5 });
|
|
90
|
+
for (const r of results) {
|
|
91
|
+
console.log(r.title, r.url);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Scrape a page
|
|
95
|
+
const page = await at.tools.scrape({ url: "https://example.com" });
|
|
96
|
+
console.log(page.text);
|
|
97
|
+
|
|
98
|
+
// Execute code
|
|
99
|
+
const output = await at.tools.execute({ code: "console.log(Math.PI)" });
|
|
100
|
+
console.log(output.stdout);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Verify
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Create an attestation
|
|
107
|
+
const proof = await at.verify.create({
|
|
108
|
+
action: "task_completed",
|
|
109
|
+
agentId: "my-agent",
|
|
110
|
+
payload: { task: "data_analysis", rowsProcessed: 1500 },
|
|
111
|
+
});
|
|
112
|
+
console.log(proof.attestationId, proof.hash);
|
|
113
|
+
|
|
114
|
+
// Verify an attestation
|
|
115
|
+
const result = await at.verify.check("att_...");
|
|
116
|
+
console.log(result.valid); // true
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Economy
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// Create a wallet
|
|
123
|
+
const wallet = await at.economy.createWallet({ name: "agent-wallet" });
|
|
124
|
+
|
|
125
|
+
// Check balance
|
|
126
|
+
const { balance } = await at.economy.getBalance(wallet.id);
|
|
127
|
+
|
|
128
|
+
// Transfer credits
|
|
129
|
+
await at.economy.transfer({
|
|
130
|
+
fromWallet: wallet.id,
|
|
131
|
+
toWallet: "wlt_...",
|
|
132
|
+
amount: 10,
|
|
133
|
+
memo: "payment for search service",
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Integration example — Vercel AI SDK
|
|
19
138
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
139
|
+
```typescript
|
|
140
|
+
import { AgentTool } from "@agenttool/sdk";
|
|
141
|
+
import { tool } from "ai";
|
|
142
|
+
import { z } from "zod";
|
|
24
143
|
|
|
25
|
-
|
|
26
|
-
const v = await at.verify.check("The Earth is round");
|
|
144
|
+
const at = new AgentTool();
|
|
27
145
|
|
|
28
|
-
|
|
29
|
-
|
|
146
|
+
export const memoryTools = {
|
|
147
|
+
remember: tool({
|
|
148
|
+
description: "Store a memory for later retrieval",
|
|
149
|
+
parameters: z.object({ content: z.string() }),
|
|
150
|
+
execute: async ({ content }) => {
|
|
151
|
+
const mem = await at.memory.store({ content, agentId: "vercel-ai-agent" });
|
|
152
|
+
return { id: mem.id, stored: true };
|
|
153
|
+
},
|
|
154
|
+
}),
|
|
155
|
+
recall: tool({
|
|
156
|
+
description: "Search past memories by semantic similarity",
|
|
157
|
+
parameters: z.object({ query: z.string() }),
|
|
158
|
+
execute: async ({ query }) => {
|
|
159
|
+
const results = await at.memory.search({ query, limit: 5 });
|
|
160
|
+
return results.map((r) => ({ content: r.content, score: r.score }));
|
|
161
|
+
},
|
|
162
|
+
}),
|
|
163
|
+
};
|
|
30
164
|
```
|
|
31
165
|
|
|
32
|
-
|
|
166
|
+
## Integration example — any agent loop
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { AgentTool } from "@agenttool/sdk";
|
|
170
|
+
|
|
171
|
+
const at = new AgentTool();
|
|
172
|
+
|
|
173
|
+
async function agentLoop(userMessage: string): Promise<string> {
|
|
174
|
+
// Recall relevant memories
|
|
175
|
+
const memories = await at.memory.search({ query: userMessage, limit: 5 });
|
|
176
|
+
const context = memories.map((m) => m.content).join("\n");
|
|
177
|
+
|
|
178
|
+
// Call your LLM with context
|
|
179
|
+
const response = await yourLLM(`Context:\n${context}\n\nUser: ${userMessage}`);
|
|
180
|
+
|
|
181
|
+
// Store the exchange
|
|
182
|
+
await at.memory.store({ content: `User: ${userMessage}\nAgent: ${response}` });
|
|
183
|
+
|
|
184
|
+
return response;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Free tier
|
|
189
|
+
|
|
190
|
+
| Resource | Free | Seed ($29/mo) | Grow ($99/mo) |
|
|
191
|
+
|----------|------|----------------|----------------|
|
|
192
|
+
| Memory ops/day | 100 | 10,000 | 100,000 |
|
|
193
|
+
| Tool calls/day | 10 | 500 | 5,000 |
|
|
194
|
+
| Verifications/day | 5 | 100 | 1,000 |
|
|
195
|
+
|
|
196
|
+
[Upgrade at app.agenttool.dev/billing](https://app.agenttool.dev/billing)
|
|
197
|
+
|
|
198
|
+
## Configuration
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { AgentTool } from "@agenttool/sdk";
|
|
202
|
+
|
|
203
|
+
const at = new AgentTool({
|
|
204
|
+
apiKey: "at_...", // default: AT_API_KEY env var
|
|
205
|
+
baseUrl: "https://api.agenttool.dev", // default
|
|
206
|
+
timeout: 30_000, // ms, default 30s
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Links
|
|
211
|
+
|
|
212
|
+
- 🏠 [agenttool.dev](https://agenttool.dev)
|
|
213
|
+
- 📖 [docs.agenttool.dev](https://docs.agenttool.dev)
|
|
214
|
+
- 🎛️ [app.agenttool.dev](https://app.agenttool.dev) — dashboard + API key
|
|
215
|
+
- 📦 [npm](https://www.npmjs.com/package/@agenttool/sdk)
|
|
216
|
+
- 🐍 [Python SDK](https://github.com/cambridgetcg/agenttool-sdk-py)
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|
package/dist/client.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { EconomyClient } from "./economy.js";
|
|
5
5
|
import { MemoryClient } from "./memory.js";
|
|
6
6
|
import { ToolsClient } from "./tools.js";
|
|
7
|
+
import { TracesClient } from "./traces.js";
|
|
7
8
|
import { VerifyClient } from "./verify.js";
|
|
8
9
|
/**
|
|
9
10
|
* Unified client for the agenttool.dev platform.
|
|
@@ -20,6 +21,7 @@ import { VerifyClient } from "./verify.js";
|
|
|
20
21
|
* const out = await at.tools.execute("print(42)"); // sandbox
|
|
21
22
|
* const v = await at.verify.check("claim"); // verify
|
|
22
23
|
* const w = await at.economy.createWallet({ name: "w" }); // wallet
|
|
24
|
+
* const t = await at.traces.store({ observations: ["saw X"], conclusion: "do Y" }); // trace
|
|
23
25
|
* ```
|
|
24
26
|
*/
|
|
25
27
|
export declare class AgentTool {
|
|
@@ -28,6 +30,7 @@ export declare class AgentTool {
|
|
|
28
30
|
private _tools;
|
|
29
31
|
private _verify;
|
|
30
32
|
private _economy;
|
|
33
|
+
private _traces;
|
|
31
34
|
/**
|
|
32
35
|
* Create a new AgentTool client.
|
|
33
36
|
*
|
|
@@ -46,5 +49,7 @@ export declare class AgentTool {
|
|
|
46
49
|
get verify(): VerifyClient;
|
|
47
50
|
/** Access the Economy/Wallet API. */
|
|
48
51
|
get economy(): EconomyClient;
|
|
52
|
+
/** Access the Traces (reasoning provenance) API. */
|
|
53
|
+
get traces(): TracesClient;
|
|
49
54
|
toString(): string;
|
|
50
55
|
}
|
package/dist/client.js
CHANGED
|
@@ -5,6 +5,7 @@ import { AgentToolError } from "./errors.js";
|
|
|
5
5
|
import { EconomyClient } from "./economy.js";
|
|
6
6
|
import { MemoryClient } from "./memory.js";
|
|
7
7
|
import { ToolsClient } from "./tools.js";
|
|
8
|
+
import { TracesClient } from "./traces.js";
|
|
8
9
|
import { VerifyClient } from "./verify.js";
|
|
9
10
|
/**
|
|
10
11
|
* Unified client for the agenttool.dev platform.
|
|
@@ -21,6 +22,7 @@ import { VerifyClient } from "./verify.js";
|
|
|
21
22
|
* const out = await at.tools.execute("print(42)"); // sandbox
|
|
22
23
|
* const v = await at.verify.check("claim"); // verify
|
|
23
24
|
* const w = await at.economy.createWallet({ name: "w" }); // wallet
|
|
25
|
+
* const t = await at.traces.store({ observations: ["saw X"], conclusion: "do Y" }); // trace
|
|
24
26
|
* ```
|
|
25
27
|
*/
|
|
26
28
|
export class AgentTool {
|
|
@@ -29,6 +31,7 @@ export class AgentTool {
|
|
|
29
31
|
_tools;
|
|
30
32
|
_verify;
|
|
31
33
|
_economy;
|
|
34
|
+
_traces;
|
|
32
35
|
/**
|
|
33
36
|
* Create a new AgentTool client.
|
|
34
37
|
*
|
|
@@ -70,6 +73,11 @@ export class AgentTool {
|
|
|
70
73
|
this._economy ??= new EconomyClient(this.http);
|
|
71
74
|
return this._economy;
|
|
72
75
|
}
|
|
76
|
+
/** Access the Traces (reasoning provenance) API. */
|
|
77
|
+
get traces() {
|
|
78
|
+
this._traces ??= new TracesClient(this.http);
|
|
79
|
+
return this._traces;
|
|
80
|
+
}
|
|
73
81
|
toString() {
|
|
74
82
|
return `AgentTool(baseUrl=${JSON.stringify(this.http.baseUrl)})`;
|
|
75
83
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,4 +11,5 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export { AgentTool } from "./client.js";
|
|
13
13
|
export { AgentToolError } from "./errors.js";
|
|
14
|
+
export type { Trace, StoreTraceOptions, SearchTracesOptions, TraceSearchResult, TraceChain } from "./traces.js";
|
|
14
15
|
export type { CreateWalletOptions, ExecuteResult, Memory, ScrapeResult, SearchMemoryOptions, SearchResponse, SearchResult, StoreOptions, UsageStats, VerifyResult, Wallet, } from "./types.js";
|
package/dist/tools.js
CHANGED
|
@@ -31,7 +31,7 @@ export class ToolsClient {
|
|
|
31
31
|
query,
|
|
32
32
|
num_results: options?.num_results ?? 5,
|
|
33
33
|
};
|
|
34
|
-
const data = (await this.post("/v1/search", body));
|
|
34
|
+
const data = (await this.post("/v1/search/search", body));
|
|
35
35
|
// Normalize: API may return results at top level or nested
|
|
36
36
|
const results = Array.isArray(data)
|
|
37
37
|
? data
|
|
@@ -49,7 +49,7 @@ export class ToolsClient {
|
|
|
49
49
|
* @returns ScrapeResult with the page content.
|
|
50
50
|
*/
|
|
51
51
|
async scrape(url) {
|
|
52
|
-
const data = await this.post("/v1/scrape", { url });
|
|
52
|
+
const data = await this.post("/v1/scrape/scrape", { url });
|
|
53
53
|
return data;
|
|
54
54
|
}
|
|
55
55
|
/**
|
package/dist/traces.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Traces client for the agent-trace reasoning provenance API.
|
|
3
|
+
*/
|
|
4
|
+
import type { HttpConfig } from "./memory.js";
|
|
5
|
+
/** A stored reasoning trace. */
|
|
6
|
+
export interface Trace {
|
|
7
|
+
id: string;
|
|
8
|
+
trace_id: string;
|
|
9
|
+
agent_id?: string;
|
|
10
|
+
project_id: string;
|
|
11
|
+
session_id?: string;
|
|
12
|
+
created_at: string;
|
|
13
|
+
decision_type: string;
|
|
14
|
+
decision_summary: string;
|
|
15
|
+
output_ref?: string;
|
|
16
|
+
observations: string[];
|
|
17
|
+
hypothesis?: string;
|
|
18
|
+
conclusion: string;
|
|
19
|
+
confidence?: number;
|
|
20
|
+
alternatives?: string[];
|
|
21
|
+
signals?: Record<string, unknown>;
|
|
22
|
+
files_read?: string[];
|
|
23
|
+
key_facts?: string[];
|
|
24
|
+
external_signals?: Record<string, unknown>;
|
|
25
|
+
tags?: string[];
|
|
26
|
+
parent_trace_id?: string;
|
|
27
|
+
}
|
|
28
|
+
/** Options for storing a trace. */
|
|
29
|
+
export interface StoreTraceOptions {
|
|
30
|
+
/** Free-form observation strings that led to the decision. */
|
|
31
|
+
observations: string[];
|
|
32
|
+
/** What was concluded / decided. */
|
|
33
|
+
conclusion: string;
|
|
34
|
+
/** One of: tool_call | memory_write | plan | decision | verification | other */
|
|
35
|
+
decision_type?: string;
|
|
36
|
+
/** Short human-readable summary of the decision. */
|
|
37
|
+
decision_summary?: string;
|
|
38
|
+
agent_id?: string;
|
|
39
|
+
session_id?: string;
|
|
40
|
+
output_ref?: string;
|
|
41
|
+
hypothesis?: string;
|
|
42
|
+
confidence?: number;
|
|
43
|
+
alternatives?: string[];
|
|
44
|
+
tags?: string[];
|
|
45
|
+
parent_trace_id?: string;
|
|
46
|
+
files_read?: string[];
|
|
47
|
+
key_facts?: string[];
|
|
48
|
+
}
|
|
49
|
+
/** A search result entry. */
|
|
50
|
+
export interface TraceSearchResult {
|
|
51
|
+
trace: Trace;
|
|
52
|
+
score: number;
|
|
53
|
+
}
|
|
54
|
+
/** Options for searching traces. */
|
|
55
|
+
export interface SearchTracesOptions {
|
|
56
|
+
/** Maximum number of results (default 10). */
|
|
57
|
+
limit?: number;
|
|
58
|
+
/** Filter by agent_id. */
|
|
59
|
+
agent_id?: string;
|
|
60
|
+
/** Filter by session_id. */
|
|
61
|
+
session_id?: string;
|
|
62
|
+
/** Filter by tag. */
|
|
63
|
+
tag?: string;
|
|
64
|
+
}
|
|
65
|
+
/** A reasoning chain (parent + children). */
|
|
66
|
+
export interface TraceChain {
|
|
67
|
+
parent: Trace;
|
|
68
|
+
children: Trace[];
|
|
69
|
+
depth: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Client for the agent-trace reasoning provenance API.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const at = new AgentTool();
|
|
77
|
+
*
|
|
78
|
+
* // Store a reasoning trace
|
|
79
|
+
* const trace = await at.traces.store({
|
|
80
|
+
* observations: ["User asked about pricing", "Checked tier table"],
|
|
81
|
+
* conclusion: "User is on Free tier, eligible to upgrade",
|
|
82
|
+
* decision_type: "decision",
|
|
83
|
+
* tags: ["billing", "upgrade"],
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* // Search traces semantically
|
|
87
|
+
* const results = await at.traces.search("billing decisions", { limit: 5 });
|
|
88
|
+
*
|
|
89
|
+
* // Retrieve a specific trace
|
|
90
|
+
* const t = await at.traces.get(trace.trace_id);
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare class TracesClient {
|
|
94
|
+
private readonly http;
|
|
95
|
+
/** @internal */
|
|
96
|
+
constructor(http: HttpConfig);
|
|
97
|
+
/**
|
|
98
|
+
* Store a reasoning trace.
|
|
99
|
+
*
|
|
100
|
+
* @param options - Trace content (observations + conclusion required).
|
|
101
|
+
* @returns The created Trace object with its trace_id.
|
|
102
|
+
*/
|
|
103
|
+
store(options: StoreTraceOptions): Promise<Trace>;
|
|
104
|
+
/**
|
|
105
|
+
* Retrieve a trace by its trace_id.
|
|
106
|
+
*
|
|
107
|
+
* @param traceId - The trace_id returned by store().
|
|
108
|
+
*/
|
|
109
|
+
get(traceId: string): Promise<Trace>;
|
|
110
|
+
/**
|
|
111
|
+
* Search traces by semantic similarity.
|
|
112
|
+
*
|
|
113
|
+
* @param query - Natural language query.
|
|
114
|
+
* @param options - Filters: limit, agent_id, session_id, tag.
|
|
115
|
+
* @returns Ranked list of matching traces with similarity scores.
|
|
116
|
+
*/
|
|
117
|
+
search(query: string, options?: SearchTracesOptions): Promise<TraceSearchResult[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Retrieve the reasoning chain for a trace (parent + all children).
|
|
120
|
+
*
|
|
121
|
+
* @param traceId - The parent trace_id.
|
|
122
|
+
*/
|
|
123
|
+
chain(traceId: string): Promise<TraceChain>;
|
|
124
|
+
/**
|
|
125
|
+
* Delete a trace.
|
|
126
|
+
*
|
|
127
|
+
* @param traceId - The trace_id to delete.
|
|
128
|
+
*/
|
|
129
|
+
delete(traceId: string): Promise<void>;
|
|
130
|
+
private _errorDetail;
|
|
131
|
+
}
|
package/dist/traces.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Traces client for the agent-trace reasoning provenance API.
|
|
3
|
+
*/
|
|
4
|
+
import { AgentToolError } from "./errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Client for the agent-trace reasoning provenance API.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const at = new AgentTool();
|
|
11
|
+
*
|
|
12
|
+
* // Store a reasoning trace
|
|
13
|
+
* const trace = await at.traces.store({
|
|
14
|
+
* observations: ["User asked about pricing", "Checked tier table"],
|
|
15
|
+
* conclusion: "User is on Free tier, eligible to upgrade",
|
|
16
|
+
* decision_type: "decision",
|
|
17
|
+
* tags: ["billing", "upgrade"],
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Search traces semantically
|
|
21
|
+
* const results = await at.traces.search("billing decisions", { limit: 5 });
|
|
22
|
+
*
|
|
23
|
+
* // Retrieve a specific trace
|
|
24
|
+
* const t = await at.traces.get(trace.trace_id);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class TracesClient {
|
|
28
|
+
http;
|
|
29
|
+
/** @internal */
|
|
30
|
+
constructor(http) {
|
|
31
|
+
this.http = http;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Store a reasoning trace.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Trace content (observations + conclusion required).
|
|
37
|
+
* @returns The created Trace object with its trace_id.
|
|
38
|
+
*/
|
|
39
|
+
async store(options) {
|
|
40
|
+
const body = {
|
|
41
|
+
observations: options.observations,
|
|
42
|
+
conclusion: options.conclusion,
|
|
43
|
+
decision_type: options.decision_type ?? "decision",
|
|
44
|
+
decision_summary: options.decision_summary ?? options.conclusion.slice(0, 120),
|
|
45
|
+
};
|
|
46
|
+
if (options.agent_id !== undefined)
|
|
47
|
+
body.agent_id = options.agent_id;
|
|
48
|
+
if (options.session_id !== undefined)
|
|
49
|
+
body.session_id = options.session_id;
|
|
50
|
+
if (options.output_ref !== undefined)
|
|
51
|
+
body.output_ref = options.output_ref;
|
|
52
|
+
if (options.hypothesis !== undefined)
|
|
53
|
+
body.hypothesis = options.hypothesis;
|
|
54
|
+
if (options.confidence !== undefined)
|
|
55
|
+
body.confidence = options.confidence;
|
|
56
|
+
if (options.alternatives !== undefined)
|
|
57
|
+
body.alternatives = options.alternatives;
|
|
58
|
+
if (options.tags !== undefined)
|
|
59
|
+
body.tags = options.tags;
|
|
60
|
+
if (options.parent_trace_id !== undefined)
|
|
61
|
+
body.parent_trace_id = options.parent_trace_id;
|
|
62
|
+
if (options.files_read !== undefined)
|
|
63
|
+
body.files_read = options.files_read;
|
|
64
|
+
if (options.key_facts !== undefined)
|
|
65
|
+
body.key_facts = options.key_facts;
|
|
66
|
+
const resp = await globalThis.fetch(`${this.http.baseUrl}/v1/traces`, {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: this.http.headers,
|
|
69
|
+
body: JSON.stringify(body),
|
|
70
|
+
signal: AbortSignal.timeout(this.http.timeout),
|
|
71
|
+
});
|
|
72
|
+
if (resp.status >= 400) {
|
|
73
|
+
const detail = await this._errorDetail(resp);
|
|
74
|
+
throw new AgentToolError(`Traces API error (${resp.status}): ${detail}`);
|
|
75
|
+
}
|
|
76
|
+
const created = (await resp.json());
|
|
77
|
+
// Return the full trace by fetching it
|
|
78
|
+
return this.get(created.trace_id);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Retrieve a trace by its trace_id.
|
|
82
|
+
*
|
|
83
|
+
* @param traceId - The trace_id returned by store().
|
|
84
|
+
*/
|
|
85
|
+
async get(traceId) {
|
|
86
|
+
const resp = await globalThis.fetch(`${this.http.baseUrl}/v1/traces/${traceId}`, {
|
|
87
|
+
headers: this.http.headers,
|
|
88
|
+
signal: AbortSignal.timeout(this.http.timeout),
|
|
89
|
+
});
|
|
90
|
+
if (resp.status >= 400) {
|
|
91
|
+
const detail = await this._errorDetail(resp);
|
|
92
|
+
throw new AgentToolError(`Traces API error (${resp.status}): ${detail}`);
|
|
93
|
+
}
|
|
94
|
+
return (await resp.json());
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Search traces by semantic similarity.
|
|
98
|
+
*
|
|
99
|
+
* @param query - Natural language query.
|
|
100
|
+
* @param options - Filters: limit, agent_id, session_id, tag.
|
|
101
|
+
* @returns Ranked list of matching traces with similarity scores.
|
|
102
|
+
*/
|
|
103
|
+
async search(query, options) {
|
|
104
|
+
const body = {
|
|
105
|
+
query,
|
|
106
|
+
limit: options?.limit ?? 10,
|
|
107
|
+
};
|
|
108
|
+
if (options?.agent_id !== undefined)
|
|
109
|
+
body.agent_id = options.agent_id;
|
|
110
|
+
if (options?.session_id !== undefined)
|
|
111
|
+
body.session_id = options.session_id;
|
|
112
|
+
if (options?.tag !== undefined)
|
|
113
|
+
body.tag = options.tag;
|
|
114
|
+
const resp = await globalThis.fetch(`${this.http.baseUrl}/v1/traces/search`, {
|
|
115
|
+
method: "POST",
|
|
116
|
+
headers: this.http.headers,
|
|
117
|
+
body: JSON.stringify(body),
|
|
118
|
+
signal: AbortSignal.timeout(this.http.timeout),
|
|
119
|
+
});
|
|
120
|
+
if (resp.status >= 400) {
|
|
121
|
+
const detail = await this._errorDetail(resp);
|
|
122
|
+
throw new AgentToolError(`Traces API error (${resp.status}): ${detail}`);
|
|
123
|
+
}
|
|
124
|
+
return (await resp.json());
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Retrieve the reasoning chain for a trace (parent + all children).
|
|
128
|
+
*
|
|
129
|
+
* @param traceId - The parent trace_id.
|
|
130
|
+
*/
|
|
131
|
+
async chain(traceId) {
|
|
132
|
+
const resp = await globalThis.fetch(`${this.http.baseUrl}/v1/traces/chain/${traceId}`, {
|
|
133
|
+
headers: this.http.headers,
|
|
134
|
+
signal: AbortSignal.timeout(this.http.timeout),
|
|
135
|
+
});
|
|
136
|
+
if (resp.status >= 400) {
|
|
137
|
+
const detail = await this._errorDetail(resp);
|
|
138
|
+
throw new AgentToolError(`Traces API error (${resp.status}): ${detail}`);
|
|
139
|
+
}
|
|
140
|
+
return (await resp.json());
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Delete a trace.
|
|
144
|
+
*
|
|
145
|
+
* @param traceId - The trace_id to delete.
|
|
146
|
+
*/
|
|
147
|
+
async delete(traceId) {
|
|
148
|
+
const resp = await globalThis.fetch(`${this.http.baseUrl}/v1/traces/${traceId}`, {
|
|
149
|
+
method: "DELETE",
|
|
150
|
+
headers: this.http.headers,
|
|
151
|
+
signal: AbortSignal.timeout(this.http.timeout),
|
|
152
|
+
});
|
|
153
|
+
if (resp.status >= 400) {
|
|
154
|
+
const detail = await this._errorDetail(resp);
|
|
155
|
+
throw new AgentToolError(`Traces API error (${resp.status}): ${detail}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async _errorDetail(resp) {
|
|
159
|
+
try {
|
|
160
|
+
const json = (await resp.json());
|
|
161
|
+
return json.detail ?? resp.statusText;
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
return resp.statusText;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|