-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.ts
More file actions
130 lines (119 loc) · 3.95 KB
/
config.ts
File metadata and controls
130 lines (119 loc) · 3.95 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
// Configuration utilities for Anode runtime agents
//
// This module provides minimal, generic configuration interfaces that can be
// extended by specific runtime implementations (Python, JavaScript, etc.).
import { logger } from "./logging.ts";
import type { Adapter } from "npm:@livestore/livestore";
import type {
IArtifactClient,
RuntimeAgentOptions,
RuntimeCapabilities,
} from "./types.ts";
import { ArtifactClient } from "./artifact-client.ts";
/**
* Default configuration values
*/
export const DEFAULT_CONFIG = {
syncUrl: "wss://app.runt.run",
imageArtifactThresholdBytes: 6 * 1024, // 6KB threshold for uploading images as artifacts
} as const;
/**
* Core configuration class for runtime agents
* Can be extended by specific runtime implementations
*/
export class RuntimeConfig {
public readonly runtimeId: string;
public readonly runtimeType: string;
public readonly syncUrl: string;
public readonly authToken: string;
public readonly notebookId: string;
public readonly capabilities: RuntimeCapabilities;
public readonly sessionId: string;
public readonly imageArtifactThresholdBytes: number;
public readonly artifactClient: IArtifactClient;
public readonly adapter: Adapter | undefined;
public readonly clientId: string;
public readonly userId: string;
constructor(options: RuntimeAgentOptions) {
this.runtimeId = options.runtimeId;
this.runtimeType = options.runtimeType;
this.syncUrl = options.syncUrl;
this.authToken = options.authToken;
this.notebookId = options.notebookId;
this.capabilities = options.capabilities;
this.imageArtifactThresholdBytes = options.imageArtifactThresholdBytes ??
DEFAULT_CONFIG.imageArtifactThresholdBytes;
this.adapter = options.adapter;
this.clientId = options.clientId;
this.userId = options.userId;
// Use injected artifact client or create default one
this.artifactClient = options.artifactClient ??
new ArtifactClient(this.getArtifactServiceUrl(options.syncUrl));
// Generate unique session ID
this.sessionId = `${this.runtimeType}-${this.runtimeId}-${Date.now()}-${
Math.random().toString(36).substring(2, 15)
}`;
}
/**
* Convert sync URL to artifact service URL
* Transforms WebSocket URLs to HTTP(S) URLs for the artifact service
*/
private getArtifactServiceUrl(syncUrl: string): string {
try {
const url = new URL(syncUrl);
// Convert wss:// to https:// and ws:// to http://
const protocol = url.protocol === "wss:" ? "https:" : "http:";
return `${protocol}//${url.host}`;
} catch (error) {
// Fallback to default if URL parsing fails
logger.warn(
"Failed to parse sync URL for artifact service, using default",
{
syncUrl,
error: error instanceof Error ? error.message : String(error),
},
);
return "https://api.runt.run";
}
}
/**
* Validate that all required configuration is present
*/
validate(): void {
const missing: { field: string; suggestion: string }[] = [];
if (!this.authToken) {
missing.push({
field: "authToken",
suggestion: "AUTH_TOKEN or RUNT_API_KEY environment variable",
});
}
if (!this.notebookId) {
missing.push({
field: "notebookId",
suggestion: "NOTEBOOK_ID environment variable",
});
}
if (!this.runtimeId) {
missing.push({
field: "runtimeId",
suggestion: "RUNTIME_ID environment variable",
});
}
if (!this.runtimeType) {
missing.push({
field: "runtimeType",
suggestion: "RUNTIME_TYPE environment variable",
});
}
if (missing.length > 0) {
const messages = missing.map(
({ field, suggestion }) => ` ${field}: ${suggestion}`,
);
throw new Error(
`Missing required configuration:\n\n${
messages.join("\n")
}\n\nConsult your runtime implementation for configuration details.`,
);
}
}
}