agent-lattice 0.9.13 → 0.9.15

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 CHANGED
@@ -56,6 +56,28 @@ Pass `{ stream: false }` to disable model streaming for a query:
56
56
  const result = await agent.prompt("Say hello", { stream: false });
57
57
  ```
58
58
 
59
+ Pass `outputFormat` when you want the model to produce JSON matching a schema:
60
+
61
+ ```ts
62
+ const jsonResult = await agent.prompt("Return JSON only.", {
63
+ outputFormat: "json",
64
+ });
65
+
66
+ const result = await agent.prompt("Return the answer to 2 + 2.", {
67
+ outputFormat: {
68
+ type: "json_schema",
69
+ schema: {
70
+ type: "object",
71
+ properties: {
72
+ answer: { type: "number" },
73
+ },
74
+ required: ["answer"],
75
+ additionalProperties: false,
76
+ },
77
+ },
78
+ });
79
+ ```
80
+
59
81
  ## Multimodal Input
60
82
 
61
83
  Pass Anthropic-compatible content blocks for image or document prompts:
@@ -227,6 +249,53 @@ const result = await agent.prompt("What is 2+2?");
227
249
  console.log(result.result);
228
250
  ```
229
251
 
252
+ ## Business Context For Tools
253
+
254
+ Pass host application data through `context`. The SDK gives that context to
255
+ tool handlers, but does not automatically put it into the model transcript. The
256
+ model sees the data only if a tool returns it.
257
+
258
+ ```ts
259
+ import { createBareAgent, tool } from "agent-lattice";
260
+ import { z } from "zod/v4";
261
+
262
+ type QcContext = {
263
+ patientRecordId: string;
264
+ scoringStandardId: string;
265
+ };
266
+
267
+ const readPatientRecordInput = z.object({});
268
+ const qcTool = tool<QcContext>();
269
+
270
+ const readPatientRecord = qcTool(
271
+ "read_patient_record",
272
+ "Read the current patient record",
273
+ readPatientRecordInput,
274
+ async (_input, { context }) => {
275
+ return {
276
+ content: JSON.stringify({
277
+ patientRecordId: context?.patientRecordId,
278
+ scoringStandardId: context?.scoringStandardId,
279
+ }),
280
+ };
281
+ },
282
+ );
283
+
284
+ const agent = createBareAgent<QcContext>({
285
+ apiKey: process.env.DEEPSEEK_API_KEY,
286
+ baseURL: "https://api.deepseek.com/anthropic",
287
+ model: "deepseek-v4-flash",
288
+ tools: [readPatientRecord],
289
+ });
290
+
291
+ const result = await agent.prompt("Review the current patient record.", {
292
+ context: {
293
+ patientRecordId: "ocr_123",
294
+ scoringStandardId: "tumor-treatment-process-v1",
295
+ },
296
+ });
297
+ ```
298
+
230
299
  ## Permission Callback
231
300
 
232
301
  ```ts
@@ -361,7 +430,7 @@ const mcp = await connectMCPStreamableHTTPServer("https://mcp.example.com/mcp",
361
430
  `Agent` and `Team` satisfy the same `AgentLike` shape:
362
431
 
363
432
  ```ts
