Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 8 additions & 91 deletions packages/core/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,104 +22,21 @@ import type {
ToolCallContext,
} from "../tool/tool";
import { ToolSet } from "../tool/tool-set";
import type { VectorFilter, VectorSearchIndex, VectorSearchResult } from "../vector-store";
import type { VectorSearchIndex } from "../vector-store";
import type { PromptHook } from "./hooks";
import { normalizeAgentId } from "./ids";
import { PromptRequest } from "./request";
import type {
AgentEventStoreRegistration,
AgentOptions,
AgentToolOptions,
DynamicContextRegistration,
DynamicToolRegistration,
} from "./types";
import { isStreamingCompletionModel } from "./utils";

export type AgentOptions<M extends CompletionModel = CompletionModel> = {
id: string;
name?: string | undefined;
description?: string | undefined;
model: M;
instructions?: string | undefined;
staticContext?: Document[];
temperature?: number | undefined;
maxTokens?: number | undefined;
additionalParams?: JsonValue | undefined;
toolSet?: ToolSet | undefined;
toolChoice?: ToolChoice | undefined;
defaultMaxTurns?: number | undefined;
hook?: PromptHook | undefined;
outputSchema?: JsonObject | undefined;
observers?: AgentObserverRegistration[] | undefined;
approvals?: ToolApprovalsOptions | undefined;
dynamicContexts?: DynamicContextRegistration[] | undefined;
dynamicTools?: DynamicToolRegistration[] | undefined;
middlewares?: AgentMiddleware[] | undefined;
/**
* @deprecated Use `middlewares` instead.
*/
toolMiddlewares?: AgentMiddleware[] | undefined;
memory?: MemoryRegistration | undefined;
eventStore?: AgentEventStoreRegistration | undefined;
};

export const DEFAULT_MAX_TURNS = 20;

export type AgentToolOptions = {
name: string;
description?: string | undefined;
maxTurns?: number | undefined;
stream?: boolean | undefined;
};

export type AgentEventStoreInclude = "all" | "agent_tool_events";

export type AgentEventStoreOptions = {
include?: AgentEventStoreInclude | undefined;
};

export type AgentEventAppendInput = {
runId: string;
agentId: string;
agentName?: string | undefined;
turn?: number | undefined;
toolName?: string | undefined;
toolCallId?: string | undefined;
internalCallId?: string | undefined;
event: unknown;
};

export type AgentEventRecord = AgentEventAppendInput & {
createdAt?: Date | undefined;
};

export interface AgentEventStore {
append(input: AgentEventAppendInput): Promise<void>;
load(runId: string): Promise<AgentEventRecord[]>;
clear?(runId: string): Promise<void>;
}

export type AgentEventStoreRegistration = {
store: AgentEventStore;
options: Required<AgentEventStoreOptions>;
};

export type DynamicContextOptions<T = unknown> = {
topK: number;
threshold?: number | undefined;
filter?: VectorFilter | undefined;
format?: ((result: VectorSearchResult<T>) => Document) | undefined;
};

export type DynamicContextRegistration<T = unknown> = {
index: VectorSearchIndex<T>;
options: DynamicContextOptions<T>;
};

export type DynamicToolOptions = {
topK: number;
threshold?: number | undefined;
filter?: VectorFilter | undefined;
};

export type DynamicToolRegistration = {
index: VectorSearchIndex<ToolSearchDocument>;
options: DynamicToolOptions;
};

