AiTool.ts overview
Since v1.0.0
Exports Grouped by Category
- Annotations
- Guards
- Models
- Type Ids
- Utility Types
- AnyStructSchema (interface)
- AnyTaggedRequestSchema (interface)
- ByName (type alias)
- Context (type alias)
- Failure (type alias)
- FailureSchema (type alias)
- FromTaggedRequest (interface)
- HandlerEffect (type alias)
- Name (type alias)
- Parameters (type alias)
- ParametersSchema (type alias)
- Success (type alias)
- SuccessSchema (type alias)
- ToHandler (type alias)
- constructors
Annotations
Destructive (class)
Signature
declare class Destructive
Since v1.0.0
Idempotent (class)
Signature
declare class Idempotent
Since v1.0.0
OpenWorld (class)
Signature
declare class OpenWorld
Since v1.0.0
Readonly (class)
Signature
declare class Readonly
Since v1.0.0
Title (class)
Signature
declare class Title
Since v1.0.0
Guards
isAiTool
Signature
declare const isAiTool: (u: unknown) => u is AiTool<any, any, any, any, any>
Since v1.0.0
Models
AiTool (interface)
A AiTool
represents an action that a large language model can take within your application. The results of a tool call can be returned back to the large language model to be incorporated into its next response.
Signature
export interface AiTool<
out Name extends string,
out Parameters extends AnyStructSchema = Schema.Struct<{}>,
out Success extends Schema.Schema.Any = typeof Schema.Void,
out Failure extends Schema.Schema.All = typeof Schema.Never,
out Requirements = never
> extends Pipeable {
readonly [TypeId]: {
readonly _Requirements: Types.Covariant<Requirements>
}
/**
* The name of the tool.
*/
readonly name: Name
/**
* The optional description of the tool.
*/
readonly description?: string | undefined
/**
* A key for the tool, used to identify the tool within a `Context`.
*/
readonly key: string
/**
* A `Schema` representing the type of the parameters that a tool handler
* must be called with.
*/
readonly parametersSchema: Parameters
/**
* A `Schema` representing the type that a tool returns from its handler
* if successful.
*/
readonly successSchema: Success
/**
* A `Schema` representing the type that a tool returns from its handler
* if it fails.
*/
readonly failureSchema: Failure
readonly annotations: Context_.Context<never>
/**
* Adds a requirement on a particular service for the tool call to be able to
* be executed.
*/
addRequirement<Requirement>(): AiTool<Name, Parameters, Success, Failure, Requirements | Requirement>
/**
* Set the schema to use for tool handler success.
*/
setSuccess<SuccessSchema extends Schema.Schema.Any>(
schema: SuccessSchema
): AiTool<Name, Parameters, SuccessSchema, Failure, Requirements>
/**
* Set the schema to use for tool handler failure.
*/
setFailure<FailureSchema extends Schema.Schema.Any>(
schema: FailureSchema
): AiTool<Name, Parameters, Success, FailureSchema, Requirements>
/**
* Set the schema for the tool parameters.
*/
setParameters<ParametersSchema extends Schema.Struct<any> | Schema.Struct.Fields>(
schema: ParametersSchema
): AiTool<
Name,
ParametersSchema extends Schema.Struct<infer _>
? ParametersSchema
: ParametersSchema extends Schema.Struct.Fields
? Schema.Struct<ParametersSchema>
: never,
Success,
Failure,
Requirements
>
/**
* Add an annotation to the tool.
*/
annotate<I, S>(tag: Context_.Tag<I, S>, value: S): AiTool<Name, Parameters, Success, Failure, Requirements>
/**
* Add many annotations to the tool.
*/
annotateContext<I>(context: Context_.Context<I>): AiTool<Name, Parameters, Success, Failure, Requirements>
}
Since v1.0.0
Any (interface)
Signature
export interface Any extends Pipeable {
readonly [TypeId]: {
readonly _Requirements: Types.Covariant<any>
}
readonly name: string
readonly description?: string | undefined
readonly key: string
readonly parametersSchema: AnyStructSchema
readonly annotations: Context_.Context<never>
}
Since v1.0.0
AnyWithProtocol (interface)
Signature
export interface AnyWithProtocol extends Any {
readonly successSchema: Schema.Schema.Any
readonly failureSchema: Schema.Schema.All
}
Since v1.0.0
Handler (interface)
Represents an AiTool
that has been implemented within the application.
Signature
export interface Handler<Name extends string> {
readonly _: unique symbol
readonly name: Name
readonly handler: (params: any) => Effect.Effect<any, any>
readonly context: Context_.Context<never>
}
Since v1.0.0
HandlerResult (interface)
Represents the result of calling the handler for a particular tool.
Signature
export interface HandlerResult<Tool extends Any> {
/**
* The result of executing the handler for a particular tool.
*/
readonly result: Success<Tool>
/**
* The encoded result of executing the handler for a particular tool, which
* is suitable for returning back to the large language model for
* incorporation into further responses.
*/
readonly encodedResult: unknown
}
Since v1.0.0
Type Ids
TypeId
Signature
declare const TypeId: unique symbol
Since v1.0.0
TypeId (type alias)
Signature
type TypeId = typeof TypeId
Since v1.0.0
Utility Types
AnyStructSchema (interface)
Signature
export interface AnyStructSchema extends Pipeable {
readonly [Schema.TypeId]: any
readonly make: any
readonly Type: any
readonly Encoded: any
readonly Context: any
readonly ast: AST.AST
readonly fields: Schema.Struct.Fields
readonly annotations: any
}
Since v1.0.0
AnyTaggedRequestSchema (interface)
Signature
export interface AnyTaggedRequestSchema extends AnyStructSchema {
readonly _tag: string
readonly success: Schema.Schema.Any
readonly failure: Schema.Schema.All
}
Since v1.0.0
ByName (type alias)
A utility mapped type which associates tool names with tools.
Signature
type ByName<Tools> = {
readonly [Tool in Tools as Tool["name"]]: Tool
}
Since v1.0.0
Context (type alias)
A utility type to the Context
type from an AiTool
.
Signature
type Context<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Parameters["Context"] | _Success["Context"] | _Failure["Context"] | _Requirements
: never
Since v1.0.0
Failure (type alias)
A utility type to extract the type of the response that an AiTool
returns from its handler if it fails.
Signature
type Failure<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Failure["Type"]
: never
Since v1.0.0
FailureSchema (type alias)
A utility type to extract the schema type of the response that an AiTool
returns from its handler if it fails.
Signature
type FailureSchema<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Failure
: never
Since v1.0.0
FromTaggedRequest (interface)
A utility type to convert a Schema.TaggedRequest
into an AiTool
.
Signature
export interface FromTaggedRequest<S extends AnyTaggedRequestSchema>
extends AiTool<S["_tag"], S, S["success"], S["failure"]> {}
Since v1.0.0
HandlerEffect (type alias)
A utility type which returns the type of the Effect
that will be used to resolve a tool call.
Signature
type HandlerEffect<Tool> = [Tool] extends [
AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
]
? Effect.Effect<
_Success["Type"],
AiError | _Failure["Type"],
_Parameters["Context"] | _Success["Context"] | _Failure["Context"] | _Requirements
>
: never
Since v1.0.0
Name (type alias)
A utility type to extract the Name
type from an AiTool
.
Signature
type Name<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Name
: never
Since v1.0.0
Parameters (type alias)
A utility type to extract the type of the parameters which an AiTool
must be called with.
Signature
type Parameters<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Parameters["Type"]
: never
Since v1.0.0
ParametersSchema (type alias)
A utility type to extract the schema type of the parameters which an AiTool
must be called with.
Signature
type ParametersSchema<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Parameters
: never
Since v1.0.0
Success (type alias)
A utility type to extract the type of the response that an AiTool
returns from its handler if successful.
Signature
type Success<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Success["Type"]
: never
Since v1.0.0
SuccessSchema (type alias)
A utility type to extract the schema type of the response that an AiTool
returns from its handler if successful.
Signature
type SuccessSchema<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? _Success
: never
Since v1.0.0
ToHandler (type alias)
A utility type which returns the handler type for an AiTool
.
Signature
type ToHandler<Tool> =
Tool extends AiTool<infer _Name, infer _Parameters, infer _Success, infer _Failure, infer _Requirements>
? Handler<_Name>
: never
Since v1.0.0
constructors
fromTaggedRequest
Constructs a new AiTool
from a Schema.TaggedRequest
.
Signature
declare const fromTaggedRequest: <S extends AnyTaggedRequestSchema>(schema: S) => FromTaggedRequest<S>
Since v1.0.0
make
Constructs an AiTool
from a name and, optionally, a specification for the tool call’s protocol.
Signature
declare const make: <
const Name extends string,
Parameters extends Schema.Struct.Fields = {},
Success extends Schema.Schema.Any = typeof Schema.Void,
Failure extends Schema.Schema.All = typeof Schema.Never
>(
name: Name,
options?: {
readonly description?: string | undefined
readonly parameters?: Parameters
readonly success?: Success
readonly failure?: Failure
}
) => AiTool<Name, Schema.Struct<Parameters>, Success, Failure>
Since v1.0.0