@bobannomi/client 0.2.0 → 0.3.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.
package/dist/service.js CHANGED
@@ -2198,16 +2198,25 @@ function validateSessionId(id2) {
2198
2198
  }
2199
2199
  function parseCreateSessionRequest(value) {
2200
2200
  const body = record3(value);
2201
- fields2(body, ["id", "requestId", "cwd", "projectId", "title", "storage", "configuration", "optionsRevision", "metadata"]);
2201
+ fields2(body, ["id", "requestId", "cwd", "projectId", "workspaceId", "outsideProject", "title", "storage", "configuration", "optionsRevision", "metadata"]);
2202
2202
  const cwd = optionalString2(body, "cwd");
2203
+ const workspaceId = optionalString2(body, "workspaceId");
2204
+ const outsideProject = optionalBoolean2(body, "outsideProject");
2203
2205
  const id2 = validateSessionId(requiredString2(body, "id"));
2204
2206
  const requestId = requiredString2(body, "requestId");
2205
2207
  if (cwd !== void 0 && !cwd.trim()) throw new ProtocolValidationError("cwd cannot be empty", "cwd");
2206
2208
  if (!requestId.trim()) throw new ProtocolValidationError("requestId cannot be empty", "requestId");
2207
- const target = cwd === void 0 ? { projectId: requiredString2(body, "projectId") } : { cwd };
2208
- if (cwd === void 0 && !target.projectId?.trim()) throw new ProtocolValidationError("projectId cannot be empty", "projectId");
2209
+ if (workspaceId !== void 0 && !workspaceId.trim()) throw new ProtocolValidationError("workspaceId cannot be empty", "workspaceId");
2210
+ if (outsideProject === false || outsideProject === true && workspaceId !== void 0)
2211
+ throw new ProtocolValidationError("outsideProject must be true and used without workspaceId", "outsideProject");
2212
+ if ((outsideProject === true || workspaceId !== void 0) && (cwd !== void 0 || body.projectId !== void 0))
2213
+ throw new ProtocolValidationError("Supply one session directory choice", outsideProject === true ? "outsideProject" : "workspaceId");
2214
+ const projectId = optionalString2(body, "projectId");
2215
+ if (cwd === void 0 && outsideProject === void 0 && workspaceId === void 0 && !projectId?.trim())
2216
+ throw new ProtocolValidationError("projectId cannot be empty", "projectId");
2217
+ const target = workspaceId !== void 0 ? { workspaceId } : outsideProject === true ? { outsideProject } : cwd !== void 0 ? { cwd, ...projectId === void 0 ? {} : { projectId } } : { projectId };
2209
2218
  const result = { id: id2, requestId, ...target };
2210
- for (const key of ["projectId", "title", "optionsRevision"]) if (body[key] !== void 0) result[key] = optionalString2(body, key);
2219
+ for (const key of ["title", "optionsRevision"]) if (body[key] !== void 0) result[key] = optionalString2(body, key);
2211
2220
  if (body.storage !== void 0) result.storage = optionalEnum2(body, "storage", ["sqlite", "memory"]);
2212
2221
  if (body.configuration !== void 0) result.configuration = parseSessionConfiguration2(body.configuration);
2213
2222
  if (body.metadata !== void 0) result.metadata = appMetadata(optionalRecord(body, "metadata"));
@@ -3915,13 +3924,13 @@ function withoutCursor(url) {
3915
3924
  }
3916
3925
 
3917
3926
  // src/resources/session/session-request-id.ts
3918
- var promptRequestCounter = 0;
3919
- function createPromptRequestId() {
3927
+ var sessionRequestCounter = 0;
3928
+ function createSessionRequestId() {
3920
3929
  if (typeof globalThis.crypto?.randomUUID === "function") {
3921
3930
  return globalThis.crypto.randomUUID();
3922
3931
  }
3923
- promptRequestCounter += 1;
3924
- return `prompt-${Date.now().toString(36)}-${promptRequestCounter.toString(36)}`;
3932
+ sessionRequestCounter += 1;
3933
+ return `prompt-${Date.now().toString(36)}-${sessionRequestCounter.toString(36)}`;
3925
3934
  }
3926
3935
 
3927
3936
  // src/resources/session/session-configuration-requests.ts
@@ -3943,18 +3952,25 @@ async function getDraftComposerOptions(transport, input, options = {}) {
3943
3952
  }
3944
3953
  return result;
3945
3954
  }
3946
- async function create(transport, input, options = {}) {
3955
+ async function create(transport, input, options = {}, prepareModel) {
3947
3956
  const body = parseCreateSessionRequest({
3948
3957
  ...input,
3949
- id: input.id ?? createPromptRequestId(),
3950
- requestId: input.requestId ?? createPromptRequestId()
3958
+ id: input.id ?? createSessionRequestId(),
3959
+ requestId: input.requestId ?? createSessionRequestId()
3951
3960
  });
3952
- if (body.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3961
+ if (body.workspaceId !== void 0 || body.outsideProject)
3962
+ await requireDaemonFeature(transport, "sessionDirectoryCreation", options.signal);
3963
+ else if (body.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3964
+ const modelConnection = await prepareModel?.(body.id, body.configuration?.model, options.signal);
3953
3965
  const response = await transport.request(
3954
3966
  "/sessions",
3955
3967
  {
3956
3968
  method: "POST",
3957
3969
  body,
3970
+ headers: {
3971
+ ...body.workspaceId === void 0 ? {} : { "x-bobannomi-workspace-id": body.workspaceId },
3972
+ ...modelConnection === void 0 ? {} : { "x-bobannomi-model-connection": modelConnection }
3973
+ },
3958
3974
  signal: options.signal
3959
3975
  }
3960
3976
  );
@@ -3968,7 +3984,9 @@ async function getConfigurationOptions(transport, input, options = {}) {
3968
3984
  );
3969
3985
  }
3970
3986
  async function getCreationResult(transport, input, options = {}) {
3971
- if (input.original.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3987
+ if (input.original.workspaceId !== void 0 || input.original.outsideProject)
3988
+ await requireDaemonFeature(transport, "sessionDirectoryCreation", options.signal);
3989
+ else if (input.original.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3972
3990
  return transport.request("/sessions/creation-result", {
3973
3991
  method: "POST",
3974
3992
  body: input,
@@ -3982,11 +4000,17 @@ async function getConfiguration(transport, sessionId, options = {}) {
3982
4000
  );
3983
4001
  return decodeSessionConfigurationSnapshot(value);
3984
4002
  }
3985
- async function configure(transport, sessionId, input, options = {}) {
4003
+ async function configure(transport, sessionId, input, options = {}, prepareModel) {
3986
4004
  const body = parseConfigureSessionRequest(input);
4005
+ const modelConnection = await prepareModel?.(sessionId, body.configuration.model, options.signal);
3987
4006
  const response = await transport.requestUnknown(
3988
4007
  `/sessions/${encodeURIComponent(sessionId)}/configure`,
3989
- { method: "POST", body, signal: options.signal }
4008
+ {
4009
+ method: "POST",
4010
+ body,
4011
+ signal: options.signal,
4012
+ ...modelConnection === void 0 ? {} : { headers: { "x-bobannomi-model-connection": modelConnection } }
4013
+ }
3990
4014
  );
3991
4015
  return decodeSessionConfigurationReceipt(response, {
3992
4016
  sessionId,
@@ -4099,7 +4123,7 @@ function selectSessionMessagesWithParts(bucket) {
4099
4123
  parts: [...bucket.partsByMessageId[message.id] ?? []].sort((a, b) => a.seq - b.seq)
4100
4124
  }));
4101
4125
  }
4102
- function selectVisibleSessionMessagesWithParts(bucket) {
4126
+ function getVisibleSessionMessages(bucket) {
4103
4127
  return selectSessionMessagesWithParts(bucket).map(({ message, parts }) => ({
4104
4128
  message,
4105
4129
  parts: parts.filter((part) => !isSupersededModelPart(part))
@@ -4124,7 +4148,7 @@ var UnsupportedSessionEventSchemaVersionError = class extends Error {
4124
4148
  };
4125
4149
  }
4126
4150
  };
4127
- function createInitialClientState() {
4151
+ function createClientState() {
4128
4152
  return {
4129
4153
  sessions: {},
4130
4154
  sessionOrder: [],
@@ -4135,7 +4159,7 @@ function createInitialClientState() {
4135
4159
  lastSeq: 0
4136
4160
  };
4137
4161
  }
4138
- function applySessionSnapshot(state, snapshot) {
4162
+ function replaceSessionState(state, snapshot) {
4139
4163
  const knownCursor = state.snapshotCursorBySession[snapshot.session.id] ?? 0;
4140
4164
  if (snapshot.cursor < knownCursor) return state;
4141
4165
  if (Object.values(state.eventsBySeq).some((event) => event.sessionId === snapshot.session.id && event.seq > snapshot.cursor)) return state;
@@ -4493,7 +4517,7 @@ function applySessionStreamEvent(state, event) {
4493
4517
  }
4494
4518
  function applySessionStreamSnapshot(state, snapshot) {
4495
4519
  if (snapshot.cursor < state.lastSeq) return state;
4496
- return { ...applySessionSnapshot(state, snapshot), eventsBySeq: {} };
4520
+ return { ...replaceSessionState(state, snapshot), eventsBySeq: {} };
4497
4521
  }
4498
4522
 
4499
4523
  // src/state/sync.ts
@@ -4508,7 +4532,7 @@ async function* syncEvents(client, options = {}) {
4508
4532
  idleTimeoutMs: options.idleTimeoutMs ?? DEFAULT_SESSION_IDLE_TIMEOUT_MS
4509
4533
  });
4510
4534
  }
4511
- async function* runEventSync(client, options, initialState = createInitialClientState(), onLiveEvent) {
4535
+ async function* runEventSync(client, options, initialState = createClientState(), onLiveEvent) {
4512
4536
  let state = initialState;
4513
4537
  let cursor = Math.max(0, options.cursor ?? 0, state.lastSeq);
4514
4538
  let attempt = 0;
@@ -4523,7 +4547,7 @@ async function* runEventSync(client, options, initialState = createInitialClient
4523
4547
  partView: options.partView
4524
4548
  });
4525
4549
  if (signal?.aborted) return;
4526
- const next = initial ? applySessionSnapshot(state, snapshot) : applySessionStreamSnapshot(state, snapshot);
4550
+ const next = initial ? replaceSessionState(state, snapshot) : applySessionStreamSnapshot(state, snapshot);
4527
4551
  state = Object.keys(next.eventsBySeq).length ? { ...next, eventsBySeq: {} } : next;
4528
4552
  cursor = Math.max(cursor, snapshot.cursor, state.lastSeq);
4529
4553
  };
@@ -4633,7 +4657,7 @@ async function waitForRun(client, options) {
4633
4657
  if (!run || run.status === "pending" || run.status === "running") continue;
4634
4658
  return {
4635
4659
  run,
4636
- messages: selectVisibleSessionMessagesWithParts(bucket).filter((item) => item.message.runId === options.runId)
4660
+ messages: getVisibleSessionMessages(bucket).filter((item) => item.message.runId === options.runId)
4637
4661
  };
4638
4662
  }
4639
4663
  options.signal?.throwIfAborted();
@@ -4827,7 +4851,7 @@ async function admitPrompt(transport, sessionId, input, options = {}) {
4827
4851
  `/sessions/${encodeURIComponent(sessionId)}/prompts`,
4828
4852
  {
4829
4853
  method: "POST",
4830
- body: { ...input, expectedModelConnection, id: input.id ?? createPromptRequestId() },
4854
+ body: { ...input, expectedModelConnection, id: input.id ?? createSessionRequestId() },
4831
4855
  signal: options.signal
4832
4856
  }
4833
4857
  );
@@ -4879,7 +4903,7 @@ async function resumeInterruptedRun(transport, sessionId, runId, input = {}, opt
4879
4903
  `/sessions/${encodeURIComponent(sessionId)}/runs/${encodeURIComponent(runId)}/resume`,
4880
4904
  {
4881
4905
  method: "POST",
4882
- body: { ...input, id: input.id ?? createPromptRequestId() },
4906
+ body: { ...input, id: input.id ?? createSessionRequestId() },
4883
4907
  signal: options.signal
4884
4908
  }
4885
4909
  );
@@ -4896,7 +4920,7 @@ async function interrupt(transport, sessionId, options = {}) {
4896
4920
  }
4897
4921
 
4898
4922
  // src/resources/session/session-handle.ts
4899
- function createSessionHandle(transport, syncClient, management, sessionId) {
4923
+ function createSessionHandle(transport, syncClient, management, sessionId, prepareModel) {
4900
4924
  function cancelQueuedPrompt2(inputId, input, options) {
4901
4925
  return cancelQueuedPrompt(transport, sessionId, inputId, input, options);
4902
4926
  }
@@ -5011,8 +5035,8 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
5011
5035
  method: "POST",
5012
5036
  body: parseForkSessionRequest({
5013
5037
  ...input,
5014
- id: input.id ?? createPromptRequestId(),
5015
- requestId: input.requestId ?? createPromptRequestId()
5038
+ id: input.id ?? createSessionRequestId(),
5039
+ requestId: input.requestId ?? createSessionRequestId()
5016
5040
  }),
5017
5041
  signal: options.signal
5018
5042
  }
@@ -5057,7 +5081,7 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
5057
5081
  return getConfiguration(transport, sessionId, options);
5058
5082
  },
5059
5083
  update(input, options = {}) {
5060
- return configure(transport, sessionId, input, options);
5084
+ return configure(transport, sessionId, input, options, prepareModel);
5061
5085
  },
5062
5086
  getResult(input, options = {}) {
5063
5087
  return getConfigurationResult(transport, sessionId, input, options);
@@ -5164,17 +5188,19 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
5164
5188
 
5165
5189
  // src/resources/session/session-resource.ts
5166
5190
  var SessionResource = class {
5167
- #management;
5168
- #events;
5169
- #transport;
5170
- constructor(transport, events) {
5191
+ constructor(transport, events, prepareModel) {
5192
+ this.prepareModel = prepareModel;
5171
5193
  this.#transport = transport;
5172
5194
  this.#management = new SessionManagementResource(transport);
5173
5195
  this.#events = events ?? new EventResource(transport, new SseTransport(transport));
5174
5196
  }
5197
+ prepareModel;
5198
+ #management;
5199
+ #events;
5200
+ #transport;
5175
5201
  /** Bind a session ID locally. Operations use this resource's daemon connection when called. */
5176
5202
  byId(sessionId) {
5177
- return createSessionHandle(this.#transport, { sessions: this, events: this.#events }, this.#management, sessionId);
5203
+ return createSessionHandle(this.#transport, { sessions: this, events: this.#events }, this.#management, sessionId, this.prepareModel);
5178
5204
  }
5179
5205
  /** `GET /sessions/search` - search saved conversation text without opening it. */
5180
5206
  async search(options) {
@@ -5205,7 +5231,7 @@ var SessionResource = class {
5205
5231
  }
5206
5232
  /** `POST /sessions` */
5207
5233
  create(input, options = {}) {
5208
- return create(this.#transport, input, options);
5234
+ return create(this.#transport, input, options, this.prepareModel);
5209
5235
  }
5210
5236
  getConfigurationOptions(input, options = {}) {
5211
5237
  return getConfigurationOptions(this.#transport, input, options);
@@ -5328,7 +5354,54 @@ var PermissionResource = class {
5328
5354
 
5329
5355
  // src/resources/runtime/runtime-resource.ts
5330
5356
  function createRuntimeResources(transport, providers) {
5331
- const catalog = typeof providers === "function" || providers === void 0 ? providers : structuredClone([...providers]);
5357
+ const catalog = typeof providers === "function" || providers === void 0 ? providers : providers.map(({ connection, ...provider2 }) => ({
5358
+ ...structuredClone(provider2),
5359
+ connection: typeof connection === "function" ? connection : structuredClone(connection)
5360
+ }));
5361
+ async function load(source, signal) {
5362
+ signal?.throwIfAborted();
5363
+ const value = await waitForRequest(Promise.resolve().then(() => {
5364
+ signal?.throwIfAborted();
5365
+ return source({ signal });
5366
+ }), signal);
5367
+ signal?.throwIfAborted();
5368
+ return value;
5369
+ }
5370
+ function readCatalog(signal) {
5371
+ signal?.throwIfAborted();
5372
+ return typeof catalog === "function" ? load(catalog, signal) : Promise.resolve(catalog ?? []);
5373
+ }
5374
+ const prepareModel = catalog === void 0 ? void 0 : async (sessionId, selection, signal) => {
5375
+ if (!selection) return;
5376
+ const provider2 = (await readCatalog(signal)).find(({ name }) => name === selection.provider);
5377
+ if (!provider2?.connection) return;
5378
+ if (!provider2.models.some(({ id: id2 }) => id2 === selection.model)) throw new Error("The selected model is not in the supplied provider catalog");
5379
+ await requireDaemonFeature(transport, "sessionModelConnections", signal);
5380
+ const connection = typeof provider2.connection === "function" ? await load(provider2.connection, signal) : provider2.connection;
5381
+ const { ref } = await transport.request("/model-connections", {
5382
+ method: "POST",
5383
+ signal,
5384
+ body: {
5385
+ sessionId,
5386
+ provider: {
5387
+ id: provider2.name,
5388
+ displayName: provider2.displayName,
5389
+ baseUrl: connection.baseUrl,
5390
+ apiKey: connection.apiKey,
5391
+ apiFormat: connection.apiFormat,
5392
+ models: provider2.models.map(({ id: id2, label, contextWindow, outputLimit, inputCapabilities }) => ({
5393
+ id: id2,
5394
+ displayName: label,
5395
+ contextWindow,
5396
+ maxOutputTokens: outputLimit,
5397
+ imageInputSupport: inputCapabilities?.image
5398
+ }))
5399
+ }
5400
+ }
5401
+ });
5402
+ if (typeof ref !== "string" || !ref) throw new Error("Invalid prepared model connection");
5403
+ return ref;
5404
+ };
5332
5405
  async function modelProviders(input, options = {}) {
5333
5406
  const response = await transport.request("/providers/manage", { ...options, method: "POST", body: input });
5334
5407
  if (!validModelProviderConnections(response?.providers)) throw new Error("Invalid model provider response");
@@ -5341,19 +5414,14 @@ function createRuntimeResources(transport, providers) {
5341
5414
  return result;
5342
5415
  }
5343
5416
  return {
5417
+ prepareModel,
5344
5418
  models: {
5345
5419
  async list({ signal } = {}) {
5346
5420
  if (catalog === void 0) {
5347
5421
  const response = await transport.request("/models", { signal });
5348
5422
  return response.providers;
5349
5423
  }
5350
- signal?.throwIfAborted();
5351
- const providers2 = typeof catalog === "function" ? await waitForRequest(Promise.resolve().then(() => {
5352
- signal?.throwIfAborted();
5353
- return catalog({ signal });
5354
- }), signal) : catalog;
5355
- signal?.throwIfAborted();
5356
- return structuredClone([...providers2]);
5424
+ return (await readCatalog(signal)).map(({ name, displayName, models }) => structuredClone({ name, displayName, models }));
5357
5425
  }
5358
5426
  },
5359
5427
  agents: {
@@ -6256,7 +6324,7 @@ var BobanNomiClient = class {
6256
6324
  const sse = new SseTransport(transport);
6257
6325
  this.protocol = new ProtocolClient(transport);
6258
6326
  const providers = options instanceof HttpTransport ? void 0 : options.providers;
6259
- const { models, modelProviders, agents, skills } = createRuntimeResources(transport, providers);
6327
+ const { models, modelProviders, agents, skills, prepareModel } = createRuntimeResources(transport, providers);
6260
6328
  this.models = models;
6261
6329
  this.modelProviders = modelProviders;
6262
6330
  this.agents = agents;
@@ -6265,7 +6333,7 @@ var BobanNomiClient = class {
6265
6333
  this.attachments = new AttachmentResource(transport);
6266
6334
  this.permissions = new PermissionResource(transport);
6267
6335
  this.events = new EventResource(transport, sse);
6268
- this.sessions = new SessionResource(transport, this.events);
6336
+ this.sessions = new SessionResource(transport, this.events, prepareModel);
6269
6337
  this.plugins = new PluginResource(transport);
6270
6338
  this.channels = new ChannelResource(transport);
6271
6339
  this.projects = new ProjectResource(transport);