export class Agent<M extends CompletionModel = CompletionModel> {
readonly id: string;
readonly name: string | undefined;
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/agent/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import type { AgentMiddleware, ToolMiddleware } from "../tool/middleware";
import type { AnyTool, ToolApprovalsOptions } from "../tool/tool";
import { ToolSet } from "../tool/tool-set";
import type { VectorSearchIndex } from "../vector-store";
import {
Agent,
type AgentEventStore,
type AgentEventStoreOptions,
type AgentEventStoreRegistration,
type DynamicContextOptions,
type DynamicContextRegistration,
type DynamicToolOptions,
type DynamicToolRegistration,
} from "./agent";
import { Agent } from "./agent";
import type { PromptHook } from "./hooks";
import { normalizeAgentId } from "./ids";
import type {
AgentEventStore,
AgentEventStoreOptions,
AgentEventStoreRegistration,
DynamicContextOptions,
DynamicContextRegistration,
DynamicToolOptions,
DynamicToolRegistration,
} from "./types";

export class AgentBuilder<M extends CompletionModel = CompletionModel> {
private readonly agentId: string;
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export type {
ToolResultMiddlewareArgs,
} from "../tool/middleware";
export { createMiddleware, createToolMiddleware } from "../tool/middleware";
export type {
AgentEventAppendInput,
AgentEventRecord,
AgentEventStore,
AgentEventStoreInclude,
AgentEventStoreOptions,
} from "./agent";
export { AgentBuilder } from "./builder";
export { MaxTurnsError, PromptCancelledError, ToolApprovalRequiredError } from "./errors";
export type {
Expand Down Expand Up @@ -61,3 +54,10 @@ export type {
AgentStreamEvent,
PromptResponse,
} from "./request-types";
export type {
AgentEventAppendInput,
AgentEventRecord,
AgentEventStore,
AgentEventStoreInclude,
AgentEventStoreOptions,
} from "./types";
105 changes: 105 additions & 0 deletions packages/core/src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type {
CompletionModel,
Document,
JsonObject,
JsonValue,
ToolChoice,
} from "../completion/index";
import type { MemoryRegistration } from "../memory";
import type { AgentObserverRegistration } from "../observability";
import type { ToolSearchDocument } from "../tool/dynamic-tools";
import type { AgentMiddleware } from "../tool/middleware";
import type { ToolApprovalsOptions } from "../tool/tool";
import type { ToolSet } from "../tool/tool-set";
import type { VectorFilter, VectorSearchIndex, VectorSearchResult } from "../vector-store";
import type { PromptHook } from "./hooks";

export type AgentOptions<M extends CompletionModel = CompletionModel> = {
id: string;
name?: string | undefined;
description?: string | undefined;
model: M;
instructions?: string | undefined;
staticContext?: Document[];
temperature?: number | undefined;
maxTokens?: number | undefined;
additionalParams?: JsonValue | undefined;
toolSet?: ToolSet | undefined;
toolChoice?: ToolChoice | undefined;
defaultMaxTurns?: number | undefined;
hook?: PromptHook | undefined;
outputSchema?: JsonObject | undefined;
observers?: AgentObserverRegistration[] | undefined;
approvals?: ToolApprovalsOptions | undefined;
dynamicContexts?: DynamicContextRegistration[] | undefined;
dynamicTools?: DynamicToolRegistration[] | undefined;
middlewares?: AgentMiddleware[] | undefined;
/**
* @deprecated Use `middlewares` instead.
*/
toolMiddlewares?: AgentMiddleware[] | undefined;
memory?: MemoryRegistration | undefined;
eventStore?: AgentEventStoreRegistration | undefined;
};

export type AgentToolOptions = {
name: string;
description?: string | undefined;
maxTurns?: number | undefined;
stream?: boolean | undefined;
};

export type AgentEventStoreInclude = "all" | "agent_tool_events";

export type AgentEventStoreOptions = {
include?: AgentEventStoreInclude | undefined;
};

export type AgentEventAppendInput = {
runId: string;
agentId: string;
agentName?: string | undefined;
turn?: number | undefined;
toolName?: string | undefined;
toolCallId?: string | undefined;
internalCallId?: string | undefined;
event: unknown;
};

export type AgentEventRecord = AgentEventAppendInput & {
createdAt?: Date | undefined;
};

export interface AgentEventStore {
append(input: AgentEventAppendInput): Promise<void>;
load(runId: string): Promise<AgentEventRecord[]>;
clear?(runId: string): Promise<void>;
}

export type AgentEventStoreRegistration = {
store: AgentEventStore;
options: Required<AgentEventStoreOptions>;
};

export type DynamicContextOptions<T = unknown> = {
topK: number;
threshold?: number | undefined;
filter?: VectorFilter | undefined;
format?: ((result: VectorSearchResult<T>) => Document) | undefined;
};

export type DynamicContextRegistration<T = unknown> = {
index: VectorSearchIndex<T>;
options: DynamicContextOptions<T>;
};

export type DynamicToolOptions = {
topK: number;
threshold?: number | undefined;
filter?: VectorFilter | undefined;
};

export type DynamicToolRegistration = {
index: VectorSearchIndex<ToolSearchDocument>;
options: DynamicToolOptions;
};
1 change: 1 addition & 0 deletions packages/core/src/internal/agent.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "../agent/agent";
export type * from "../agent/types";
Loading