@nones/agentflow-core 0.1.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ import { stat } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+ import { AgentFlowError } from "./errors";
4
+ import type { AgentSpec, Profile, SessionConfiguration } from "./types";
5
+
6
+ const CONTEXT_BYTES = 64 * 1024;
7
+ const MAX_PATHS = 32;
8
+
9
+ function paths(
10
+ agent: AgentSpec,
11
+ profile: Profile,
12
+ key: "skillDirectories" | "contextFiles",
13
+ ) {
14
+ const values = agent.settings?.[key] ?? profile.settings[key];
15
+ if (values === undefined) return [];
16
+ if (
17
+ !Array.isArray(values) ||
18
+ values.length > MAX_PATHS ||
19
+ values.some((value) => typeof value !== "string" || value.trim() === "")
20
+ ) {
21
+ throw new AgentFlowError(
22
+ "validation",
23
+ `${key} must contain at most ${MAX_PATHS} paths`,
24
+ );
25
+ }
26
+ return [...new Set(values.map((value) => resolve(agent.cwd, value)))];
27
+ }
28
+
29
+ export async function configureSession(
30
+ agent: AgentSpec,
31
+ profile: Profile,
32
+ moduleSession?: SessionConfiguration,
33
+ ): Promise<SessionConfiguration | undefined> {
34
+ const directories = paths(agent, profile, "skillDirectories");
35
+ const files = paths(agent, profile, "contextFiles");
36
+ if (directories.length === 0 && files.length === 0) return moduleSession;
37
+
38
+ for (const directory of directories) {
39
+ const info = await stat(directory).catch(() => undefined);
40
+ if (!info?.isDirectory())
41
+ throw new AgentFlowError(
42
+ "validation",
43
+ `skill directory is unavailable: ${directory}`,
44
+ );
45
+ }
46
+ const texts =
47
+ directories.length === 0
48
+ ? []
49
+ : [
50
+ "Additional skill directories. Read the applicable SKILL.md from these directories before using that skill.",
51
+ ...directories.map((directory) => `- ${JSON.stringify(directory)}`),
52
+ ];
53
+ let remaining = CONTEXT_BYTES;
54
+ for (const path of files) {
55
+ const info = await stat(path).catch(() => undefined);
56
+ if (!info?.isFile())
57
+ throw new AgentFlowError(
58
+ "validation",
59
+ `context file is unavailable: ${path}`,
60
+ );
61
+ const bytes = await Bun.file(path)
62
+ .slice(0, remaining + 1)
63
+ .arrayBuffer();
64
+ if (info.size > remaining || bytes.byteLength > remaining) {
65
+ throw new AgentFlowError(
66
+ "validation",
67
+ "additional context exceeds 64 KiB",
68
+ );
69
+ }
70
+ remaining -= bytes.byteLength;
71
+ texts.push(
72
+ `Additional context from ${JSON.stringify(path)}\n${new TextDecoder().decode(bytes)}`,
73
+ );
74
+ }
75
+
76
+ return {
77
+ ...moduleSession,
78
+ instructions: [moduleSession?.instructions, texts.join("\n\n")]
79
+ .filter(Boolean)
80
+ .join("\n\n"),
81
+ };
82
+ }