@flue/sdk 0.4.1 → 0.5.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,22 +1,23 @@
1
1
  import { u as discoverSessionContext } from "./result-K1IRhWKM.mjs";
2
2
  import { n as createCallHandle } from "./abort-Bg3qsAkU.mjs";
3
- import { a as assertRoleExists, n as Session, r as deleteSessionTree } from "./session-CFOByKnM.mjs";
3
+ import { a as assertRoleExists, n as Session, r as deleteSessionTree } from "./session-CRFfAJDq.mjs";
4
4
  import { createCwdSessionEnv, createFlueFs } from "./sandbox.mjs";
5
5
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
6
6
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
7
 
8
- //#region src/agent-client.ts
9
- const DEFAULT_SESSION_ID = "default";
10
- var AgentClient = class {
8
+ //#region src/harness.ts
9
+ const DEFAULT_SESSION_NAME = "default";
10
+ var Harness = class {
11
11
  sessions = {
12
- get: (id, options) => this.openSession(id, "get", options),
13
- create: (id, options) => this.openSession(id, "create", options),
14
- delete: (id) => this.deleteSession(id)
12
+ get: (name, options) => this.openSession(name, "get", options),
13
+ create: (name, options) => this.openSession(name, "create", options),
14
+ delete: (name) => this.deleteSession(name)
15
15
  };
16
16
  fs;
17
17
  openSessions = /* @__PURE__ */ new Map();
18
- constructor(id, config, env, store, eventCallback, agentTools = []) {
19
- this.id = id;
18
+ constructor(instanceId, name, config, env, store, eventCallback, agentTools = []) {
19
+ this.instanceId = instanceId;
20
+ this.name = name;
20
21
  this.config = config;
21
22
  this.env = env;
22
23
  this.store = store;
@@ -24,8 +25,8 @@ var AgentClient = class {
24
25
  this.agentTools = agentTools;
25
26
  this.fs = createFlueFs(env);
26
27
  }
27
- async session(id, options) {
28
- return this.openSession(id, "get-or-create", options);
28
+ async session(name, options) {
29
+ return this.openSession(name, "get-or-create", options);
29
30
  }
30
31
  shell(command, options) {
31
32
  return createCallHandle(options?.signal, async (signal) => {
@@ -41,53 +42,53 @@ var AgentClient = class {
41
42
  };
42
43
  });
43
44
  }
44
- async openSession(id, mode, options) {
45
+ async openSession(name, mode, options) {
45
46
  assertRoleExists(this.config.roles, options?.role);
46
- const sessionId = normalizeSessionId(id);
47
- const open = this.openSessions.get(sessionId);
47
+ const sessionName = normalizeSessionName(name);
48
+ const open = this.openSessions.get(sessionName);
48
49
  if (open) {
49
- if (mode === "create") throw new Error(`[flue] Session "${sessionId}" already exists for agent "${this.id}".`);
50
- if (options?.role !== void 0 && options.role !== open.role) throw new Error(`[flue] Session "${sessionId}" is already open with role ${JSON.stringify(open.role ?? null)}; cannot reopen with role ${JSON.stringify(options.role)}.`);
50
+ if (mode === "create") throw new Error(`[flue] Session "${sessionName}" already exists in harness "${this.name}".`);
51
+ if (options?.role !== void 0 && options.role !== open.role) throw new Error(`[flue] Session "${sessionName}" is already open with role ${JSON.stringify(open.role ?? null)}; cannot reopen with role ${JSON.stringify(options.role)}.`);
51
52
  return open;
52
53
  }
53
- const storageKey = createSessionStorageKey(this.id, sessionId);
54
+ const storageKey = createSessionStorageKey(this.instanceId, this.name, sessionName);
54
55
  const existingData = await this.store.load(storageKey);
55
- if (mode === "get" && !existingData) throw new Error(`[flue] Session "${sessionId}" does not exist for agent "${this.id}".`);
56
- if (mode === "create" && existingData) throw new Error(`[flue] Session "${sessionId}" already exists for agent "${this.id}".`);
56
+ if (mode === "get" && !existingData) throw new Error(`[flue] Session "${sessionName}" does not exist in harness "${this.name}".`);
57
+ if (mode === "create" && existingData) throw new Error(`[flue] Session "${sessionName}" already exists in harness "${this.name}".`);
57
58
  let data = existingData;
58
59
  if (!data && mode !== "get") {
59
60
  data = createEmptySessionData();
60
61
  await this.store.save(storageKey, data);
61
62
  }
62
63
  const session = new Session({
63
- id: sessionId,
64
+ name: sessionName,
64
65
  storageKey,
65
66
  config: this.config,
66
67
  env: this.env,
67
68
  store: this.store,
68
69
  existingData: data,
69
- onAgentEvent: this.eventCallback,
70
+ onAgentEvent: this.decorateEventCallback(this.eventCallback),
70
71
  agentTools: this.agentTools,
71
72
  sessionRole: options?.role,
72
73
  taskDepth: 0,
73
74
  createTaskSession: (taskOptions) => this.createTaskSession(taskOptions),
74
- onDelete: () => this.openSessions.delete(sessionId)
75
+ onDelete: () => this.openSessions.delete(sessionName)
75
76
  });
76
- this.openSessions.set(sessionId, session);
77
+ this.openSessions.set(sessionName, session);
77
78
  return session;
78
79
  }
79
- async deleteSession(id) {
80
- const sessionId = normalizeSessionId(id);
81
- const open = this.openSessions.get(sessionId);
80
+ async deleteSession(name) {
81
+ const sessionName = normalizeSessionName(name);
82
+ const open = this.openSessions.get(sessionName);
82
83
  if (open) {
83
84
  await open.delete();
84
85
  return;
85
86
  }
86
- await deleteSessionTree(this.store, createSessionStorageKey(this.id, sessionId));
87
+ await deleteSessionTree(this.store, createSessionStorageKey(this.instanceId, this.name, sessionName));
87
88
  }
88
89
  async createTaskSession(options) {
89
90
  assertRoleExists(this.config.roles, options.role);
90
- const sessionId = `task:${options.parentSessionId}:${options.taskId}`;
91
+ const sessionName = `task:${options.parentSession}:${options.taskId}`;
91
92
  const taskEnv = options.cwd ? createCwdSessionEnv(options.parentEnv, options.parentEnv.resolvePath(options.cwd)) : options.parentEnv;
92
93
  const localContext = await discoverSessionContext(taskEnv);
93
94
  const taskConfig = {
@@ -95,10 +96,10 @@ var AgentClient = class {
95
96
  systemPrompt: localContext.systemPrompt,
96
97
  skills: localContext.skills
97
98
  };
98
- const storageKey = createSessionStorageKey(this.id, sessionId);
99
+ const storageKey = createSessionStorageKey(this.instanceId, this.name, sessionName);
99
100
  const data = createEmptySessionData();
100
101
  data.metadata = {
101
- parentSessionId: options.parentSessionId,
102
+ parentSession: options.parentSession,
102
103
  taskId: options.taskId,
103
104
  cwd: taskEnv.cwd,
104
105
  role: options.role,
@@ -108,12 +109,13 @@ var AgentClient = class {
108
109
  const eventCallback = this.eventCallback ? (event) => {
109
110
  this.eventCallback?.({
110
111
  ...event,
111
- parentSessionId: event.parentSessionId ?? options.parentSessionId,
112
+ harness: event.harness ?? this.name,
113
+ parentSession: event.parentSession ?? options.parentSession,
112
114
  taskId: event.taskId ?? options.taskId
113
115
  });
114
116
  } : void 0;
115
117
  return new Session({
116
- id: sessionId,
118
+ name: sessionName,
117
119
  storageKey,
118
120
  config: taskConfig,
119
121
  env: taskEnv,
@@ -126,17 +128,29 @@ var AgentClient = class {
126
128
  createTaskSession: (childOptions) => this.createTaskSession(childOptions)
127
129
  });
128
130
  }
131
+ decorateEventCallback(callback) {
132
+ return callback ? (event) => {
133
+ callback({
134
+ ...event,
135
+ harness: event.harness ?? this.name
136
+ });
137
+ } : void 0;
138
+ }
129
139
  };
130
- function normalizeSessionId(id) {
131
- return id ?? DEFAULT_SESSION_ID;
140
+ function normalizeSessionName(name) {
141
+ return name ?? DEFAULT_SESSION_NAME;
132
142
  }
133
- function createSessionStorageKey(agentId, sessionId) {
134
- return `agent-session:${JSON.stringify([agentId, sessionId])}`;
143
+ function createSessionStorageKey(instanceId, harness, sessionName) {
144
+ return `agent-session:${JSON.stringify([
145
+ instanceId,
146
+ harness,
147
+ sessionName
148
+ ])}`;
135
149
  }
136
150
  function createEmptySessionData() {
137
151
  const now = (/* @__PURE__ */ new Date()).toISOString();
138
152
  return {
139
- version: 2,
153
+ version: 3,
140
154
  entries: [],
141
155
  leafId: null,
142
156
  metadata: {},
@@ -269,4 +283,4 @@ function formatMcpResult(result) {
269
283
  }
270
284
 
271
285
  //#endregion
272
- export { AgentClient as n, connectMcpServer as t };
286
+ export { Harness as n, connectMcpServer as t };
@@ -1,4 +1,4 @@
1
- import { O as SessionEnv } from "../types-BAmV4f3Q.mjs";
1
+ import { O as SessionEnv } from "../types-Cdcq_ET2.mjs";
2
2
 
3
3
  //#region src/node/local-env.d.ts
4
4
  interface LocalSessionEnvOptions {
@@ -1,5 +1,15 @@
1
1
  import { registerApiProvider } from "@mariozechner/pi-ai";
2
+ import { ulid } from "ulidx";
2
3
 
4
+ //#region src/runtime/ids.ts
5
+ function generateRunId() {
6
+ return `run_${ulid()}`;
7
+ }
8
+ function generateOperationId() {
9
+ return `op_${ulid()}`;
10
+ }
11
+
12
+ //#endregion
3
13
  //#region src/cloudflare-model.ts
4
14
  /** Pi-ai `Api` slug for the binding-backed Workers AI provider. */
5
15
  const CLOUDFLARE_AI_BINDING_API = "cloudflare-ai-binding";
@@ -155,4 +165,4 @@ function effectiveProviderSlug(name, def) {
155
165
  }
156
166
 
157
167
  //#endregion
158
- export { registerApiProvider$1 as a, CLOUDFLARE_AI_BINDING_API as c, getRegisteredApiKey as i, getModelBinding as n, registerProvider as o, getProviderConfiguration as r, resolveRegisteredModel as s, configureProvider as t };
168
+ export { registerApiProvider$1 as a, CLOUDFLARE_AI_BINDING_API as c, getRegisteredApiKey as i, generateOperationId as l, getModelBinding as n, registerProvider as o, getProviderConfiguration as r, resolveRegisteredModel as s, configureProvider as t, generateRunId as u };
@@ -1,4 +1,4 @@
1
- import { E as SandboxFactory, M as ShellResult, O as SessionEnv, h as FlueFs, i as BashFactory, u as FileStat } from "./types-BAmV4f3Q.mjs";
1
+ import { E as SandboxFactory, M as ShellResult, O as SessionEnv, i as BashFactory, m as FlueFs, u as FileStat } from "./types-Cdcq_ET2.mjs";
2
2
 
3
3
  //#region src/sandbox.d.ts
4
4
  /** Adapt a SessionEnv to the public FlueFs surface. */
package/dist/sandbox.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import "./result-K1IRhWKM.mjs";
2
- import "./providers-DeFRIwp0.mjs";
2
+ import "./providers-BjEEoKLy.mjs";
3
3
  import { t as abortErrorFor } from "./abort-Bg3qsAkU.mjs";
4
- import { i as normalizePath } from "./session-CFOByKnM.mjs";
4
+ import { i as normalizePath } from "./session-CRFfAJDq.mjs";
5
5
 
6
6
  //#region src/sandbox.ts
7
7
  /** Adapt a SessionEnv to the public FlueFs surface. */