@flue/sdk 0.2.0 → 0.3.0

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.
@@ -1,358 +0,0 @@
1
- import { Model, TSchema } from "@mariozechner/pi-ai";
2
- import { AgentMessage } from "@mariozechner/pi-agent-core";
3
- import * as v from "valibot";
4
-
5
- //#region src/types.d.ts
6
- interface Skill {
7
- name: string;
8
- description: string;
9
- /** Markdown body of SKILL.md (below the frontmatter). */
10
- instructions: string;
11
- }
12
- interface Role {
13
- name: string;
14
- description: string;
15
- /** Markdown body of the role file (below the frontmatter). */
16
- instructions: string;
17
- model?: string;
18
- }
19
- /**
20
- * An executable command that can be passed to prompt(), skill(), or shell().
21
- * Registered into just-bash for the duration of the call.
22
- */
23
- interface Command {
24
- name: string;
25
- execute(args: string[]): Promise<{
26
- stdout: string;
27
- stderr: string;
28
- exitCode: number;
29
- }>;
30
- }
31
- /** @deprecated Use `Command` with `defineCommand()` instead. */
32
- interface CommandDef {
33
- name: string;
34
- env?: Record<string, string>;
35
- }
36
- /**
37
- * Custom tool passed to prompt() or skill(). Scoped to the duration of the call.
38
- * Parameters use TypeBox schemas — import `Type` from `@flue/sdk/client`.
39
- */
40
- interface ToolDef<TParams extends TSchema = TSchema> {
41
- /** Must be unique across built-in and custom tools. */
42
- name: string;
43
- /** Tells the LLM when and how to use this tool. */
44
- description: string;
45
- /** TypeBox parameter schema. */
46
- parameters: TParams;
47
- /** Returns a string result sent back to the LLM. Thrown errors become tool errors. */
48
- execute: (args: Record<string, any>) => Promise<string>;
49
- }
50
- interface FileStat {
51
- isFile: boolean;
52
- isDirectory: boolean;
53
- isSymbolicLink: boolean;
54
- size: number;
55
- mtime: Date;
56
- }
57
- /** Registers commands into the isolate's bash. Only present when the sandbox supports it. */
58
- interface CommandSupport {
59
- register(cmd: Command): void;
60
- unregister(name: string): void;
61
- }
62
- /**
63
- * Universal session environment interface. All sandbox modes (isolate, local, remote)
64
- * implement this — no mode-specific branching needed in core logic.
65
- *
66
- * File methods accept both absolute and relative paths (resolved against `cwd`).
67
- */
68
- interface SessionEnv {
69
- exec(command: string, options?: {
70
- cwd?: string;
71
- env?: Record<string, string>;
72
- }): Promise<ShellResult>;
73
- readFile(path: string): Promise<string>;
74
- readFileBuffer(path: string): Promise<Uint8Array>;
75
- writeFile(path: string, content: string | Uint8Array): Promise<void>;
76
- stat(path: string): Promise<FileStat>;
77
- readdir(path: string): Promise<string[]>;
78
- exists(path: string): Promise<boolean>;
79
- mkdir(path: string, options?: {
80
- recursive?: boolean;
81
- }): Promise<void>;
82
- rm(path: string, options?: {
83
- recursive?: boolean;
84
- force?: boolean;
85
- }): Promise<void>;
86
- cwd: string;
87
- /**
88
- * Resolve a relative path against cwd. Absolute paths pass through.
89
- * File methods resolve internally — only needed when you need the absolute path
90
- * for your own logic (e.g., extracting the parent directory).
91
- */
92
- resolvePath(p: string): string;
93
- /** Only present with isolate/local sandboxes. Undefined for remote sandboxes. */
94
- commandSupport?: CommandSupport;
95
- cleanup(): Promise<void>;
96
- }
97
- interface CompactionConfig {
98
- enabled?: boolean;
99
- /** Token buffer to keep free in the context window. Default: 16384 */
100
- reserveTokens?: number;
101
- /** Recent tokens to preserve (not summarized). Default: 20000 */
102
- keepRecentTokens?: number;
103
- }
104
- interface AgentConfig {
105
- /** Discovered at runtime from AGENTS.md + .agents/skills/ in the session's cwd. */
106
- systemPrompt: string;
107
- /** Discovered at runtime from .agents/skills/ in the session's cwd. */
108
- skills: Record<string, Skill>;
109
- roles: Record<string, Role>;
110
- /**
111
- * Session-wide default model. Undefined by default — the user must set it via
112
- * `init({ model: "provider/model-id" })` or pass `{ model }` at each prompt/
113
- * skill/task call site. Calls with no model resolved throw clearly at runtime.
114
- */
115
- model: Model<any> | undefined;
116
- /** Resolve a "provider/modelId" string to a Model instance. Throws on invalid input. */
117
- resolveModel?: (modelString: string) => Model<any>;
118
- compaction?: CompactionConfig;
119
- }
120
- /** Request context passed to agent handler functions. */
121
- interface FlueContext {
122
- readonly sessionId: string;
123
- readonly payload: any;
124
- /** Platform env bindings (process.env on Node, Worker env on Cloudflare). */
125
- readonly env: Record<string, any>;
126
- /** Create a session with sandbox + persistence. Can only be called once per request. */
127
- init(options?: SessionInit): Promise<FlueSession>;
128
- }
129
- /** All fields are optional — omitting gives platform defaults (empty sandbox, platform store, build-time model). */
130
- interface SessionInit {
131
- /**
132
- * - `'empty'` (default): In-memory sandbox, no files, no host access.
133
- * - `'local'`: Mounts process.cwd() at /workspace. Node only.
134
- * - `BashLike`: User-configured just-bash instance.
135
- * - `SandboxFactory`: Connector-wrapped external sandbox (Daytona, CF Containers, etc.).
136
- */
137
- sandbox?: 'empty' | 'local' | SandboxFactory | BashLike;
138
- /** Defaults to platform store (in-memory on Node, DO SQLite on Cloudflare). */
139
- persist?: SessionStore;
140
- /**
141
- * Override the default model for this session. Applies to all prompt(), skill(),
142
- * and task() calls unless overridden at the call site.
143
- *
144
- * Format: `'provider/modelId'` (e.g. `'anthropic/claude-opus-4-20250514'`).
145
- *
146
- * Precedence (highest wins): per-call `model` > role `model` > session `model` > build-time default.
147
- */
148
- model?: string;
149
- /**
150
- * Session-wide commands. Every prompt(), skill(), and shell() call inherits
151
- * this list. Per-call `commands` are merged on top — if a per-call command
152
- * shares a name with a session command, the per-call version wins for that
153
- * call.
154
- */
155
- commands?: Command[];
156
- }
157
- interface FlueSession {
158
- prompt<S extends v.GenericSchema>(text: string, options: PromptOptions<S> & {
159
- result: S;
160
- }): Promise<v.InferOutput<S>>;
161
- prompt(text: string, options?: PromptOptions): Promise<PromptResponse>;
162
- shell(command: string, options?: ShellOptions): Promise<ShellResult>;
163
- skill<S extends v.GenericSchema>(name: string, options: SkillOptions<S> & {
164
- result: S;
165
- }): Promise<v.InferOutput<S>>;
166
- skill(name: string, options?: SkillOptions): Promise<PromptResponse>;
167
- /** Sub-agent task with its own conversation history, context discovery, and compaction. */
168
- task<S extends v.GenericSchema>(prompt: string, options: TaskOptions<S> & {
169
- result: S;
170
- }): Promise<v.InferOutput<S>>;
171
- task(prompt: string, options?: TaskOptions): Promise<PromptResponse>;
172
- destroy(): Promise<void>;
173
- }
174
- interface PromptResponse {
175
- text: string;
176
- }
177
- interface SessionData {
178
- messages: AgentMessage[];
179
- metadata: Record<string, any>;
180
- createdAt: string;
181
- updatedAt: string;
182
- lastCompaction?: {
183
- summary: string;
184
- firstKeptIndex: number;
185
- details?: {
186
- readFiles: string[];
187
- modifiedFiles: string[];
188
- };
189
- };
190
- }
191
- interface SessionStore {
192
- save(id: string, data: SessionData): Promise<void>;
193
- load(id: string): Promise<SessionData | null>;
194
- delete(id: string): Promise<void>;
195
- }
196
- /** All option fields are scoped to the duration of the call. */
197
- interface PromptOptions<S extends v.GenericSchema | undefined = undefined> {
198
- result?: S;
199
- timeout?: number;
200
- commands?: Command[];
201
- tools?: ToolDef[];
202
- role?: string;
203
- /** e.g., 'anthropic/claude-sonnet-4-20250514' */
204
- model?: string;
205
- }
206
- interface SkillOptions<S extends v.GenericSchema | undefined = undefined> {
207
- args?: Record<string, unknown>;
208
- result?: S;
209
- timeout?: number;
210
- commands?: Command[];
211
- tools?: ToolDef[];
212
- role?: string;
213
- model?: string;
214
- }
215
- interface TaskOptions<S extends v.GenericSchema | undefined = undefined> {
216
- /** Workspace directory — AGENTS.md and skills are discovered from here. */
217
- workspace?: string;
218
- result?: S;
219
- role?: string;
220
- model?: string;
221
- }
222
- interface ShellOptions {
223
- env?: Record<string, string>;
224
- cwd?: string;
225
- timeout?: number;
226
- commands?: Command[];
227
- }
228
- interface ShellResult {
229
- stdout: string;
230
- stderr: string;
231
- exitCode: number;
232
- }
233
- /** Wraps external sandboxes (Daytona, CF Containers, etc.) into Flue's SessionEnv. */
234
- interface SandboxFactory {
235
- createSessionEnv(options: {
236
- sessionId: string;
237
- workspace?: string;
238
- }): Promise<SessionEnv>;
239
- }
240
- /**
241
- * Structural type for duck-type detection of just-bash `Bash` instances in init().
242
- * Purely structural — no just-bash import, so client.ts stays platform-agnostic.
243
- */
244
- interface BashLike {
245
- exec(command: string, options?: {
246
- cwd?: string;
247
- env?: Record<string, string>;
248
- }): Promise<ShellResult>;
249
- getCwd(): string;
250
- fs: {
251
- readFile(path: string, options?: any): Promise<string>;
252
- readFileBuffer(path: string): Promise<Uint8Array>;
253
- writeFile(path: string, content: string | Uint8Array, options?: any): Promise<void>;
254
- stat(path: string): Promise<any>;
255
- readdir(path: string): Promise<string[]>;
256
- exists(path: string): Promise<boolean>;
257
- mkdir(path: string, options?: {
258
- recursive?: boolean;
259
- }): Promise<void>;
260
- rm(path: string, options?: {
261
- recursive?: boolean;
262
- force?: boolean;
263
- }): Promise<void>;
264
- resolvePath(base: string, path: string): string;
265
- };
266
- registerCommand?(cmd: any): void;
267
- }
268
- type FlueEvent = {
269
- type: 'agent_start';
270
- } | {
271
- type: 'text_delta';
272
- text: string;
273
- } | {
274
- type: 'tool_start';
275
- toolName: string;
276
- toolCallId: string;
277
- args?: any;
278
- } | {
279
- type: 'tool_end';
280
- toolName: string;
281
- toolCallId: string;
282
- isError: boolean;
283
- result?: any;
284
- } | {
285
- type: 'turn_end';
286
- } | {
287
- type: 'command_start';
288
- command: string;
289
- args: string[];
290
- } | {
291
- type: 'command_end';
292
- command: string;
293
- exitCode: number;
294
- } | {
295
- type: 'compaction_start';
296
- reason: 'threshold' | 'overflow';
297
- estimatedTokens: number;
298
- } | {
299
- type: 'compaction_end';
300
- messagesBefore: number;
301
- messagesAfter: number;
302
- } | {
303
- type: 'task_start';
304
- workspace: string;
305
- } | {
306
- type: 'task_end';
307
- } | {
308
- type: 'done';
309
- } | {
310
- type: 'error';
311
- error: string;
312
- };
313
- type FlueEventCallback = (event: FlueEvent) => void;
314
- interface AgentInfo {
315
- name: string;
316
- filePath: string;
317
- triggers: {
318
- webhook?: boolean;
319
- cron?: string;
320
- };
321
- }
322
- interface BuildContext {
323
- agents: AgentInfo[];
324
- roles: Record<string, Role>;
325
- /** The workspace root: the directory directly containing agents/ and roles/. */
326
- workspaceDir: string;
327
- /** Where dist/ is written. Typically the project root, independent of workspaceDir. */
328
- outputDir: string;
329
- options: BuildOptions;
330
- }
331
- /** Controls the build output format for a target platform. */
332
- interface BuildPlugin {
333
- name: string;
334
- generateEntryPoint(ctx: BuildContext): string;
335
- esbuildOptions(ctx: BuildContext): Record<string, any>;
336
- /** Additional files to write to dist/ (e.g., wrangler.jsonc, Dockerfile). */
337
- additionalOutputs?(ctx: BuildContext): Record<string, string>;
338
- }
339
- interface BuildOptions {
340
- /**
341
- * The workspace directory: the directory directly containing agents/ and
342
- * roles/. Pass an explicit path — no .flue/ waterfall is performed here.
343
- * Callers that want the waterfall behavior (e.g. the CLI when --workspace
344
- * is omitted) should resolve it themselves with `resolveWorkspaceFromCwd`.
345
- */
346
- workspaceDir: string;
347
- /**
348
- * Where to write the dist/ directory. Independent of workspaceDir — typically
349
- * the project root, so platform config like wrangler.jsonc ends up where the
350
- * deploy tool expects it.
351
- */
352
- outputDir: string;
353
- target?: 'node' | 'cloudflare';
354
- /** Overrides `target` when provided. */
355
- plugin?: BuildPlugin;
356
- }
357
- //#endregion
358
- export { ShellOptions as C, TaskOptions as D, SkillOptions as E, ToolDef as O, SessionStore as S, Skill as T, Role as _, BuildOptions as a, SessionEnv as b, CommandDef as c, FlueContext as d, FlueEvent as f, PromptResponse as g, PromptOptions as h, BuildContext as i, CommandSupport as l, FlueSession as m, AgentInfo as n, BuildPlugin as o, FlueEventCallback as p, BashLike as r, Command as s, AgentConfig as t, FileStat as u, SandboxFactory as v, ShellResult as w, SessionInit as x, SessionData as y };