ai-sdk-memory
Version:
Semantic and intent-based memory for AI SDK. Supports one-shot semantic caching and multi-turn intent-aware conversations. Reduces token costs by reusing similar responses.
119 lines (112 loc) • 6.14 kB
text/typescript
import { streamText, StreamTextResult, generateText, GenerateTextResult, generateObject, GenerateObjectResult, streamObject, StreamObjectResult } from 'ai';
import z$1, { z } from 'zod';
declare const semanticCacheConfigSchema: z.ZodObject<{
model: z.ZodUnion<readonly [z.ZodString, z.ZodCustom<unknown, unknown>]>;
vector: z.ZodDefault<z.ZodOptional<z.ZodObject<{
url: z.ZodURL;
token: z.ZodString;
}, z.core.$strip>>>;
redis: z.ZodDefault<z.ZodOptional<z.ZodObject<{
url: z.ZodURL;
token: z.ZodString;
}, z.core.$strip>>>;
threshold: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
ttl: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
debug: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
cacheMode: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
default: "default";
refresh: "refresh";
}>>>;
simulateStream: z.ZodDefault<z.ZodOptional<z.ZodObject<{
enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
initialDelayInMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
chunkDelayInMs: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
}, z.core.$strip>>>;
useFullMessages: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
onStepFinish: z.ZodOptional<z.ZodFunction<z.ZodTuple<readonly [z.ZodObject<{
step: z.ZodEnum<{
"cache-check-start": "cache-check-start";
"cache-score-evaluated": "cache-score-evaluated";
"cache-hit": "cache-hit";
"cache-miss": "cache-miss";
"generation-start": "generation-start";
"generation-complete": "generation-complete";
"cache-store-start": "cache-store-start";
"cache-store-complete": "cache-store-complete";
"cache-store-error": "cache-store-error";
}>;
prompt: z.ZodOptional<z.ZodString>;
cacheScore: z.ZodOptional<z.ZodNumber>;
error: z.ZodOptional<z.ZodAny>;
}, z.core.$strip>], null>, z.ZodVoid>>;
}, z.core.$strip>;
type SemanticCacheConfig = z.input<typeof semanticCacheConfigSchema>;
declare function createSemanticMemory(config: SemanticCacheConfig): {
streamText: <TOOLS extends Record<string, any> = {}>(options: Parameters<typeof streamText<TOOLS>>[0]) => Promise<StreamTextResult<TOOLS, any>>;
generateText: <TOOLS extends Record<string, any> = {}, OUTPUT = undefined>(options: Parameters<typeof generateText<TOOLS, OUTPUT>>[0]) => Promise<GenerateTextResult<TOOLS, OUTPUT>>;
generateObject: <T = any>(options: Parameters<typeof generateObject>[0]) => Promise<GenerateObjectResult<T>>;
streamObject: <T = any>(options: Parameters<typeof streamObject>[0]) => Promise<StreamObjectResult<T, T, any>>;
};
declare const intentCacheConfigSchema: z$1.ZodObject<{
model: z$1.ZodUnion<readonly [z$1.ZodString, z$1.ZodCustom<unknown, unknown>]>;
vector: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodObject<{
url: z$1.ZodURL;
token: z$1.ZodString;
}, z$1.core.$strip>>>;
redis: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodObject<{
url: z$1.ZodURL;
token: z$1.ZodString;
}, z$1.core.$strip>>>;
threshold: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
ttl: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
debug: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodBoolean>>;
cacheMode: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodEnum<{
default: "default";
refresh: "refresh";
}>>>;
simulateStream: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodObject<{
enabled: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodBoolean>>;
initialDelayInMs: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
chunkDelayInMs: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
}, z$1.core.$strip>>>;
useFullMessages: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodBoolean>>;
intentExtractor: z$1.ZodObject<{
model: z$1.ZodUnion<readonly [z$1.ZodString, z$1.ZodCustom<unknown, unknown>]>;
windowSize: z$1.ZodDefault<z$1.ZodNumber>;
prompt: z$1.ZodOptional<z$1.ZodString>;
}, z$1.core.$strip>;
onStepFinish: z$1.ZodOptional<z$1.ZodFunction<z$1.ZodTuple<readonly [z$1.ZodObject<{
step: z$1.ZodEnum<{
"cache-check-start": "cache-check-start";
"cache-score-evaluated": "cache-score-evaluated";
"cache-hit": "cache-hit";
"cache-miss": "cache-miss";
"generation-start": "generation-start";
"generation-complete": "generation-complete";
"cache-store-start": "cache-store-start";
"cache-store-complete": "cache-store-complete";
"cache-store-error": "cache-store-error";
"intent-extraction-start": "intent-extraction-start";
"intent-extraction-complete": "intent-extraction-complete";
"intent-extraction-error": "intent-extraction-error";
}>;
userIntention: z$1.ZodOptional<z$1.ZodString>;
extractedIntent: z$1.ZodOptional<z$1.ZodObject<{
intent: z$1.ZodString;
domain: z$1.ZodArray<z$1.ZodString>;
stack: z$1.ZodArray<z$1.ZodString>;
goal: z$1.ZodString;
constraints: z$1.ZodArray<z$1.ZodString>;
}, z$1.core.$strip>>;
cacheScore: z$1.ZodOptional<z$1.ZodNumber>;
error: z$1.ZodOptional<z$1.ZodAny>;
}, z$1.core.$strip>], null>, z$1.ZodVoid>>;
}, z$1.core.$strip>;
type IntentCacheConfig = z$1.input<typeof intentCacheConfigSchema>;
declare function createIntentMemory(config: IntentCacheConfig): {
streamText: <TOOLS extends Record<string, any> = {}>(options: Parameters<typeof streamText<TOOLS>>[0]) => Promise<StreamTextResult<TOOLS, any>>;
generateText: <TOOLS extends Record<string, any> = {}, OUTPUT = undefined>(options: Parameters<typeof generateText<TOOLS, OUTPUT>>[0]) => Promise<GenerateTextResult<TOOLS, OUTPUT>>;
generateObject: <T = any>(options: Parameters<typeof generateObject>[0]) => Promise<GenerateObjectResult<T>>;
streamObject: <T = any>(options: Parameters<typeof streamObject>[0]) => Promise<StreamObjectResult<T, T, any>>;
};
export { createIntentMemory, createSemanticMemory };