* refactor: rename open-harness to open-agents * refactor: replace ai-sdk import with inline FetchFunction type * refactor: rename "Open Agents" to "Open Agent" * fix: align chat runtime package imports --------- Co-authored-by: willsather <56037657+willsather@users.noreply.github.com> Co-authored-by: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { discoverSkills } from "@open-agents/agent";
|
|
import { connectSandbox } from "@open-agents/sandbox";
|
|
import { getUserGitHubToken } from "@/lib/github/token";
|
|
import { DEFAULT_SANDBOX_PORTS } from "@/lib/sandbox/config";
|
|
import { getSandboxSkillDirectories } from "@/lib/skills/directories";
|
|
import { getCachedSkills, setCachedSkills } from "@/lib/skills-cache";
|
|
import type { SessionRecord } from "./chat-context";
|
|
|
|
type DiscoveredSkills = Awaited<ReturnType<typeof discoverSkills>>;
|
|
type ConnectedSandbox = Awaited<ReturnType<typeof connectSandbox>>;
|
|
type ActiveSandboxState = NonNullable<SessionRecord["sandboxState"]>;
|
|
|
|
async function loadSessionSkills(
|
|
sessionId: string,
|
|
sandboxState: ActiveSandboxState,
|
|
sandbox: ConnectedSandbox,
|
|
): Promise<DiscoveredSkills> {
|
|
const cachedSkills = await getCachedSkills(sessionId, sandboxState);
|
|
if (cachedSkills !== null) {
|
|
return cachedSkills;
|
|
}
|
|
|
|
// Discover project-level skills from the sandbox working directory plus
|
|
// global skills installed outside the repo working tree.
|
|
// TODO: Optimize if this becomes a bottleneck (~20ms no skills, ~130ms with 5 skills)
|
|
const skillDirs = await getSandboxSkillDirectories(sandbox);
|
|
|
|
const discoveredSkills = await discoverSkills(sandbox, skillDirs);
|
|
await setCachedSkills(sessionId, sandboxState, discoveredSkills);
|
|
return discoveredSkills;
|
|
}
|
|
|
|
export async function createChatRuntime(params: {
|
|
userId: string;
|
|
sessionId: string;
|
|
sessionRecord: SessionRecord;
|
|
}): Promise<{
|
|
sandbox: ConnectedSandbox;
|
|
skills: DiscoveredSkills;
|
|
}> {
|
|
const { userId, sessionId, sessionRecord } = params;
|
|
|
|
const sandboxState = sessionRecord.sandboxState;
|
|
if (!sandboxState) {
|
|
throw new Error("Sandbox state is required to create chat runtime");
|
|
}
|
|
|
|
const githubToken = await getUserGitHubToken(userId);
|
|
|
|
const sandbox = await connectSandbox(sandboxState, {
|
|
githubToken: githubToken ?? undefined,
|
|
ports: DEFAULT_SANDBOX_PORTS,
|
|
});
|
|
|
|
const skills = await loadSessionSkills(sessionId, sandboxState, sandbox);
|
|
|
|
return {
|
|
sandbox,
|
|
skills,
|
|
};
|
|
}
|