ai-functions
Version:
Core AI primitives for building intelligent applications
229 lines • 7.03 kB
TypeScript
/**
* DigitalObjectsFunctionRegistry - Persistent function registry using digital-objects
*
* This implementation stores function definitions as Things and function calls as Actions,
* providing full persistence and audit trail capabilities.
*
* Nouns:
* - CodeFunction: Functions that generate executable code
* - GenerativeFunction: Functions that use AI to generate content
* - AgenticFunction: Functions that run in a loop with tools
* - HumanFunction: Functions that require human input/approval
*
* Verbs:
* - define: Function definition action
* - call: Function invocation action
* - complete: Successful completion action
* - fail: Failed execution action
*
* @packageDocumentation
*/
import type { DigitalObjectsProvider, Thing, Action } from 'digital-objects';
import type { FunctionRegistry, DefinedFunction } from './types.js';
/**
* Noun names for function types
*/
export declare const FUNCTION_NOUNS: {
readonly CODE: "CodeFunction";
readonly GENERATIVE: "GenerativeFunction";
readonly AGENTIC: "AgenticFunction";
readonly HUMAN: "HumanFunction";
};
/**
* Verb names for function actions
*/
export declare const FUNCTION_VERBS: {
readonly DEFINE: "define";
readonly CALL: "call";
readonly COMPLETE: "complete";
readonly FAIL: "fail";
};
/**
* Stored function definition shape
*/
export interface StoredFunctionDefinition {
name: string;
type: 'code' | 'generative' | 'agentic' | 'human';
description?: string;
args: unknown;
returnType?: unknown;
/** Inline deterministic code body for a `code` function (handlers are not persistable). */
code?: string;
language?: string;
instructions?: string;
/** @deprecated Legacy code-authoring fields; retained only for back-compat reads. */
includeTests?: boolean;
/** @deprecated Legacy code-authoring fields; retained only for back-compat reads. */
includeExamples?: boolean;
output?: string;
system?: string;
promptTemplate?: string;
model?: string;
temperature?: number;
tools?: unknown[];
maxIterations?: number;
stream?: boolean;
channel?: string;
timeout?: number;
assignee?: string;
}
/**
* Function call data stored in actions
*/
export interface FunctionCallData {
args: unknown;
result?: unknown;
error?: string;
duration?: number;
}
/**
* Options for creating a DigitalObjectsFunctionRegistry
*/
export interface DigitalObjectsRegistryOptions {
/** The digital-objects provider to use for storage */
provider: DigitalObjectsProvider;
/** Whether to auto-initialize nouns and verbs (default: true) */
autoInitialize?: boolean;
}
/**
* DigitalObjectsFunctionRegistry - Persistent function registry using digital-objects
*
* This class implements the FunctionRegistry interface using digital-objects for storage.
* Function definitions are stored as Things, and function calls are tracked as Actions.
*
* @example
* ```ts
* import { createMemoryProvider } from 'digital-objects'
* import { createDigitalObjectsRegistry, defineFunction } from 'ai-functions'
*
* const provider = createMemoryProvider()
* const registry = await createDigitalObjectsRegistry({ provider })
*
* // Define a function
* const summarize = defineFunction({
* type: 'generative',
* name: 'summarize',
* args: { text: 'Text to summarize' },
* output: 'string',
* })
*
* // Store it in the registry
* registry.set('summarize', summarize)
*
* // Later, retrieve it
* const fn = registry.get('summarize')
* if (fn) {
* const result = await fn.call({ text: 'Long article...' })
* }
* ```
*/
export declare class DigitalObjectsFunctionRegistry implements FunctionRegistry {
private provider;
private initialized;
private autoInitialize;
private initPromise;
private functionCache;
constructor(options: DigitalObjectsRegistryOptions);
/**
* Initialize the registry by defining all necessary nouns and verbs
*/
initialize(): Promise<void>;
private _initialize;
/**
* Ensure the registry is initialized before operations
*/
private ensureInitialized;
/**
* Get a function by name
*/
get(name: string): DefinedFunction | undefined;
/**
* Get a function by name (async version for loading from storage)
*/
getAsync(name: string): Promise<DefinedFunction | undefined>;
/**
* Store a function definition
*/
set(name: string, fn: DefinedFunction): void;
/**
* Store a function definition (async version)
*/
setAsync(name: string, fn: DefinedFunction): Promise<Thing<StoredFunctionDefinition>>;
/**
* Check if a function exists
*/
has(name: string): boolean;
/**
* Check if a function exists (async version that also checks storage)
*/
hasAsync(name: string): Promise<boolean>;
/**
* List all function names
*/
list(): string[];
/**
* List all function names (async version that includes storage)
*/
listAsync(): Promise<string[]>;
/**
* Delete a function
*/
delete(name: string): boolean;
/**
* Delete a function (async version)
*/
deleteAsync(name: string): Promise<boolean>;
/**
* Clear all functions
*/
clear(): void;
/**
* Clear all functions (async version)
*/
clearAsync(): Promise<void>;
/**
* Record a function call as an Action
*/
trackCall(functionName: string, args: unknown): Promise<Action<FunctionCallData>>;
/**
* Record a successful function completion
*/
trackCompletion(callActionId: string, result: unknown, duration?: number): Promise<Action<FunctionCallData>>;
/**
* Record a function failure
*/
trackFailure(callActionId: string, error: string, duration?: number): Promise<Action<FunctionCallData>>;
/**
* Get call history for a function
*/
getCallHistory(functionName: string): Promise<Action<FunctionCallData>[]>;
/**
* Get all recent function calls
*/
getRecentCalls(limit?: number): Promise<Action<FunctionCallData>[]>;
/**
* Get the underlying provider for advanced operations
*/
getProvider(): DigitalObjectsProvider;
}
/**
* Create a DigitalObjectsFunctionRegistry
*
* @param options - Configuration options including the provider
* @returns An initialized DigitalObjectsFunctionRegistry
*
* @example
* ```ts
* import { createMemoryProvider } from 'digital-objects'
* import { createDigitalObjectsRegistry } from 'ai-functions'
*
* const provider = createMemoryProvider()
* const registry = await createDigitalObjectsRegistry({ provider })
*
* // Use the registry
* registry.set('myFunc', definedFunction)
* const fn = registry.get('myFunc')
* ```
*/
export declare function createDigitalObjectsRegistry(options: DigitalObjectsRegistryOptions): Promise<DigitalObjectsFunctionRegistry>;
//# sourceMappingURL=digital-objects-registry.d.ts.map