364
- type AgentLike = {
433
+ type AgentLike<TContext = unknown> = {
365
434
  query(prompt, options?): AsyncGenerator<SDKMessage | TeamRunnerMessage>;
366
435
  prompt(prompt, options?): Promise<SDKResultMessage>;
367
436
  };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { DocumentBlockParam, ImageBlockParam, TextBlockParam } from "@anthropic-ai/sdk/resources/messages.mjs";
2
+ import type { BetaJSONOutputFormat } from "@anthropic-ai/sdk/resources/beta/messages/messages.mjs";
2
3
  import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js";
3
4
  import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
4
5
  import { type StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
@@ -21,9 +22,9 @@ export type ToolResultBlock = {
21
22
  };
22
23
  export type ContentBlock = TextBlock | ImageBlock | DocumentBlock | ToolUseBlock | ToolResultBlock;
23
24
  export type AgentLikeEvent = SDKMessage | TeamRunnerMessage;
24
- export type AgentLike = {
25
- query(prompt: string | ContentBlock[], options?: QueryOptions): AsyncGenerator<AgentLikeEvent>;
26
- prompt(prompt: string | ContentBlock[], options?: QueryOptions): Promise<SDKResultMessage>;
25
+ export type AgentLike<TContext = unknown> = {
26
+ query(prompt: string | ContentBlock[], options?: QueryOptions<TContext>): AsyncGenerator<AgentLikeEvent>;
27
+ prompt(prompt: string | ContentBlock[], options?: QueryOptions<TContext>): Promise<SDKResultMessage>;
27
28
  };
28
29
  export type DelegateWaitMode = "result" | "accepted";
29
30
  export type AgentRuntimeSource = {
@@ -36,7 +37,7 @@ export type AgentRuntimeSource = {
36
37
  export type AgentRuntimeDelegateInput = {
37
38
  name: string;
38
39
  description?: string;
39
- agent: AgentLike;
40
+ agent: AgentLike<any>;
40
41
  task: string;
41
42
  wait?: DelegateWaitMode;
42
43
  targetMailboxId?: string;
@@ -149,6 +150,7 @@ export type ModelRequest = {
149
150
  messages: ModelMessage[];
150
151
  tools: ModelToolDefinition[];
151
152
  stream: boolean;
153
+ outputFormat?: OutputFormat;
152
154
  onStreamEvent?: (event: Record<string, unknown>) => void;
153
155
  signal?: AbortSignal;
154
156
  };
@@ -158,19 +160,21 @@ export interface ModelClient {
158
160
  export type ToolResult = {
159
161
  content: string | ContentBlock[];
160
162
  };
161
- export type ToolHandler<TInput = unknown> = (input: TInput, context: {
163
+ export type ToolExecutionContext<TContext = unknown> = {
162
164
  signal?: AbortSignal;
163
165
  toolUseId: string;
164
- runtime?: AgentRuntimeContext;
166
+ context?: TContext;
167
+ agentRuntime?: AgentRuntimeContext;
165
168
  permissions?: RuntimePermissions;
166
- }) => Promise<ToolResult> | ToolResult;
167
- export type ToolDefinition<TInput = unknown> = {
169
+ };
170
+ export type ToolHandler<TInput = unknown, TContext = unknown> = (input: TInput, context: ToolExecutionContext<TContext>) => Promise<ToolResult> | ToolResult;
171
+ export type ToolDefinition<TInput = unknown, TContext = unknown> = {
168
172
  name: string;
169
173
  description: string;
170
174
  inputSchema: unknown;
171
175
  jsonSchema: Record<string, unknown>;
172
176
  parse(input: unknown): TInput;
173
- handler: ToolHandler<TInput>;
177
+ handler: ToolHandler<TInput, TContext>;
174
178
  };
175
179
  export type SkillDefinition = {
176
180
  name: string;
@@ -220,7 +224,7 @@ export type MCPStdioServerOptions = MCPToolsOptions & {
220
224
  };
221
225
  export type MCPStdioConnection = {
222
226
  client: MCPClient;
223
- tools: Array<ToolDefinition<any>>;
227
+ tools: Array<ToolDefinition<any, any>>;
224
228
  close(): Promise<void>;
225
229
  };
226
230
  export type MCPStreamableHTTPServerOptions = MCPToolsOptions & {
@@ -242,7 +246,7 @@ export type TeamMemberDefinition = {
242
246
  name: string;
243
247
  role: TeamMemberRole;
244
248
  focus?: string;
245
- agent: AgentLike;
249
+ agent: AgentLike<any>;
246
250
  mailboxId?: string;
247
251
  };
248
252
  export type TeamMemberInput = TeamMemberDefinition;
@@ -294,18 +298,18 @@ export type SQLiteMailboxOptions = {
294
298
  };
295
299
  export type TeamOptions = {
296
300
  name: string;
297
- lead: Agent;
301
+ lead: Agent<any>;
298
302
  members: TeamMemberDefinition[];
299
303
  mailbox?: TeamMailbox;
300
304
  exposeLeadMailboxTools?: boolean;
301
305
  };
302
306
  export type Team = {
303
307
  name: string;
304
- lead: Agent;
308
+ lead: Agent<any>;
305
309
  members: TeamMemberDefinition[];
306
310
  mailbox: TeamMailbox;
307
- tools: Array<ToolDefinition<any>>;
308
- memberTools: Record<string, Array<ToolDefinition<any>>>;
311
+ tools: Array<ToolDefinition<any, any>>;
312
+ memberTools: Record<string, Array<ToolDefinition<any, any>>>;
309
313
  send(from: string, to: string, content: string, options?: TeamSendOptions): Promise<TeamMessage>;
310
314
  drain(options?: TeamDrainOptions): Promise<TeamDrainResult>;
311
315
  query(prompt: string | ContentBlock[], options?: QueryOptions): AsyncGenerator<TeamRunnerMessage>;
@@ -323,13 +327,13 @@ export type TeamDrainResult = {
323
327
  };
324
328
  export type TeamRunnerOptions = {
325
329
  team?: Team;
326
- root?: AgentLike;
330
+ root?: AgentLike<any>;
327
331
  mailbox?: TeamMailbox;
328
332
  source?: AgentRuntimeSource;
329
333
  maxDelegateDepth?: number;
330
334
  };
331
335
  export type TeamRunner = {
332
- root: AgentLike;
336
+ root: AgentLike<any>;
333
337
  mailbox: TeamMailbox;
334
338
  query(prompt: string | ContentBlock[], options?: QueryOptions): AsyncGenerator<TeamRunnerMessage>;
335
339
  prompt(prompt: string | ContentBlock[], options?: QueryOptions): Promise<SDKResultMessage>;
@@ -350,7 +354,7 @@ export type AgentWorkspaceOptions = string | {
350
354
  allowedDirectories?: string[];
351
355
  bashTimeoutMs?: number;
352
356
  };
353
- export type AgentOptions = {
357
+ export type AgentOptions<TContext = unknown> = {
354
358
  apiKey?: string;
355
359
  baseURL?: string;
356
360
  name?: string;
@@ -358,26 +362,29 @@ export type AgentOptions = {
358
362
  systemPrompt?: string;
359
363
  maxTokens?: number;
360
364
  maxTurns?: number;
361
- tools?: Array<ToolDefinition<any>>;
365
+ tools?: Array<ToolDefinition<any, TContext>>;
362
366
  skills?: SkillDefinition[];
363
367
  workspace?: AgentWorkspaceOptions;
364
368
  permission?: (request: PermissionRequest) => Promise<PermissionDecision> | PermissionDecision;
365
369
  modelClient?: ModelClient;
366
370
  tracer?: ContextTracer;
367
371
  };
368
- export type BareAgentOptions = Omit<AgentOptions, "workspace">;
372
+ export type BareAgentOptions<TContext = unknown> = Omit<AgentOptions<TContext>, "workspace">;
369
373
  export type AgentWorkspaceToolsOptions = {
370
374
  cwd?: string;
371
375
  allowedDirectories?: string[];
372
376
  bashTimeoutMs?: number;
373
377
  };
374
- export type QueryOptions = {
378
+ export type QueryOptions<TContext = unknown> = {
375
379
  stream?: boolean;
380
+ outputFormat?: OutputFormat;
376
381
  signal?: AbortSignal;
377
- runtime?: AgentRuntimeContext;
382
+ context?: TContext;
383
+ agentRuntime?: AgentRuntimeContext;
378
384
  permissions?: RuntimePermissions;
379
385
  tracer?: ContextTracer;
380
386
  };
387
+ export type OutputFormat = "json" | BetaJSONOutputFormat;
381
388
  export type SDKSystemInitMessage = {
382
389
  type: "system";
383
390
  subtype: "init";
@@ -454,7 +461,8 @@ export declare class ToolPermissionDeniedError extends AgentSDKError {
454
461
  readonly denial: PermissionDenial;
455
462
  constructor(denial: PermissionDenial);
456
463
  }
457
- export declare function tool<TSchema>(name: string, description: string, inputSchema: TSchema, handler: ToolHandler<InferInput<TSchema>>): ToolDefinition<InferInput<TSchema>>;
464
+ export declare function tool<TContext = unknown>(): <TSchema>(name: string, description: string, inputSchema: TSchema, handler: ToolHandler<InferInput<TSchema>, TContext>) => ToolDefinition<InferInput<TSchema>, TContext>;
465
+ export declare function tool<TSchema, TContext = unknown>(name: string, description: string, inputSchema: TSchema, handler: ToolHandler<InferInput<TSchema>, TContext>): ToolDefinition<InferInput<TSchema>, TContext>;
458
466
  export type DelegateToolOptions = {
459
467
  wait?: DelegateWaitMode;
460
468
  targetMailboxId?: string;
@@ -484,13 +492,13 @@ export type AgentToolOptions = {
484
492
  description: string;
485
493
  targetMailboxId?: string;
486
494
  };
487
- export declare function agentTool(name: string, agent: AgentLike, options: AgentToolOptions): ToolDefinition<AgentToolInput>;
488
- export declare function delegateTool(name: string, description: string, agent: AgentLike, options?: DelegateToolOptions): ToolDefinition<{
495
+ export declare function agentTool(name: string, agent: AgentLike<any>, options: AgentToolOptions): ToolDefinition<AgentToolInput>;
496
+ export declare function delegateTool(name: string, description: string, agent: AgentLike<any>, options?: DelegateToolOptions): ToolDefinition<{
489
497
  task: string;
490
498
  }>;
491
- export declare function createAgent(options: AgentOptions): Agent;
492
- export declare function createBareAgent(options: BareAgentOptions): Agent;
493
- export declare function createBuiltinTools(options?: AgentWorkspaceToolsOptions): Array<ToolDefinition<any>>;
499
+ export declare function createAgent<TContext = unknown>(options: AgentOptions<TContext>): Agent<TContext>;
500
+ export declare function createBareAgent<TContext = unknown>(options: BareAgentOptions<TContext>): Agent<TContext>;
501
+ export declare function createBuiltinTools(options?: AgentWorkspaceToolsOptions): Array<ToolDefinition<any, any>>;
494
502
  export declare function createJsonlContextTracer(options: JsonlContextTracerOptions): ContextTracer;
495
503
  export declare function createCompositeContextTracer(tracers: Array<ContextTracer | undefined | null>): ContextTracer;
496
504
  export declare function createLangSmithContextTracer(options: LangSmithContextTracerOptions): ContextTracer;
@@ -504,21 +512,23 @@ export declare function createMemoryMailbox(): TeamMailbox;
504
512
  export declare function createSQLiteMailbox(options: SQLiteMailboxOptions): TeamMailbox;
505
513
  export declare function createTeam(options: TeamOptions): Team;
506
514
  export declare function createTeamRunner(options: TeamRunnerOptions): TeamRunner;
507
- export declare function createAgentWorkspaceTools(options?: AgentWorkspaceToolsOptions): Array<ToolDefinition<any>>;
508
- export declare function query(params: AgentOptions & {
515
+ export declare function createAgentWorkspaceTools(options?: AgentWorkspaceToolsOptions): Array<ToolDefinition<any, any>>;
516
+ export declare function query<TContext = unknown>(params: AgentOptions<TContext> & {
509
517
  prompt: string;
510
518
  stream?: boolean;
519
+ outputFormat?: OutputFormat;
511
520
  signal?: AbortSignal;
521
+ context?: TContext;
512
522
  }): AsyncGenerator<SDKMessage>;
513
- export declare class Agent {
523
+ export declare class Agent<TContext = unknown> {
514
524
  private readonly options;
515
525
  private readonly modelClient;
516
526
  private readonly messages;
517
527
  private readonly sessionId;
518
- constructor(options: AgentOptions);
519
- query(prompt: string | ContentBlock[], options?: QueryOptions): AsyncGenerator<SDKMessage>;
520
- prompt(prompt: string | ContentBlock[], options?: QueryOptions): Promise<SDKResultMessage>;
521
- addTools(tools: Array<ToolDefinition<any>>): void;
528
+ constructor(options: AgentOptions<TContext>);
529
+ query(prompt: string | ContentBlock[], options?: QueryOptions<TContext>): AsyncGenerator<SDKMessage>;
530
+ prompt(prompt: string | ContentBlock[], options?: QueryOptions<TContext>): Promise<SDKResultMessage>;
531
+ addTools(tools: Array<ToolDefinition<any, TContext>>): void;
522
532
  private initMessage;
523
533
  private resultMessage;
524
534
  private modelTools;