summaryrefslogtreecommitdiffhomepage
path: root/packages/system-prompt/src/extension.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/system-prompt/src/extension.ts')
-rw-r--r--packages/system-prompt/src/extension.ts206
1 files changed, 103 insertions, 103 deletions
diff --git a/packages/system-prompt/src/extension.ts b/packages/system-prompt/src/extension.ts
index 5cb9125..d281bf8 100644
--- a/packages/system-prompt/src/extension.ts
+++ b/packages/system-prompt/src/extension.ts
@@ -17,43 +17,43 @@ import { createSystemPromptService } from "./service.js";
import { systemPromptHandle } from "./types.js";
export const manifest: Manifest = {
- id: "system-prompt",
- name: "System Prompt",
- version: "0.0.0",
- apiVersion: "^0.1.0",
- trust: "bundled",
- activation: "eager",
- // exec-backend provides the resolver used to obtain a remote ExecBackend
- // when computerId is set. The lookup is lazy (at construct time, not
- // activation), but declaring the dep keeps the DAG honest.
- dependsOn: ["exec-backend"],
- capabilities: { fs: true, spawn: true },
- contributes: { services: ["system-prompt"] },
+ id: "system-prompt",
+ name: "System Prompt",
+ version: "0.0.0",
+ apiVersion: "^0.1.0",
+ trust: "bundled",
+ activation: "eager",
+ // exec-backend provides the resolver used to obtain a remote ExecBackend
+ // when computerId is set. The lookup is lazy (at construct time, not
+ // activation), but declaring the dep keeps the DAG honest.
+ dependsOn: ["exec-backend"],
+ capabilities: { fs: true, spawn: true },
+ contributes: { services: ["system-prompt"] },
};
/** Run a command and capture stdout/stderr (used for git). */
async function realSpawn(
- command: readonly string[],
- opts: { readonly cwd: string },
+ command: readonly string[],
+ opts: { readonly cwd: string },
): Promise<GitSpawnResult> {
- const proc = Bun.spawn([...command], {
- cwd: opts.cwd,
- stdout: "pipe",
- stderr: "pipe",
- });
- const [stdout, stderr, exitCode] = await Promise.all([
- Bun.readableStreamToText(proc.stdout),
- Bun.readableStreamToText(proc.stderr),
- proc.exited,
- ]);
- return { stdout, stderr, exitCode };
+ const proc = Bun.spawn([...command], {
+ cwd: opts.cwd,
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+ const [stdout, stderr, exitCode] = await Promise.all([
+ Bun.readableStreamToText(proc.stdout),
+ Bun.readableStreamToText(proc.stderr),
+ proc.exited,
+ ]);
+ return { stdout, stderr, exitCode };
}
function realFs() {
- return {
- readText: async (path: string): Promise<string> => Bun.file(path).text(),
- exists: async (path: string): Promise<boolean> => Bun.file(path).exists(),
- };
+ return {
+ readText: async (path: string): Promise<string> => Bun.file(path).text(),
+ exists: async (path: string): Promise<boolean> => Bun.file(path).exists(),
+ };
}
const localAdapters: ResolverAdapters = { spawn: realSpawn, fs: realFs() };
@@ -63,25 +63,25 @@ const localAdapters: ResolverAdapters = { spawn: realSpawn, fs: realFs() };
* `null` on any error (the resolver treats null as "unavailable").
*/
async function remoteCommand(
- backend: ExecBackend,
- command: string,
- cwd: string,
+ backend: ExecBackend,
+ command: string,
+ cwd: string,
): Promise<string | null> {
- let stdout = "";
- try {
- const result = await backend.spawn({
- command,
- cwd,
- signal: new AbortController().signal,
- timeout: 10_000,
- onOutput: (data: string, stream: "stdout" | "stderr") => {
- if (stream === "stdout") stdout += data;
- },
- });
- return result.exitCode === 0 ? stdout.trim() : null;
- } catch {
- return null;
- }
+ let stdout = "";
+ try {
+ const result = await backend.spawn({
+ command,
+ cwd,
+ signal: new AbortController().signal,
+ timeout: 10_000,
+ onOutput: (data: string, stream: "stdout" | "stderr") => {
+ if (stream === "stdout") stdout += data;
+ },
+ });
+ return result.exitCode === 0 ? stdout.trim() : null;
+ } catch {
+ return null;
+ }
}
/**
@@ -90,68 +90,68 @@ async function remoteCommand(
* prompt reflect the REMOTE machine's OS, hostname, and git state.
*/
function buildRemoteAdapters(backend: ExecBackend, cwd: string): ResolverAdapters {
- return {
- spawn: async (command, opts) => {
- let stdout = "";
- let stderr = "";
- const result = await backend.spawn({
- command: command.join(" "),
- cwd: opts.cwd,
- signal: new AbortController().signal,
- timeout: 10_000,
- onOutput: (data: string, stream: "stdout" | "stderr") => {
- if (stream === "stdout") stdout += data;
- else stderr += data;
- },
- });
- return { stdout, stderr, exitCode: result.exitCode };
- },
- fs: {
- readText: async (path: string): Promise<string> => backend.readFile(path),
- exists: async (path: string): Promise<boolean> => backend.exists(path),
- },
- // Run hostname/uname on the REMOTE machine. These are resolved once per
- // construct call (cached by the service's cwd+computerId cache). If the
- // remote command fails, fall back to a generic value (the resolver will
- // still read /etc/os-release via SFTP for the distro name).
- hostname: async () => (await remoteCommand(backend, "hostname", cwd)) ?? "remote",
- platform: async () => (await remoteCommand(backend, "uname -s", cwd)) ?? "linux",
- };
+ return {
+ spawn: async (command, opts) => {
+ let stdout = "";
+ let stderr = "";
+ const result = await backend.spawn({
+ command: command.join(" "),
+ cwd: opts.cwd,
+ signal: new AbortController().signal,
+ timeout: 10_000,
+ onOutput: (data: string, stream: "stdout" | "stderr") => {
+ if (stream === "stdout") stdout += data;
+ else stderr += data;
+ },
+ });
+ return { stdout, stderr, exitCode: result.exitCode };
+ },
+ fs: {
+ readText: async (path: string): Promise<string> => backend.readFile(path),
+ exists: async (path: string): Promise<boolean> => backend.exists(path),
+ },
+ // Run hostname/uname on the REMOTE machine. These are resolved once per
+ // construct call (cached by the service's cwd+computerId cache). If the
+ // remote command fails, fall back to a generic value (the resolver will
+ // still read /etc/os-release via SFTP for the distro name).
+ hostname: async () => (await remoteCommand(backend, "hostname", cwd)) ?? "remote",
+ platform: async () => (await remoteCommand(backend, "uname -s", cwd)) ?? "linux",
+ };
}
export function activate(host: HostAPI): void {
- const storage = host.storage("system-prompt");
+ const storage = host.storage("system-prompt");
- /**
- * Resolve remote-backed adapters for a given computerId. Looks up the
- * ExecBackendResolver (provided by exec-backend, which delegates to ssh's
- * remote factory when computerId is set) and wraps it in ResolverAdapters.
- * Falls back to local adapters if the resolver or backend is unavailable.
- */
- const resolveRemoteAdapters = async (
- computerId: string,
- cwd: string,
- ): Promise<ResolverAdapters> => {
- try {
- const resolver = host.getService(execBackendHandle);
- const backend = resolver(computerId);
- return buildRemoteAdapters(backend, cwd);
- } catch {
- // exec-backend not loaded or resolver unavailable → local.
- return localAdapters;
- }
- };
+ /**
+ * Resolve remote-backed adapters for a given computerId. Looks up the
+ * ExecBackendResolver (provided by exec-backend, which delegates to ssh's
+ * remote factory when computerId is set) and wraps it in ResolverAdapters.
+ * Falls back to local adapters if the resolver or backend is unavailable.
+ */
+ const resolveRemoteAdapters = async (
+ computerId: string,
+ cwd: string,
+ ): Promise<ResolverAdapters> => {
+ try {
+ const resolver = host.getService(execBackendHandle);
+ const backend = resolver(computerId);
+ return buildRemoteAdapters(backend, cwd);
+ } catch {
+ // exec-backend not loaded or resolver unavailable → local.
+ return localAdapters;
+ }
+ };
- const service = createSystemPromptService({
- storage,
- adapters: localAdapters,
- resolveRemoteAdapters,
- });
- host.provideService(systemPromptHandle, service);
- host.logger.info("system-prompt: activated");
+ const service = createSystemPromptService({
+ storage,
+ adapters: localAdapters,
+ resolveRemoteAdapters,
+ });
+ host.provideService(systemPromptHandle, service);
+ host.logger.info("system-prompt: activated");
}
export const extension: Extension = {
- manifest,
- activate,
+ manifest,
+ activate,
};