langshell 0.2.0

Rust SDK for building stateful, capability-scoped LangShell runtimes.
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::{fmt, future::Future, path::PathBuf, sync::Arc};

pub use langshell_core::*;
pub use langshell_tools::{FileMount, ToolConfig};

use serde_json::{Map, Value};

type RuntimeFactory =
    dyn Fn(ToolRegistry, SessionLimits) -> Arc<dyn LanguageRuntime> + Send + Sync + 'static;

#[derive(Debug, Clone)]
pub struct LangShell {
    runtimes: Vec<Arc<dyn LanguageRuntime>>,
}

impl LangShell {
    pub fn builder() -> LangShellBuilder {
        LangShellBuilder::default()
    }

    pub async fn run(&self, request: RunRequest) -> RunResult {
        let language = request.language;
        match self.runtime_for(language) {
            Ok(runtime) => runtime.run(request).await,
            Err(error) => RunResult::error(
                RunStatus::ValidationError,
                error,
                String::new(),
                Metrics::default(),
            ),
        }
    }

    pub async fn validate(&self, mut request: RunRequest) -> RunResult {
        request.validate_only = true;
        self.run(request).await
    }

    pub fn session(&self, session_id: impl Into<String>) -> SessionHandle {
        self.session_with_language(session_id, Language::Python)
    }

    pub fn typescript_session(&self, session_id: impl Into<String>) -> SessionHandle {
        self.session_with_language(session_id, Language::TypeScript)
    }

    pub fn session_with_language(
        &self,
        session_id: impl Into<String>,
        language: Language,
    ) -> SessionHandle {
        SessionHandle {
            shell: self.clone(),
            session_id: session_id.into(),
            language,
        }
    }

    pub async fn create_session(&self, session_id: impl Into<String>) -> Result<(), ErrorObject> {
        self.create_session_with_language(session_id, Language::Python)
            .await
    }

    pub async fn create_session_with_language(
        &self,
        session_id: impl Into<String>,
        language: Language,
    ) -> Result<(), ErrorObject> {
        let session_id = SessionId::new(session_id)?;
        self.runtime_for(language)?
            .create_session(session_id, None)
            .await
    }

    pub async fn list_sessions(&self) -> Vec<SessionId> {
        let mut ids = Vec::new();
        for runtime in &self.runtimes {
            if let Ok(runtime_ids) = runtime.list_sessions().await {
                ids.extend(runtime_ids);
            }
        }
        ids.sort_by(|a, b| a.0.cmp(&b.0));
        ids.dedup_by(|a, b| a.0 == b.0);
        ids
    }

    pub async fn destroy_session(
        &self,
        session_id: impl Into<String>,
    ) -> Result<bool, ErrorObject> {
        let session_id = SessionId::new(session_id)?;
        let mut removed = false;
        for runtime in &self.runtimes {
            removed |= runtime.destroy_session(session_id.clone()).await?;
        }
        Ok(removed)
    }

    pub async fn snapshot_session(
        &self,
        session_id: impl Into<String>,
    ) -> Result<Vec<u8>, ErrorObject> {
        let session_id = SessionId::new(session_id)?;
        let mut not_found = None;
        for runtime in &self.runtimes {
            match runtime.snapshot_session(session_id.clone()).await {
                Ok(snapshot) => return Ok(snapshot),
                Err(error) if error.code == "SESSION_NOT_FOUND" => not_found = Some(error),
                Err(error) => return Err(error),
            }
        }
        Err(not_found.unwrap_or_else(|| no_runtime_registered_error(None)))
    }

    pub async fn snapshot_session_with_language(
        &self,
        session_id: impl Into<String>,
        language: Language,
    ) -> Result<Vec<u8>, ErrorObject> {
        let session_id = SessionId::new(session_id)?;
        self.runtime_for(language)?
            .snapshot_session(session_id)
            .await
    }

