All files / src types.ts

0% Statements 0/0
0% Branches 1/1
0% Functions 1/1
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * Copyright 2024 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
export type Schema = Record<string, any>;
 
export interface ToolDefinition {
  name: string;
  description?: string;
  inputSchema: Schema;
  outputSchema?: Schema;
}
 
export type ToolArgument = string | ToolDefinition;
 
interface HasMetadata {
  /** Arbitrary metadata to be used by tooling or for informational purposes. */
  metadata?: Record<string, any>;
}
 
export interface PromptRef {
  name: string;
  variant?: string;
  version?: string;
}
 
export interface PromptData extends PromptRef {
  source: string;
}
 
export interface PromptMetadata<ModelConfig = Record<string, any>>
  extends HasMetadata {
  /** The name of the prompt. */
  name?: string;
  /** The variant name for the prompt. */
  variant?: string;
  /** The version of the prompt. */
  version?: string;
  /** A description of the prompt. */
  description?: string;
  /** The name of the model to use for this prompt, e.g.
   * `vertexai/gemini-1.0-pro` */
  model?: string;
  /** Names of tools (registered separately) to allow use of in this prompt. */
  tools?: string[];
  /** Definitions of tools to allow use of in this prompt. */
  toolDefs?: ToolDefinition[];
  /** Model configuration. Not all models support all options. */
  config?: ModelConfig;
  /** Configuration for input variables. */
  input?: {
    /** Defines the default input variable values to use if none are provided. */
    default?: Record<string, any>;
    /** Schema definition for input variables. */
    schema?: Schema;
  };
 
  /** Defines the expected model output format. */
  output?: {
    /** Desired output format for this prompt. */
    format?: string | 'json' | 'text';
    /** Schema defining the output structure. */
    schema?: Schema;
  };
  /**
   * This field will contain the raw frontmatter as parsed with no additional
   * processing or substitutions. If your implementation requires custom fields
   * they will be available here.
   */
  raw?: Record<string, any>;
  /**
   * Fields that contain a period will be considered "extension fields" in the
   * frontmatter and will be gathered by namespace. For example, `myext.foo:
   * 123` would be available at `parsedPrompt.ext.myext.foo`. Nested namespaces
   * will be flattened, so `myext.foo.bar: 123` would be available at
   * `parsedPrompt.ext["myext.foo"].bar`.
   */
  ext?: Record<string, Record<string, any>>;
}
 
export interface ParsedPrompt<ModelConfig = Record<string, any>>
  extends PromptMetadata<ModelConfig> {
  /** The source of the template with metadata / frontmatter already removed. */
  template: string;
}
 
interface EmptyPart extends HasMetadata {
  text?: never;
  data?: never;
  media?: never;
  toolRequest?: never;
  toolResponse?: never;
}
 
export type TextPart = Omit<EmptyPart, 'text'> & { text: string };
export type DataPart = Omit<EmptyPart, 'data'> & { data: Record<string, any> };
export type MediaPart = Omit<EmptyPart, 'media'> & {
  media: { url: string; contentType?: string };
};
export type ToolRequestPart<Input = any> = Omit<EmptyPart, 'toolRequest'> & {
  toolRequest: { name: string; input?: Input; ref?: string };
};
export type ToolResponsePart<Output = any> = Omit<EmptyPart, 'toolResponse'> & {
  toolResponse: { name: string; output?: Output; ref?: string };
};
export type PendingPart = EmptyPart & {
  metadata: { pending: true; [key: string]: any };
};
export type Part =
  | TextPart
  | DataPart
  | MediaPart
  | ToolRequestPart
  | ToolResponsePart
  | PendingPart;
 
// TODO: Check with mbleigh whether it is okay to add 'assistant' here.
// TODO: Also update typing.py depending on what is decided.
export type Role = 'user' | 'model' | 'tool' | 'system' | 'assistant';
 
export interface Message extends HasMetadata {
  role: Role;
  content: Part[];
}
 
export interface Document extends HasMetadata {
  content: Part[];
}
 
/**
 * DataArgument provides all of the information necessary to render a
 * template at runtime.
 **/
export interface DataArgument<Variables = any, State = any> {
  /** Input variables for the prompt template. */
  input?: Variables;
  /** Relevant documents. */
  docs?: Document[];
  /** Previous messages in the history of a multi-turn conversation. */
  messages?: Message[];
  /**
   * Items in the context argument are exposed as `@` variables, e.g.
   * `context: {state: {...}}` is exposed as `@state`.
   **/
  context?: Record<string, any>;
}
 
export type JSONSchema = any;
 
/**
 * SchemaResolver is a function that can resolve a provided schema name to
 * an underlying JSON schema, utilized for shorthand to a schema library
 * provided by an external tool.
 **/
export type SchemaResolver = (
  schemaName: string,
) => JSONSchema | null | Promise<JSONSchema | null>;
 
