@flue/sdk 0.4.1 → 0.5.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.
@@ -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-BQULcLE9.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,55 @@ 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);
55
+ const affinityKey = createSessionAffinityKey(this.instanceId, this.name, sessionName);
54
56
  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}".`);
57
+ if (mode === "get" && !existingData) throw new Error(`[flue] Session "${sessionName}" does not exist in harness "${this.name}".`);
58
+ if (mode === "create" && existingData) throw new Error(`[flue] Session "${sessionName}" already exists in harness "${this.name}".`);
57
59
  let data = existingData;
58
60
  if (!data && mode !== "get") {
59
61
  data = createEmptySessionData();
60
62
  await this.store.save(storageKey, data);
61
63
  }
62
64
  const session = new Session({
63
- id: sessionId,
65
+ name: sessionName,
64
66
  storageKey,
67
+ affinityKey,
65
68
  config: this.config,
66
69
  env: this.env,
67
70
  store: this.store,
68
71
  existingData: data,
69
- onAgentEvent: this.eventCallback,
72
+ onAgentEvent: this.decorateEventCallback(this.eventCallback),
70
73
  agentTools: this.agentTools,
71
74
  sessionRole: options?.role,
72
75
  taskDepth: 0,
73
76
  createTaskSession: (taskOptions) => this.createTaskSession(taskOptions),
74
- onDelete: () => this.openSessions.delete(sessionId)
77
+ onDelete: () => this.openSessions.delete(sessionName)
75
78
  });
76
- this.openSessions.set(sessionId, session);
79
+ this.openSessions.set(sessionName, session);
77
80
  return session;
78
81
  }
79
- async deleteSession(id) {
80
- const sessionId = normalizeSessionId(id);
81
- const open = this.openSessions.get(sessionId);
82
+ async deleteSession(name) {
83
+ const sessionName = normalizeSessionName(name);
84
+ const open = this.openSessions.get(sessionName);
82
85
  if (open) {
83
86
  await open.delete();
84
87
  return;
85
88
  }
86
- await deleteSessionTree(this.store, createSessionStorageKey(this.id, sessionId));
89
+ await deleteSessionTree(this.store, createSessionStorageKey(this.instanceId, this.name, sessionName));
87
90
  }
88
91
  async createTaskSession(options) {
89
92
  assertRoleExists(this.config.roles, options.role);
90
- const sessionId = `task:${options.parentSessionId}:${options.taskId}`;
93
+ const sessionName = `task:${options.parentSession}:${options.taskId}`;
91
94
  const taskEnv = options.cwd ? createCwdSessionEnv(options.parentEnv, options.parentEnv.resolvePath(options.cwd)) : options.parentEnv;
92
95
  const localContext = await discoverSessionContext(taskEnv);
93
96
  const taskConfig = {
@@ -95,10 +98,11 @@ var AgentClient = class {
95
98
  systemPrompt: localContext.systemPrompt,
96
99
  skills: localContext.skills
97
100
  };
98
- const storageKey = createSessionStorageKey(this.id, sessionId);
101
+ const storageKey = createSessionStorageKey(this.instanceId, this.name, sessionName);
102
+ const affinityKey = createSessionAffinityKey(this.instanceId, this.name, sessionName);
99
103
  const data = createEmptySessionData();
100
104
  data.metadata = {
101
- parentSessionId: options.parentSessionId,
105
+ parentSession: options.parentSession,
102
106
  taskId: options.taskId,
103
107
  cwd: taskEnv.cwd,
104
108
  role: options.role,
@@ -108,13 +112,15 @@ var AgentClient = class {
108
112
  const eventCallback = this.eventCallback ? (event) => {
109
113
  this.eventCallback?.({
110
114
  ...event,
111
- parentSessionId: event.parentSessionId ?? options.parentSessionId,
115
+ harness: event.harness ?? this.name,
116
+ parentSession: event.parentSession ?? options.parentSession,
112
117
  taskId: event.taskId ?? options.taskId
113
118
  });
114
119
  } : void 0;
115
120
  return new Session({
116
- id: sessionId,
121
+ name: sessionName,
117
122
  storageKey,
123
+ affinityKey,
118
124
  config: taskConfig,
119
125
  env: taskEnv,
120
126
  store: this.store,
@@ -126,17 +132,32 @@ var AgentClient = class {
126
132
  createTaskSession: (childOptions) => this.createTaskSession(childOptions)
127
133
  });
128
134
  }
135
+ decorateEventCallback(callback) {
136
+ return callback ? (event) => {
137
+ callback({
138
+ ...event,
139
+ harness: event.harness ?? this.name
140
+ });
141
+ } : void 0;
142
+ }
129
143
  };
130
- function normalizeSessionId(id) {
131
- return id ?? DEFAULT_SESSION_ID;
144
+ function normalizeSessionName(name) {
145
+ return name ?? DEFAULT_SESSION_NAME;
146
+ }
147
+ function createSessionStorageKey(instanceId, harness, sessionName) {
148
+ return `agent-session:${JSON.stringify([
149
+ instanceId,
150
+ harness,
151
+ sessionName
152
+ ])}`;
132
153
  }
133
- function createSessionStorageKey(agentId, sessionId) {
134
- return `agent-session:${JSON.stringify([agentId, sessionId])}`;
154
+ function createSessionAffinityKey(instanceId, harness, sessionName) {
155
+ return `${instanceId}::${harness}::${sessionName}`;
135
156
  }
136
157
  function createEmptySessionData() {
137
158
  const now = (/* @__PURE__ */ new Date()).toISOString();
138
159
  return {
139
- version: 2,
160
+ version: 3,
140
161
  entries: [],
141
162
  leafId: null,
142
163
  metadata: {},
@@ -269,4 +290,4 @@ function formatMcpResult(result) {
269
290
  }
270
291
 
271
292
  //#endregion
272
- export { AgentClient as n, connectMcpServer as t };
293
+ export { Harness as n, connectMcpServer as t };
@@ -1,4 +1,4 @@
1
- import { L as ToolDef } from "./types-BAmV4f3Q.mjs";
1
+ import { L as ToolDef } from "./types-Cdcq_ET2.mjs";
2
2
 
3
3
  //#region src/mcp.d.ts
4
4
  type McpTransport = 'streamable-http' | 'sse';
@@ -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-BQULcLE9.mjs";
5
5
 
6
6
  //#region src/sandbox.ts
7
7
  /** Adapt a SessionEnv to the public FlueFs surface. */