    pub async fn restore_session(
        &self,
        snapshot: &[u8],
        session_id: Option<impl Into<String>>,
    ) -> Result<SessionId, ErrorObject> {
        let session_id = session_id.map(|id| SessionId::new(id.into())).transpose()?;
        if let Some(runtime) = self
            .runtimes
            .iter()
            .find(|runtime| runtime.can_restore_snapshot(snapshot))
        {
            return runtime.restore_session(snapshot.to_vec(), session_id).await;
        }

        if self.runtimes.len() == 1 {
            return self.runtimes[0]
                .restore_session(snapshot.to_vec(), session_id)
                .await;
        }

        Err(ErrorObject::new(
            "SNAPSHOT_CORRUPT",
            "No registered language runtime can restore this snapshot.",
        )
        .with_hint(
            "Register the backend that created the snapshot before calling restore_session.",
        ))
    }

    fn runtime_for(&self, language: Language) -> Result<&dyn LanguageRuntime, ErrorObject> {
        self.runtimes
            .iter()
            .find(|runtime| runtime.language() == language)
            .map(|runtime| runtime.as_ref())
            .ok_or_else(|| no_runtime_registered_error(Some(language)))
    }
}

#[derive(Debug, Clone)]
pub struct SessionHandle {
    shell: LangShell,
    session_id: String,
    language: Language,
}

impl SessionHandle {
    pub fn with_language(&self, language: Language) -> Self {
        Self {
            shell: self.shell.clone(),
            session_id: self.session_id.clone(),
            language,
        }
    }

    pub fn run(&self, code: impl Into<String>) -> RunBuilder {
        RunBuilder {
            shell: self.shell.clone(),
            request: RunRequest {
                session_id: SessionId(self.session_id.clone()),
                language: self.language,
                code: code.into(),
                inputs: Map::new(),
                timeout_ms: None,
                limits: None,
                return_snapshot: false,
                validate_only: false,
            },
        }
    }

    pub fn validate(&self, code: impl Into<String>) -> RunBuilder {
        let mut builder = self.run(code);
        builder.request.validate_only = true;
        builder
    }
}

#[derive(Debug, Clone)]
pub struct RunBuilder {
    shell: LangShell,
    request: RunRequest,
}

impl RunBuilder {
    pub fn input(mut self, key: impl Into<String>, value: Value) -> Self {
        self.request.inputs.insert(key.into(), value);
        self
    }

    pub fn inputs(mut self, inputs: Map<String, Value>) -> Self {
        self.request.inputs = inputs;
        self
    }

    pub fn timeout_ms(mut self, timeout_ms: u32) -> Self {
        self.request.timeout_ms = Some(timeout_ms);
        self
    }

    pub fn return_snapshot(mut self, enabled: bool) -> Self {
        self.request.return_snapshot = enabled;
        self
    }

    pub async fn execute(self) -> RunResult {
        self.shell.run(self.request).await
    }
}

#[derive(Clone)]
pub struct LangShellBuilder {
    registry: ToolRegistry,
    limits: SessionLimits,
    file_mounts: Vec<FileMount>,
    http_allowlist: Vec<String>,
    runtime_factories: Vec<Arc<RuntimeFactory>>,
}

impl fmt::Debug for LangShellBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LangShellBuilder")
            .field("registry", &self.registry)
            .field("limits", &self.limits)
            .field("file_mounts", &self.file_mounts)
            .field("http_allowlist", &self.http_allowlist)
            .field("runtime_factories", &self.runtime_factories.len())
            .finish()
    }
}

impl Default for LangShellBuilder {
    fn default() -> Self {
        Self {
            registry: ToolRegistry::new(),
            limits: SessionLimits::default(),
            file_mounts: Vec::new(),
            http_allowlist: Vec::new(),
            runtime_factories: Vec::new(),
        }
    }
}

impl LangShellBuilder {
    pub fn limits(mut self, limits: SessionLimits) -> Self {
        self.limits = limits;
        self
    }

    pub fn memory_limit_mb(mut self, memory_mb: u32) -> Self {
        self.limits.memory_mb = memory_mb;
        self
    }

