@agentick/ai-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,302 @@
1
+ /**
2
+ * ============================================================================
3
+ * AI SDK Compiler Adapter
4
+ * ============================================================================
5
+ *
6
+ * This adapter provides progressive adoption for AI SDK users who want to use
7
+ * our JSX compilation without fully committing to our Engine.
8
+ *
9
+ * Direction of adaptation: Engine → ai-sdk
10
+ * (For ai-sdk → Engine direction, use adapter.ts directly)
11
+ *
12
+ * ============================================================================
13
+ * PROGRESSIVE ADOPTION LEVELS
14
+ * ============================================================================
15
+ *
16
+ * Level 1: compile() only
17
+ * Returns library-native input. User calls generateText themselves.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { compile } from '@tentickle/ai-sdk';
22
+ * import { generateText } from 'ai';
23
+ * import { openai } from '@ai-sdk/openai';
24
+ *
25
+ * const compiled = await compile(<MyAgent />);
26
+ *
27
+ * const result = await generateText({
28
+ * model: compiled.model ?? openai('gpt-4o'),
29
+ * messages: compiled.messages,
30
+ * tools: compiled.tools,
31
+ * system: compiled.system,
32
+ * });
33
+ * ```
34
+ *
35
+ * Level 2: run() with executor
36
+ * User controls model execution, we handle the tick loop.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { createCompiler } from '@tentickle/ai-sdk';
41
+ * import { generateText } from 'ai';
42
+ * import { openai } from '@ai-sdk/openai';
43
+ *
44
+ * const compiler = createCompiler();
45
+ *
46
+ * const result = await compiler.run(<MyAgent />, async (input) => {
47
+ * return await generateText({
48
+ * model: openai('gpt-4o'),
49
+ * ...input,
50
+ * });
51
+ * });
52
+ * ```
53
+ *
54
+ * Level 3: run() / stream() - managed execution
55
+ * We handle everything. Model comes from <Model> component or config.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * import { createCompiler } from '@tentickle/ai-sdk';
60
+ * import { openai } from '@ai-sdk/openai';
61
+ *
62
+ * const compiler = createCompiler({ model: openai('gpt-4o') });
63
+ *
64
+ * const result = await compiler.run(<MyAgent />);
65
+ *
66
+ * // Or streaming:
67
+ * for await (const chunk of compiler.stream(<MyAgent />)) {
68
+ * process.stdout.write(chunk.textDelta ?? '');
69
+ * }
70
+ * ```
71
+ *
72
+ * Level 4: generateText() / streamText() - mirror library API
73
+ * Same API as ai-sdk, but with JSX as the first argument.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * import { generateText, streamText } from '@tentickle/ai-sdk';
78
+ * import { openai } from '@ai-sdk/openai';
79
+ *
80
+ * // createCompiler run internally
81
+ *
82
+ * // Exact same return type as ai-sdk's generateText
83
+ * const result = await generateText(<MyAgent />, {
84
+ * temperature: 0.8,
85
+ * });
86
+ *
87
+ * // Exact same return type as ai-sdk's streamText
88
+ * const { fullStream, text } = streamText(<MyAgent />);
89
+ * for await (const chunk of fullStream) {
90
+ * // Native ai-sdk chunks
91
+ * }
92
+ * ```
93
+ *
94
+ * ============================================================================
95
+ * COMPONENT PORTABILITY
96
+ * ============================================================================
97
+ *
98
+ * All adapter packages export the same component names:
99
+ * - Model: Configure the model declaratively
100
+ * - Tool: Define tools in JSX
101
+ * - Message, System, User, Assistant: Message components
102
+ *
103
+ * Switch adapters without changing agent code.
104
+ *
105
+ * ============================================================================
106
+ */
107
+ import { type RuntimeConfig, type JSX } from "@tentickle/core";
108
+ import type { LanguageModel, ModelMessage as AiSdkMessage, ToolSet, TextStreamPart } from "ai";
109
+ import { generateText as aiSdkGenerateText, streamText as aiSdkStreamText } from "ai";
110
+ /**
111
+ * Library-native compiled output.
112
+ * This is what compile() returns - ready to pass to generateText/streamText.
113
+ */
114
+ export interface CompiledInput {
115
+ /** Messages in ai-sdk CoreMessage format */
116
+ messages: AiSdkMessage[];
117
+ /** Tools in ai-sdk ToolSet format (definitions only, no execute) */
118
+ tools?: ToolSet;
119
+ /** System prompt (extracted from system messages) */
120
+ system?: string;
121
+ /** Model extracted from <Model> component (if present) */
122
+ model?: LanguageModel;
123
+ /** Current tick number (for multi-tick execution) */
124
+ tick: number;
125
+ }
126
+ /**
127
+ * Executor function signature.
128
+ * User provides this to control model execution.
129
+ */
130
+ export type Executor = (input: CompiledInput) => Promise<Awaited<ReturnType<typeof aiSdkGenerateText>>>;
131
+ /**
132
+ * Stream executor function signature.
133
+ */
134
+ export type StreamExecutor = (input: CompiledInput) => ReturnType<typeof aiSdkStreamText>;
135
+ /**
136
+ * Configuration for the compiler.
137
+ */
138
+ export interface CompilerConfig {
139
+ /** Default model (used when no <Model> component and no executor provided) */
140
+ model?: LanguageModel;
141
+ /** Default temperature */
142
+ temperature?: number;
143
+ /** Default max tokens */
144
+ maxTokens?: number;
145
+ /** Maximum ticks per execution (default: 10) */
146
+ maxTicks?: number;
147
+ /** Additional service config */
148
+ serviceConfig?: Partial<Omit<RuntimeConfig, "modelGetter" | "processMethods">>;
149
+ }
150
+ /**
151
+ * Options for generateText/streamText methods.
152
+ * Matches ai-sdk's options interface.
153
+ */
154
+ export type GenerateOptions = Partial<Omit<Parameters<typeof aiSdkGenerateText>[0], "messages" | "prompt">>;
155
+ /**
156
+ * Events emitted during streaming.
157
+ */
158
+ export type CompilerStreamEvent = {
159
+ type: "tick_start";
160
+ tick: number;
161
+ } | {
162
+ type: "compiled";
163
+ tick: number;
164
+ input: CompiledInput;
165
+ } | {
166
+ type: "chunk";
167
+ tick: number;
168
+ chunk: TextStreamPart<ToolSet>;
169
+ } | {
170
+ type: "tick_end";
171
+ tick: number;
172
+ } | {
173
+ type: "complete";
174
+ result: any;
175
+ };
176
+ /**
177
+ * Compile JSX to library-native input.
178
+ *
179
+ * This is the simplest entry point. You get back messages, tools, and system
180
+ * in ai-sdk format, ready to pass to generateText/streamText.
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * import { compile } from '@tentickle/ai-sdk';
185
+ * import { generateText } from 'ai';
186
+ * import { openai } from '@ai-sdk/openai';
187
+ *
188
+ * const { messages, tools, system, model } = await compile(<MyAgent />);
189
+ *
190
+ * const result = await generateText({
191
+ * model: model ?? openai('gpt-4o'),
192
+ * messages,
193
+ * tools,
194
+ * system,
195
+ * });
196
+ * ```
197
+ */
198
+ export declare function compile(jsx: JSX.Element, initialMessages?: AiSdkMessage[]): Promise<CompiledInput>;
199
+ /**
200
+ * AI SDK Compiler.
201
+ *
202
+ * Provides progressive adoption from simple compilation to full execution management.
203
+ *
204
+ * @example Level 2: User-controlled execution
205
+ * ```typescript
206
+ * const compiler = createCompiler();
207
+ *
208
+ * const result = await compiler.run(<MyAgent />, async (input) => {
209
+ * return await generateText({ model: openai('gpt-4o'), ...input });
210
+ * });
211
+ * ```
212
+ *
213
+ * @example Level 3: Managed execution
214
+ * ```typescript
215
+ * const compiler = createCompiler({ model: openai('gpt-4o') });
216
+ * const result = await compiler.run(<MyAgent />);
217
+ * ```
218
+ *
219
+ * @example Level 4: Library-mirroring API
220
+ * ```typescript
221
+ * const compiler = createCompiler({ model: openai('gpt-4o') });
222
+ * const result = await compiler.generateText(<MyAgent />, { temperature: 0.8 });
223
+ * ```
224
+ */
225
+ export declare class AiSdkCompiler {
226
+ private service;
227
+ private defaultModel?;
228
+ private defaultOptions;
229
+ private maxTicks;
230
+ private executions;
231
+ private currentExecutor?;
232
+ constructor(config?: CompilerConfig);
233
+ private createProcessMethods;
234
+ private engineInputToMessages;
235
+ /**
236
+ * Execute a JSX program.
237
+ *
238
+ * If executor is provided (Level 2), user controls model execution.
239
+ * If not provided (Level 3), we manage execution using configured model.
240
+ *
241
+ * @param jsx Root JSX element
242
+ * @param executorOrMessages Optional executor function OR initial messages
243
+ * @param maybeExecutor Optional executor (if second arg was messages)
244
+ */
245
+ run(jsx: JSX.Element, executorOrMessages?: Executor | AiSdkMessage[], maybeExecutor?: Executor): Promise<Awaited<ReturnType<typeof aiSdkGenerateText>>>;
246
+ private runInternal;
247
+ private createManagedExecutor;
248
+ /**
249
+ * Execute a JSX program with streaming.
250
+ *
251
+ * @param jsx Root JSX element
252
+ * @param executorOrMessages Optional executor function OR initial messages
253
+ * @param maybeExecutor Optional executor (if second arg was messages)
254
+ */
255
+ stream(jsx: JSX.Element, executorOrMessages?: StreamExecutor | AiSdkMessage[], maybeExecutor?: StreamExecutor): AsyncGenerator<CompilerStreamEvent>;
256
+ private streamInternal;
257
+ private createManagedStreamExecutor;
258
+ destroy(): Promise<void>;
259
+ }
260
+ /**
261
+ * Generate text using JSX.
262
+ *
263
+ * Mirrors ai-sdk's generateText API exactly.
264
+ * Returns the same type for seamless integration.
265
+ *
266
+ * @param jsx Root JSX element
267
+ * @param options Additional options (merged with defaults and JSX config)
268
+ */
269
+ export declare function generateText(jsx: JSX.Element, options?: GenerateOptions): Promise<Awaited<ReturnType<typeof aiSdkGenerateText>>>;
270
+ /**
271
+ * Stream text using JSX.
272
+ *
273
+ * Mirrors ai-sdk's streamText API exactly.
274
+ * Returns the same type for seamless integration.
275
+ *
276
+ * @param jsx Root JSX element
277
+ * @param options Additional options (merged with defaults and JSX config)
278
+ */
279
+ export declare function streamText(jsx: JSX.Element, options?: GenerateOptions): ReturnType<typeof aiSdkStreamText>;
280
+ /**
281
+ * Create an AI SDK compiler.
282
+ *
283
+ * @example Without model (requires executor or <Model> component)
284
+ * ```typescript
285
+ * const compiler = createCompiler();
286
+ *
287
+ * // Use with executor
288
+ * const result = await compiler.run(<MyAgent />, async (input) => {
289
+ * return await generateText({ model: openai('gpt-4o'), ...input });
290
+ * });
291
+ * ```
292
+ *
293
+ * @example With model (managed execution)
294
+ * ```typescript
295
+ * const compiler = createCompiler({ model: openai('gpt-4o') });
296
+ *
297
+ * const result = await compiler.run(<MyAgent />);
298
+ * ```
299
+ */
300
+ export declare function createCompiler(config?: CompilerConfig): AiSdkCompiler;
301
+ export { createCompiler as createAiSdkCompiler };
302
+ //# sourceMappingURL=compiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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