-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtypes.ts
More file actions
278 lines (257 loc) · 8.1 KB
/
types.ts
File metadata and controls
278 lines (257 loc) · 8.1 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
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
// Type definitions for Anode runtime agents
//
// This module provides TypeScript interfaces and types used throughout
// the runtime agent library, importing existing types from @runt/schema
// and adding runtime-specific extensions.
import type { Adapter, Store } from "npm:@livestore/livestore";
import type { CellData, ExecutionQueueData, OutputType } from "@runt/schema";
import { events, materializers, tables } from "@runt/schema";
import { makeSchema, State } from "npm:@livestore/livestore";
// Create schema locally
const state = State.SQLite.makeState({ tables, materializers });
const schema = makeSchema({ events, state });
/**
* Raw output data format accepted by context.display() methods
* This is converted internally to MediaContainer format
*/
export interface RawOutputData {
[mimeType: string]: unknown;
}
/**
* Interface for artifact client dependency injection
*/
export interface IArtifactClient {
/** Submit content data to artifact service */
submitContent(
data: Uint8Array,
options: ArtifactSubmissionOptions,
): Promise<ArtifactSubmissionResult>;
/** Get artifact URL by ID */
getArtifactUrl(artifactId: string): string;
}
/** Artifact submission options */
export interface ArtifactSubmissionOptions {
notebookId: string;
authToken: string;
mimeType?: string;
filename?: string;
}
/** Artifact submission result */
export interface ArtifactSubmissionResult {
artifactId: string;
}
/**
* Core configuration options for runtime agents
* Runtime implementations can extend this interface with their own specific options
*/
export interface RuntimeAgentOptions {
/** Unique identifier for this runtime */
readonly runtimeId: string;
/** Human-readable runtime type (appears in UI) */
readonly runtimeType: string;
/** Capabilities this runtime supports */
readonly capabilities: Readonly<RuntimeCapabilities>;
/** LiveStore sync URL */
readonly syncUrl: string;
/** Authentication token */
readonly authToken: string;
/** Notebook ID to connect to */
readonly notebookId: string;
/** Threshold in bytes for uploading images as artifacts (default: 6KB) */
readonly imageArtifactThresholdBytes?: number;
/** Artifact client for dependency injection (optional) */
readonly artifactClient?: IArtifactClient;
/** LiveStore adapter (required) */
readonly adapter: Adapter;
/** Client ID for sync payload (must be provided) */
readonly clientId: string;
/** User ID for sync payload authorization (must be provided) */
readonly userId: string;
}
/**
* Capabilities that a runtime can advertise to the notebook UI
* (extracted from existing schema capabilities structure)
*/
export interface RuntimeCapabilities {
/** Can execute code cells */
canExecuteCode: boolean;
/** Can execute SQL cells */
canExecuteSql: boolean;
/** Can execute AI cells */
canExecuteAi: boolean;
/** Available AI models with their capabilities */
availableAiModels?: AiModel[];
}
/**
* Represents an AI model with its capabilities
*/
export interface AiModel {
/** Model identifier (e.g., "gpt-4o-mini", "llama3.1") */
name: string;
/** Human-readable display name */
displayName: string;
/** AI provider (e.g., "openai", "ollama", "anthropic") */
provider: string;
/** Model capabilities */
capabilities: ModelCapability[];
/** Model metadata (size, type, etc.) */
metadata?: {
[key: string]: unknown;
};
}
/**
* Capabilities that an AI model can have
*/
export type ModelCapability =
| "completion" // Basic text completion
| "tools" // Function/tool calling
| "vision" // Image understanding
| "thinking"; // Chain of thought reasoning
/**
* Execution context passed to handlers
*/
export interface ExecutionContext {
/** The cell being executed */
cell: CellData;
/** The execution queue entry */
queueEntry: ExecutionQueueData;
/** LiveStore instance */
store: Store<typeof schema>;
/** This runtime's session ID */
sessionId: string;
/** Runtime ID */
runtimeId: string;
/** AbortSignal for cancellation support */
abortSignal: AbortSignal;
/** Helper to check if execution should be cancelled */
checkCancellation: () => void;
// Output emission methods for real-time streaming
/** Emit text to stdout stream */
stdout: (text: string) => void;
/** Emit text to stderr stream */
stderr: (text: string) => void;
/** Emit rich display data (plots, HTML, etc.) */
display: (
data: RawOutputData,
metadata?: Record<string, unknown>,
displayId?: string,
) => Promise<void>;
/** Update existing display data by display ID */
updateDisplay: (
displayId: string,
data: RawOutputData,
metadata?: Record<string, unknown>,
) => Promise<void>;
/** Emit execution result (final output) */
result: (
data: RawOutputData,
metadata?: Record<string, unknown>,
) => Promise<void>;
/** Emit error output */
error: (ename: string, evalue: string, traceback: string[]) => void;
/** Clear all previous outputs for this cell */
clear: (wait?: boolean) => void;
/** Append text to existing terminal output (for streaming) */
appendTerminal: (
outputId: string,
delta: string,
sequenceNumber: number,
) => void;
/** Emit markdown content (for AI responses) */
markdown: (content: string, metadata?: Record<string, unknown>) => string;
/** Append to existing markdown output (for streaming AI responses) */
appendMarkdown: (
outputId: string,
delta: string,
sequenceNumber: number,
) => void;
}
/**
* Result of cell execution
*
* Note: With streaming output support, most outputs should be emitted
* via ExecutionContext methods (emitStream, emitDisplay, etc.) during
* execution. This result primarily indicates final success/failure state.
*/
export interface ExecutionResult {
/** Whether execution succeeded */
success: boolean;
/** Optional final output data (for simple cases) */
data?: RawOutputData;
/** Optional metadata for final output */
metadata?: Record<string, unknown>;
/** Error message if execution failed */
error?: string;
/** Output type for final data (default: "execute_result") */
outputType?: OutputType;
}
/**
* Function signature for execution handlers
*
* Handlers should use the ExecutionContext methods to emit outputs in real-time:
* - context.stdout() / context.stderr() for streaming text output
* - context.display() for rich displays (plots, HTML, etc.)
* - context.result() for final execution results
* - context.error() for errors
* - context.clear() to clear previous outputs
*
* The returned ExecutionResult indicates overall success/failure and can
* optionally include final output data for simple cases.
*
* @param context - Execution context with cell, store, and output methods
* @returns Promise resolving to execution result (success/failure state)
*/
export type ExecutionHandler = (
context: ExecutionContext,
) => Promise<ExecutionResult>;
/**
* Event handlers for runtime agent lifecycle
*/
export interface RuntimeAgentEventHandlers {
/** Called when agent starts up */
onStartup?: () => void | Promise<void>;
/** Called when agent shuts down */
onShutdown?: () => void | Promise<void>;
/** Called when connection to LiveStore is established */
onConnected?: () => void | Promise<void>;
/** Called when connection to LiveStore is lost */
onDisconnected?: (error?: Error) => void | Promise<void>;
/** Called when an execution fails */
onExecutionError?: (
error: Error,
context: ExecutionContext,
) => void | Promise<void>;
}
/**
* Cancellation error for when execution is interrupted
*/
export interface CancellationError extends Error {
name: "CancellationError";
queueId: string;
cellId: string;
}
/**
* Handler for execution cancellation events
*/
export type CancellationHandler = (
queueId: string,
cellId: string,
reason: string,
) => void | Promise<void>;
/**
* Runtime agent status
*/
export type AgentStatus =
| "starting"
| "ready"
| "busy"
| "error"
| "shutting-down";
// Re-export commonly used schema types for convenience
export type {
CellData,
ErrorOutputData,
ExecutionQueueData,
OutputType,
RuntimeSessionData,
} from "@runt/schema";