Skip to content

Agent generic should use result type, not Zod schema type #25

Description

@dremnik

Problem

When defining an agent with structured output, you currently have to use typeof Schema as the generic parameter:

const TriageSchema = z.object({
  needs_updating: z.boolean(),
  reason: z.string(),
});

type TriageResult = z.infer<typeof TriageSchema>;

// Awkward - have to use typeof Schema
export const triager = new Agent<RepoContext, typeof TriageSchema>({
  output: TriageSchema,
});

This is unintuitive because the generic should represent what you get back, not the Zod machinery.

Desired API

// Clean - generic is the result type
export const triager = new Agent<RepoContext, TriageResult>({
  output: TriageSchema,
});

Proposed Solution

  1. Change AgentOutputType to represent result types instead of schema types:
// Before
export type AgentOutputType = TextOutput | ZodType;

// After  
export type AgentResultType = string | Record<string, any>;
  1. Update config to accept ZodType<TResult> for the output property:
interface AgentConfig<TContext, TResult = string> {
  output?: ZodType<TResult>;
}
  1. Simplify ResolvedAgentResponse to be identity:
// Before
export type ResolvedAgentResponse<TOutput extends AgentOutputType> =
  TOutput extends TextOutput ? string : TOutput extends ZodType ? z.infer<TOutput> : never;

// After
export type ResolvedAgentResponse<TResult> = TResult;

Files to Update

  • packages/kernl/src/agent/types.ts
  • packages/kernl/src/guardrail.ts
  • packages/kernl/src/agent.ts
  • packages/kernl/src/agent/base.ts
  • packages/kernl/src/thread/thread.ts
  • packages/kernl/src/thread/types.ts
  • packages/kernl/src/thread/utils.ts
  • packages/kernl/src/kernl/kernl.ts
  • packages/kernl/src/lib/error.ts
  • packages/kernl/src/api/resources/agents/agents.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions