Spaces:
Paused
Paused
Download server/src/adapters/process/execute.ts from testfirela/paperclip-app: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/testfirela/paperclip-app/resolve/main/server/src/adapters/process/execute.ts
- Command line
-
hf download hf://spaces/testfirela/paperclip-app/server/src/adapters/process/execute.ts
-
curl -L -o execute.ts https://huggingface.co/spaces/testfirela/paperclip-app/resolve/main/server/src/adapters/process/execute.ts
2.19 kB
| import type { AdapterExecutionContext, AdapterExecutionResult } from "../types.js"; | |
| import { | |
| asString, | |
| asNumber, | |
| asStringArray, | |
| parseObject, | |
| buildPaperclipEnv, | |
| buildInvocationEnvForLogs, | |
| ensurePathInEnv, | |
| resolveCommandForLogs, | |
| runChildProcess, | |
| } from "../utils.js"; | |
| export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExecutionResult> { | |
| const { runId, agent, config, onLog, onMeta } = ctx; | |
| const command = asString(config.command, ""); | |
| if (!command) throw new Error("Process adapter missing command"); | |
| const args = asStringArray(config.args); | |
| const cwd = asString(config.cwd, process.cwd()); | |
| const envConfig = parseObject(config.env); | |
| const env: Record<string, string> = { ...buildPaperclipEnv(agent) }; | |
| for (const [k, v] of Object.entries(envConfig)) { | |
| if (typeof v === "string") env[k] = v; | |
| } | |
| const runtimeEnv = ensurePathInEnv({ ...process.env, ...env }); | |
| const resolvedCommand = await resolveCommandForLogs(command, cwd, runtimeEnv); | |
| const loggedEnv = buildInvocationEnvForLogs(env, { | |
| runtimeEnv, | |
| includeRuntimeKeys: ["HOME"], | |
| resolvedCommand, | |
| }); | |
| const timeoutSec = asNumber(config.timeoutSec, 0); | |
| const graceSec = asNumber(config.graceSec, 15); | |
| if (onMeta) { | |
| await onMeta({ | |
| adapterType: "process", | |
| command: resolvedCommand, | |
| cwd, | |
| commandArgs: args, | |
| env: loggedEnv, | |
| }); | |
| } | |
| const proc = await runChildProcess(runId, command, args, { | |
| cwd, | |
| env, | |
| timeoutSec, | |
| graceSec, | |
| onLog, | |
| }); | |
| if (proc.timedOut) { | |
| return { | |
| exitCode: proc.exitCode, | |
| signal: proc.signal, | |
| timedOut: true, | |
| errorMessage: `Timed out after ${timeoutSec}s`, | |
| }; | |
| } | |
| if ((proc.exitCode ?? 0) !== 0) { | |
| return { | |
| exitCode: proc.exitCode, | |
| signal: proc.signal, | |
| timedOut: false, | |
| errorMessage: `Process exited with code ${proc.exitCode ?? -1}`, | |
| resultJson: { | |
| stdout: proc.stdout, | |
| stderr: proc.stderr, | |
| }, | |
| }; | |
| } | |
| return { | |
| exitCode: proc.exitCode, | |
| signal: proc.signal, | |
| timedOut: false, | |
| resultJson: { | |
| stdout: proc.stdout, | |
| stderr: proc.stderr, | |
| }, | |
| }; | |
| } | |