-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathllm-adapter.ts
More file actions
78 lines (68 loc) · 2.34 KB
/
Copy pathllm-adapter.ts
File metadata and controls
78 lines (68 loc) · 2.34 KB
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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* LLMAdapter — LLM Provider Adapter Contract
*
* Adapters translate between the ObjectStack AI protocol and concrete
* LLM provider SDKs (OpenAI, Anthropic, Ollama, etc.).
*
* Each adapter is a thin wrapper — all orchestration, conversation
* management, and tool execution logic lives in the AI service layer.
*
* Follows Dependency Inversion Principle — third-party adapter packages
* depend only on `@objectstack/spec/contracts` for type alignment.
*
* Aligned with `IAIService` in `ai-service.ts`.
*/
import type {
ModelMessage,
TextStreamPart,
ToolSet,
} from 'ai';
import type {
AIRequestOptions,
AIResult,
AIObjectResult,
GenerateObjectOptions,
} from './ai-service.js';
import type { z } from 'zod';
export interface LLMAdapter {
/** Unique adapter identifier (e.g. 'openai', 'anthropic', 'memory') */
readonly name: string;
/**
* Generate a chat completion.
* @param messages - Conversation messages (Vercel `ModelMessage`)
* @param options - Request configuration (includes tool definitions)
*/
chat(messages: ModelMessage[], options?: AIRequestOptions): Promise<AIResult>;
/**
* Generate a text completion from a single prompt.
* @param prompt - Input prompt string
* @param options - Request configuration
*/
complete(prompt: string, options?: AIRequestOptions): Promise<AIResult>;
/**
* Stream a chat completion as an async iterable of Vercel AI SDK stream parts.
* Implementations that do not support streaming may omit this method.
*/
streamChat?(messages: ModelMessage[], options?: AIRequestOptions): AsyncIterable<TextStreamPart<ToolSet>>;
/**
* Generate embedding vectors.
*/
embed?(input: string | string[], model?: string): Promise<number[][]>;
/**
* Generate a strongly-typed object that conforms to a Zod schema.
*
* Adapters should delegate to the provider's native structured-output
* facility when available. Adapters without structured-output support
* may omit this method — the AI service will throw a clear error.
*/
generateObject?<T>(
messages: ModelMessage[],
schema: z.ZodType<T>,
options?: GenerateObjectOptions,
): Promise<AIObjectResult<T>>;
/**
* List models available through this adapter.
*/
listModels?(): Promise<string[]>;
}