-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmod.ts
More file actions
124 lines (107 loc) · 3.34 KB
/
mod.ts
File metadata and controls
124 lines (107 loc) · 3.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
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
#!/usr/bin/env -S deno run --allow-all --unstable-broadcast-channel
/**
* PyoRunt - Python Runtime Agent
*
* Main executable entry point for the Pyodide runtime agent.
* This file serves as both the library entry point and the executable.
*/
import { PyodideRuntimeAgent } from "./pyodide-agent.ts";
export { PyodideRuntimeAgent } from "./pyodide-agent.ts";
import { logger, LogLevel } from "@runt/lib";
import { discoverUserIdentity } from "./auth.ts";
import { parseBaseRuntimeArgs } from "./config-cli.ts";
import { makeAdapter } from "npm:@livestore/adapter-node";
import { makeCfSync } from "npm:@livestore/sync-cf";
// Run the agent if this file is executed directly
if (import.meta.main) {
const name = "PyoRunt";
// Configure logger early based on environment variables for CLI usage
const runtLogLevel = Deno.env.get("RUNT_LOG_LEVEL");
const disableConsole = Deno.env.get("RUNT_DISABLE_CONSOLE_LOGS") === "true";
if (runtLogLevel) {
const normalizedLevel = runtLogLevel.toUpperCase();
let logLevel: LogLevel;
switch (normalizedLevel) {
case "DEBUG":
logLevel = LogLevel.DEBUG;
break;
case "INFO":
logLevel = LogLevel.INFO;
break;
case "WARN":
case "WARNING":
logLevel = LogLevel.WARN;
break;
case "ERROR":
logLevel = LogLevel.ERROR;
break;
default:
logLevel = LogLevel.INFO;
}
logger.configure({
level: logLevel,
console: !disableConsole,
});
} else {
// Configure with INFO as default when no RUNT_LOG_LEVEL is set
logger.configure({
level: LogLevel.INFO,
console: !disableConsole,
});
}
logger.info("Authenticating...");
// Parse CLI args once to get auth details
const cliConfig = parseBaseRuntimeArgs(Deno.args);
const syncUrl = cliConfig.syncUrl ||
"wss://app.runt.run";
const authToken = cliConfig.authToken;
if (!authToken) {
console.error("❌ Configuration Error: Missing auth token");
console.error("Use --auth-token or set RUNT_API_KEY environment variable");
Deno.exit(1);
}
// Discover user identity and generate proper clientId
const { userId, clientId, userInfo } = await discoverUserIdentity({
authToken,
syncUrl,
});
logger.info("Authenticated successfully", {
userId,
clientId,
email: userInfo.email,
});
// Create adapter for Node.js environment with Cloudflare sync
const adapter = makeAdapter({
storage: { type: "in-memory" },
sync: {
backend: makeCfSync({ url: syncUrl }),
onSyncError: "ignore",
},
});
// Create agent with discovered clientId, userId, and Node.js adapter
const agent = new PyodideRuntimeAgent(
Deno.args,
{}, // pyodide options
{ clientId, adapter, userId }, // runtime options
);
logger.info("Starting Agent");
try {
await agent.start();
logger.info(`${name} started`, {
runtimeId: agent.config.runtimeId,
runtimeType: agent.config.runtimeType,
notebookId: agent.config.notebookId,
sessionId: agent.config.sessionId,
syncUrl: agent.config.syncUrl,
});
await agent.keepAlive();
} catch (error) {
logger.error(`${name} failed to start`, error);
Deno.exit(1);
}
agent.shutdown().catch((error) => {
logger.error(`${name} failed to shutdown`, error);
Deno.exit(1);
});
Deno.exit(0);
}