forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt-file.ts
More file actions
29 lines (25 loc) · 870 Bytes
/
Copy pathprompt-file.ts
File metadata and controls
29 lines (25 loc) · 870 Bytes
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
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import type { RuntimeAgentDef } from './types.js';
export type PreparedPromptFile = {
path: string;
cleanup: () => Promise<void>;
};
export async function preparePromptFileForAgent(
def: RuntimeAgentDef | null | undefined,
prompt: string,
label: string,
): Promise<PreparedPromptFile | null> {
if (!def?.promptViaFile) return null;
const safeLabel = label.replace(/[^a-zA-Z0-9_.-]/g, '-').slice(0, 80) || 'prompt';
const dir = await fs.mkdtemp(path.join(os.tmpdir(), `od-${def.id}-${safeLabel}-`));
const filePath = path.join(dir, 'prompt.md');
await fs.writeFile(filePath, prompt, { encoding: 'utf8', mode: 0o600 });
return {
path: filePath,
cleanup: async () => {
await fs.rm(dir, { recursive: true, force: true });
},
};
}