    pub fn timeout_ms(mut self, timeout_ms: u32) -> Self {
        self.limits.wall_ms = timeout_ms;
        self
    }

    pub fn mount_readonly(
        mut self,
        virtual_path: impl Into<String>,
        host_path: impl Into<PathBuf>,
    ) -> Self {
        self.file_mounts
            .push(FileMount::readonly(virtual_path, host_path));
        self
    }

    pub fn mount_readwrite(
        mut self,
        virtual_path: impl Into<String>,
        host_path: impl Into<PathBuf>,
    ) -> Self {
        self.file_mounts
            .push(FileMount::readwrite(virtual_path, host_path));
        self
    }

    pub fn allow_http_host(mut self, host: impl Into<String>) -> Self {
        self.http_allowlist.push(host.into());
        self
    }

    pub fn runtime<R>(
        mut self,
        factory: impl Fn(ToolRegistry, SessionLimits) -> R + Send + Sync + 'static,
    ) -> Self
    where
        R: LanguageRuntime + 'static,
    {
        self.runtime_factories
            .push(Arc::new(move |registry, limits| {
                let runtime: Arc<dyn LanguageRuntime> = Arc::new(factory(registry, limits));
                runtime
            }));
        self
    }

    pub fn register_sync(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        side_effect: SideEffect,
        handler: impl Fn(ToolCallContext) -> ToolResult + Send + Sync + 'static,
    ) -> Result<Self, ErrorObject> {
        let capability = Capability::new(name, description, side_effect);
        self.registry
            .register(RegisteredTool::sync(capability, handler))?;
        Ok(self)
    }

    pub fn register_async<F, Fut>(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        side_effect: SideEffect,
        handler: F,
    ) -> Result<Self, ErrorObject>
    where
        F: Fn(ToolCallContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ToolResult> + Send + 'static,
    {
        let capability = Capability::new(name, description, side_effect);
        self.registry
            .register(RegisteredTool::asynchronous(capability, move |ctx| {
                Box::pin(handler(ctx)) as ToolFuture
            }))?;
        Ok(self)
    }

    pub fn register_tool(mut self, tool: RegisteredTool) -> Result<Self, ErrorObject> {
        self.registry.register(tool)?;
        Ok(self)
    }

    pub fn build(mut self) -> Result<LangShell, ErrorObject> {
        langshell_tools::register_builtin_tools(
            &mut self.registry,
            ToolConfig {
                file_mounts: self.file_mounts,
                http_allowlist: self.http_allowlist,
            },
        )?;
        let registry = self.registry;
        let limits = self.limits;
        let mut runtimes = Vec::<Arc<dyn LanguageRuntime>>::new();
        for factory in self.runtime_factories {
            let runtime = factory(registry.clone(), limits.clone());
            let language = runtime.language();
            if runtimes
                .iter()
                .any(|existing| existing.language() == language)
            {
                return Err(ErrorObject::new(
                    "INVALID_ARGUMENT",
                    format!(
                        "A runtime for language {} is already registered.",
                        language_name(language)
                    ),
                ));
            }
            runtimes.push(runtime);
        }
        if runtimes.is_empty() {
            return Err(no_runtime_registered_error(None));
        }
        Ok(LangShell { runtimes })
    }
}

fn no_runtime_registered_error(language: Option<Language>) -> ErrorObject {
    match language {
        Some(language) => ErrorObject::new(
            "UNSUPPORTED_FEATURE",
            format!(
                "No language runtime is registered for {}.",
                language_name(language)
            ),
        )
        .with_hint("Register a backend with LangShell::builder().runtime(...) before build()."),
        None => ErrorObject::new(
            "INVALID_ARGUMENT",
            "At least one language runtime must be registered.",
        )
        .with_hint(
            "Call .runtime(langshell_monty::MontyRuntime::new) or .runtime(langshell_deno::DenoRuntime::new) before build().",
        ),
    }
}

fn language_name(language: Language) -> &'static str {
    match language {
        Language::Python => "python",
        Language::TypeScript => "typescript",
    }
}