/**
 * SchemaResolver is a function that can resolve a provided tool name to
 * an underlying ToolDefinition, utilized for shorthand to a tool registry
 * provided by an external library.
 **/
export type ToolResolver = (
  toolName: string,
) => ToolDefinition | null | Promise<ToolDefinition | null>;
 
/**
 * RenderedPrompt is the final result of rendering a Dotprompt template.
 * It includes all of the prompt metadata as well as a set of `messages` to
 * be sent to the  model.
 */
export interface RenderedPrompt<ModelConfig = Record<string, any>>
  extends PromptMetadata<ModelConfig> {
  /** The rendered messages of the prompt. */
  messages: Message[];
}
 
/**
 * PromptFunction is a function that takes runtime data / context and returns
 * a rendered prompt result.
 */
export interface PromptFunction<ModelConfig = Record<string, any>> {
  (
    data: DataArgument,
    options?: PromptMetadata<ModelConfig>,
  ): Promise<RenderedPrompt<ModelConfig>>;
  prompt: ParsedPrompt<ModelConfig>;
}
 
/**
 * PromptRefFunction is a function that takes runtime data / context and returns
 * a rendered prompt result after loading a prompt via reference.
 */
export interface PromptRefFunction<ModelConfig = Record<string, any>> {
  (
    data: DataArgument,
    options?: PromptMetadata<ModelConfig>,
  ): Promise<RenderedPrompt<ModelConfig>>;
  promptRef: PromptRef;
}
 
export interface PaginatedResponse {
  cursor?: string;
}
 
export interface PartialRef {
  name: string;
  variant?: string;
  version?: string;
}
 
export interface PartialData extends PartialRef {
  source: string;
}
 
/**
 * Options for listing prompts with pagination.
 */
export interface ListPromptsOptions {
  /**
   * The cursor to start listing from.
   */
  cursor?: string;
  /**
   * The maximum number of items to return.
   */
  limit?: number;
}
 
/**
 * Options for listing partials with pagination.
 */
export interface ListPartialsOptions {
  /**
   * The cursor to start listing from.
   */
  cursor?: string;
  /**
   * The maximum number of items to return.
   */
  limit?: number;
}
 
/**
 * Options for loading a prompt.
 */
export interface LoadPromptOptions {
  /**
   * The specific variant identifier of the prompt to load.
   */
  variant?: string;
  /**
   * A specific version hash to load. If provided, an error is thrown if the
   * calculated version of the file content does not match this value.
   */
  version?: string;
}
 
/**
 * Options for loading a partial.
 */
export interface LoadPartialOptions {
  /**
   * The specific variant identifier of the partial to load.
   */
  variant?: string;
  /**
   * A specific version hash to load. If provided, an error is thrown if the
   * calculated version of the file content does not match this value.
   */
  version?: string;
}
 
/**
 * Options for deleting a prompt or partial.
 */
export interface DeletePromptOrPartialOptions {
  /**
   * The specific variant identifier to delete. If omitted, targets the
   * default (no variant) file.
   */
  variant?: string;
}
 
/**
 * A paginated list of prompts.
 */
export interface PaginatedPrompts {
  /**
   * The list of prompts.
   */
  prompts: PromptRef[];
  /**
   * The cursor to start the next page of results.
   */
  cursor?: string;
}
 
/**
 * A paginated list of partials.
 */
export interface PaginatedPartials {
  /**
   * The list of partials.
   */
  partials: PartialRef[];
  /**
   * The cursor to start the next page of results.
   */
  cursor?: string;
}
 
/**
 * PromptStore is a common interface that provides for reading and writing
 * prompts and partials.
 */
export interface PromptStore {
  /** Return a list of all prompts in the store (optionally paginated). Some
   * store providers may return limited metadata. */
  list(options?: ListPromptsOptions): Promise<PaginatedPrompts>;
  /** Return a list of partial names available in this store. */
  listPartials(options?: ListPartialsOptions): Promise<PaginatedPartials>;
  /** Retrieve a prompt from the store.  */
  load(name: string, options?: LoadPromptOptions): Promise<PromptData>;
  /** Retrieve a partial from the store. */
  loadPartial(name: string, options?: LoadPartialOptions): Promise<PromptData>;
}
 
/**
 * PromptStoreWritable is a PromptStore that also has built-in methods for
 * writing prompts in addition to reading them.
 */
export interface PromptStoreWritable extends PromptStore {
  /** Save a prompt in the store. May be destructive for prompt stores without
   * versioning. */
  save(prompt: PromptData): Promise<void>;
  /** Delete a prompt from the store. */
  delete(name: string, options?: DeletePromptOrPartialOptions): Promise<void>;
}
 
/**
 * A bundle of prompts and partials.
 */
export interface PromptBundle {
  partials: PartialData[];
  prompts: PromptData[];
}