feat(listen): add protocol_v2, move ws server to v2 (#1387)
Co-authored-by: Shubham Naik <shub@letta.com> Co-authored-by: Letta Code <noreply@letta.com>
This commit is contained in:
71
src/runtime-context.ts
Normal file
71
src/runtime-context.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import type { SkillSource } from "./agent/skills";
|
||||
|
||||
export type RuntimePermissionMode =
|
||||
| "default"
|
||||
| "acceptEdits"
|
||||
| "plan"
|
||||
| "bypassPermissions";
|
||||
|
||||
export interface RuntimeContextSnapshot {
|
||||
agentId?: string | null;
|
||||
conversationId?: string | null;
|
||||
skillsDirectory?: string | null;
|
||||
skillSources?: SkillSource[];
|
||||
workingDirectory?: string | null;
|
||||
permissionMode?: RuntimePermissionMode;
|
||||
planFilePath?: string | null;
|
||||
modeBeforePlan?: RuntimePermissionMode | null;
|
||||
}
|
||||
|
||||
const runtimeContextStorage = new AsyncLocalStorage<RuntimeContextSnapshot>();
|
||||
|
||||
export function getRuntimeContext(): RuntimeContextSnapshot | undefined {
|
||||
return runtimeContextStorage.getStore();
|
||||
}
|
||||
|
||||
export function runWithRuntimeContext<T>(
|
||||
snapshot: RuntimeContextSnapshot,
|
||||
fn: () => T,
|
||||
): T {
|
||||
const parent = runtimeContextStorage.getStore();
|
||||
return runtimeContextStorage.run(
|
||||
{
|
||||
...parent,
|
||||
...snapshot,
|
||||
...(snapshot.skillSources
|
||||
? { skillSources: [...snapshot.skillSources] }
|
||||
: {}),
|
||||
},
|
||||
fn,
|
||||
);
|
||||
}
|
||||
|
||||
export function runOutsideRuntimeContext<T>(fn: () => T): T {
|
||||
return runtimeContextStorage.exit(fn);
|
||||
}
|
||||
|
||||
export function updateRuntimeContext(
|
||||
update: Partial<RuntimeContextSnapshot>,
|
||||
): void {
|
||||
const current = runtimeContextStorage.getStore();
|
||||
if (!current) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object.assign(
|
||||
current,
|
||||
update,
|
||||
update.skillSources && {
|
||||
skillSources: [...update.skillSources],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function getCurrentWorkingDirectory(): string {
|
||||
const workingDirectory = runtimeContextStorage.getStore()?.workingDirectory;
|
||||
if (typeof workingDirectory === "string" && workingDirectory.length > 0) {
|
||||
return workingDirectory;
|
||||
}
|
||||
return process.env.USER_CWD || process.cwd();
|
||||
}
|
||||
Reference in New Issue
Block a user