@bobannomi/client 0.3.0 → 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.
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"));
@@ -3515,6 +3524,12 @@ var BobanNomiApiError = class extends Error {
3515
3524
  this.status = status;
3516
3525
  this.body = body;
3517
3526
  this.name = "BobanNomiApiError";
3527
+ if (!body || typeof body !== "object" || Array.isArray(body)) return;
3528
+ const { code, error } = body;
3529
+ const machineCode = [code, error].find(
3530
+ (value) => typeof value === "string" && /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/.test(value)
3531
+ );
3532
+ if (machineCode !== void 0) this.code = machineCode;
3518
3533
  }
3519
3534
  status;
3520
3535
  body;
@@ -3943,18 +3958,25 @@ async function getDraftComposerOptions(transport, input, options = {}) {
3943
3958
  }
3944
3959
  return result;
3945
3960
  }
3946
- async function create(transport, input, options = {}) {
3961
+ async function create(transport, input, options = {}, prepareModel) {
3947
3962
  const body = parseCreateSessionRequest({
3948
3963
  ...input,
3949
3964
  id: input.id ?? createSessionRequestId(),
3950
3965
  requestId: input.requestId ?? createSessionRequestId()
3951
3966
  });
3952
- if (body.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3967
+ if (body.workspaceId !== void 0 || body.outsideProject)
3968
+ await requireDaemonFeature(transport, "sessionDirectoryCreation", options.signal);
3969
+ else if (body.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3970
+ const modelConnection = await prepareModel?.(body.id, body.configuration?.model, options.signal);
3953
3971
  const response = await transport.request(
3954
3972
  "/sessions",
3955
3973
  {
3956
3974
  method: "POST",
3957
3975
  body,
3976
+ headers: {
3977
+ ...body.workspaceId === void 0 ? {} : { "x-bobannomi-workspace-id": body.workspaceId },
3978
+ ...modelConnection === void 0 ? {} : { "x-bobannomi-model-connection": modelConnection }
3979
+ },
3958
3980
  signal: options.signal
3959
3981
  }
3960
3982
  );
@@ -3968,7 +3990,9 @@ async function getConfigurationOptions(transport, input, options = {}) {
3968
3990
  );
3969
3991
  }
3970
3992
  async function getCreationResult(transport, input, options = {}) {
3971
- if (input.original.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3993
+ if (input.original.workspaceId !== void 0 || input.original.outsideProject)
3994
+ await requireDaemonFeature(transport, "sessionDirectoryCreation", options.signal);
3995
+ else if (input.original.cwd === void 0) await requireDaemonFeature(transport, "projectSessionCreation", options.signal);
3972
3996
  return transport.request("/sessions/creation-result", {
3973
3997
  method: "POST",
3974
3998
  body: input,
@@ -3982,11 +4006,17 @@ async function getConfiguration(transport, sessionId, options = {}) {
3982
4006
  );
3983
4007
  return decodeSessionConfigurationSnapshot(value);
3984
4008
  }
3985
- async function configure(transport, sessionId, input, options = {}) {
4009
+ async function configure(transport, sessionId, input, options = {}, prepareModel) {
3986
4010
  const body = parseConfigureSessionRequest(input);
4011
+ const modelConnection = await prepareModel?.(sessionId, body.configuration.model, options.signal);
3987
4012
  const response = await transport.requestUnknown(
3988
4013
  `/sessions/${encodeURIComponent(sessionId)}/configure`,
3989
- { method: "POST", body, signal: options.signal }
4014
+ {
4015
+ method: "POST",
4016
+ body,
4017
+ signal: options.signal,
4018
+ ...modelConnection === void 0 ? {} : { headers: { "x-bobannomi-model-connection": modelConnection } }
4019
+ }
3990
4020
  );
3991
4021
  return decodeSessionConfigurationReceipt(response, {
3992
4022
  sessionId,
@@ -4107,6 +4137,18 @@ function getVisibleSessionMessages(bucket) {
4107
4137
  ({ message, parts }) => parts.length > 0 || message.role !== "assistant"
4108
4138
  );
4109
4139
  }
4140
+ function selectSessionInputs(bucket) {
4141
+ if (!bucket) return [];
4142
+ return [...bucket.inputs].sort((a, b) => a.seq - b.seq);
4143
+ }
4144
+ function selectSessionRuns(bucket) {
4145
+ if (!bucket) return [];
4146
+ return Object.values(bucket.runs);
4147
+ }
4148
+ function selectSessionPermissions(bucket) {
4149
+ if (!bucket) return [];
4150
+ return Object.values(bucket.permissions);
4151
+ }
4110
4152
 
4111
4153
  // src/state/reducer.ts
4112
4154
  var UnsupportedSessionEventSchemaVersionError = class extends Error {
@@ -4592,6 +4634,7 @@ async function* runEventSync(client, options, initialState = createClientState()
4592
4634
  if (error instanceof UnsupportedSessionEventSchemaVersionError) throw error;
4593
4635
  if (isAbortError(error) || signal?.aborted) return;
4594
4636
  if (options.shouldRetry?.(error) === false) throw error;
4637
+ if (error && typeof error === "object" && "code" in error && error.code === "connection_revoked") throw error;
4595
4638
  if (error instanceof BobanNomiApiError && error.status >= 400 && error.status < 500 && ![408, 425, 429].includes(error.status)) throw error;
4596
4639
  }
4597
4640
  if (signal?.aborted) return;
@@ -4623,6 +4666,20 @@ function isAbortError(error) {
4623
4666
  return Boolean(error && typeof error === "object" && "name" in error && error.name === "AbortError");
4624
4667
  }
4625
4668
 
4669
+ // src/state/session-observation.ts
4670
+ async function* observeSession(client, options) {
4671
+ for await (const { source, state } of syncEvents(client, options)) {
4672
+ const bucket = state.buckets[options.sessionId];
4673
+ yield {
4674
+ source,
4675
+ messages: getVisibleSessionMessages(bucket),
4676
+ inputs: selectSessionInputs(bucket),
4677
+ runs: selectSessionRuns(bucket),
4678
+ permissions: selectSessionPermissions(bucket)
4679
+ };
4680
+ }
4681
+ }
4682
+
4626
4683
  // src/state/wait-for-run.ts
4627
4684
  async function waitForRun(client, options) {
4628
4685
  options.signal?.throwIfAborted();
@@ -4896,7 +4953,7 @@ async function interrupt(transport, sessionId, options = {}) {
4896
4953
  }
4897
4954
 
4898
4955
  // src/resources/session/session-handle.ts
4899
- function createSessionHandle(transport, syncClient, management, sessionId) {
4956
+ function createSessionHandle(transport, syncClient, management, sessionId, prepareModel) {
4900
4957
  function cancelQueuedPrompt2(inputId, input, options) {
4901
4958
  return cancelQueuedPrompt(transport, sessionId, inputId, input, options);
4902
4959
  }
@@ -4908,6 +4965,7 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
4908
4965
  }
4909
4966
  return {
4910
4967
  id: sessionId,
4968
+ observe: (options) => observeSession(syncClient, { ...options, sessionId }),
4911
4969
  waitForRun: (runId, options) => waitForRun(syncClient, { sessionId, runId, signal: options?.signal }),
4912
4970
  async updateAppMetadata(metadata, options) {
4913
4971
  const body = parseUpdateSessionRequest({ metadata: { app: metadata } });
@@ -5057,7 +5115,7 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
5057
5115
  return getConfiguration(transport, sessionId, options);
5058
5116
  },
5059
5117
  update(input, options = {}) {
5060
- return configure(transport, sessionId, input, options);
5118
+ return configure(transport, sessionId, input, options, prepareModel);
5061
5119
  },
5062
5120
  getResult(input, options = {}) {
5063
5121
  return getConfigurationResult(transport, sessionId, input, options);
@@ -5164,17 +5222,20 @@ function createSessionHandle(transport, syncClient, management, sessionId) {
5164
5222
 
5165
5223
  // src/resources/session/session-resource.ts
5166
5224
  var SessionResource = class {
5167
- #management;
5168
- #events;
5169
- #transport;
5170
- constructor(transport, events) {
5225
+ constructor(transport, events, prepareModel) {
5226
+ this.prepareModel = prepareModel;
5171
5227
  this.#transport = transport;
5172
5228
  this.#management = new SessionManagementResource(transport);
5173
5229
  this.#events = events ?? new EventResource(transport, new SseTransport(transport));
5174
5230
  }
5231
+ prepareModel;
5232
+ #management;
5233
+ #events;
5234
+ #transport;
5175
5235
  /** Bind a session ID locally. Operations use this resource's daemon connection when called. */
5176
5236
  byId(sessionId) {
5177
- return createSessionHandle(this.#transport, { sessions: this, events: this.#events }, this.#management, sessionId);
5237
+ if (typeof sessionId !== "string" || sessionId.length === 0) throw new TypeError("sessionId is required");
5238
+ return createSessionHandle(this.#transport, { sessions: this, events: this.#events }, this.#management, sessionId, this.prepareModel);
5178
5239
  }
5179
5240
  /** `GET /sessions/search` - search saved conversation text without opening it. */
5180
5241
  async search(options) {
@@ -5205,7 +5266,7 @@ var SessionResource = class {
5205
5266
  }
5206
5267
  /** `POST /sessions` */
5207
5268
  create(input, options = {}) {
5208
- return create(this.#transport, input, options);
5269
+ return create(this.#transport, input, options, this.prepareModel);
5209
5270
  }
5210
5271
  getConfigurationOptions(input, options = {}) {
5211
5272
  return getConfigurationOptions(this.#transport, input, options);
@@ -5328,7 +5389,54 @@ var PermissionResource = class {
5328
5389
 
5329
5390
  // src/resources/runtime/runtime-resource.ts
5330
5391
  function createRuntimeResources(transport, providers) {
5331
- const catalog = typeof providers === "function" || providers === void 0 ? providers : structuredClone([...providers]);
5392
+ const catalog = typeof providers === "function" || providers === void 0 ? providers : providers.map(({ connection, ...provider2 }) => ({
5393
+ ...structuredClone(provider2),
5394
+ connection: typeof connection === "function" ? connection : structuredClone(connection)
5395
+ }));
5396
+ async function load(source, signal) {
5397
+ signal?.throwIfAborted();
5398
+ const value = await waitForRequest(Promise.resolve().then(() => {
5399
+ signal?.throwIfAborted();
5400
+ return source({ signal });
5401
+ }), signal);
5402
+ signal?.throwIfAborted();
5403
+ return value;
5404
+ }
5405
+ function readCatalog(signal) {
5406
+ signal?.throwIfAborted();
5407
+ return typeof catalog === "function" ? load(catalog, signal) : Promise.resolve(catalog ?? []);
5408
+ }
5409
+ const prepareModel = catalog === void 0 ? void 0 : async (sessionId, selection, signal) => {
5410
+ if (!selection) return;
5411
+ const provider2 = (await readCatalog(signal)).find(({ name }) => name === selection.provider);
5412
+ if (!provider2?.connection) return;
5413
+ if (!provider2.models.some(({ id: id2 }) => id2 === selection.model)) throw new Error("The selected model is not in the supplied provider catalog");
5414
+ await requireDaemonFeature(transport, "sessionModelConnections", signal);
5415
+ const connection = typeof provider2.connection === "function" ? await load(provider2.connection, signal) : provider2.connection;
5416
+ const { ref } = await transport.request("/model-connections", {
5417
+ method: "POST",
5418
+ signal,
5419
+ body: {
5420
+ sessionId,
5421
+ provider: {
5422
+ id: provider2.name,
5423
+ displayName: provider2.displayName,
5424
+ baseUrl: connection.baseUrl,
5425
+ apiKey: connection.apiKey,
5426
+ apiFormat: connection.apiFormat,
5427
+ models: provider2.models.map(({ id: id2, label, contextWindow, outputLimit, inputCapabilities }) => ({
5428
+ id: id2,
5429
+ displayName: label,
5430
+ contextWindow,
5431
+ maxOutputTokens: outputLimit,
5432
+ imageInputSupport: inputCapabilities?.image
5433
+ }))
5434
+ }
5435
+ }
5436
+ });
5437
+ if (typeof ref !== "string" || !ref) throw new Error("Invalid prepared model connection");
5438
+ return ref;
5439
+ };
5332
5440
  async function modelProviders(input, options = {}) {
5333
5441
  const response = await transport.request("/providers/manage", { ...options, method: "POST", body: input });
5334
5442
  if (!validModelProviderConnections(response?.providers)) throw new Error("Invalid model provider response");
@@ -5341,19 +5449,14 @@ function createRuntimeResources(transport, providers) {
5341
5449
  return result;
5342
5450
  }
5343
5451
  return {
5452
+ prepareModel,
5344
5453
  models: {
5345
5454
  async list({ signal } = {}) {
5346
5455
  if (catalog === void 0) {
5347
5456
  const response = await transport.request("/models", { signal });
5348
5457
  return response.providers;
5349
5458
  }
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]);
5459
+ return (await readCatalog(signal)).map(({ name, displayName, models }) => structuredClone({ name, displayName, models }));
5357
5460
  }
5358
5461
  },
5359
5462
  agents: {
@@ -6256,7 +6359,7 @@ var BobanNomiClient = class {
6256
6359
  const sse = new SseTransport(transport);
6257
6360
  this.protocol = new ProtocolClient(transport);
6258
6361
  const providers = options instanceof HttpTransport ? void 0 : options.providers;
6259
- const { models, modelProviders, agents, skills } = createRuntimeResources(transport, providers);
6362
+ const { models, modelProviders, agents, skills, prepareModel } = createRuntimeResources(transport, providers);
6260
6363
  this.models = models;
6261
6364
  this.modelProviders = modelProviders;
6262
6365
  this.agents = agents;
@@ -6265,7 +6368,7 @@ var BobanNomiClient = class {
6265
6368
  this.attachments = new AttachmentResource(transport);
6266
6369
  this.permissions = new PermissionResource(transport);
6267
6370
  this.events = new EventResource(transport, sse);
6268
- this.sessions = new SessionResource(transport, this.events);
6371
+ this.sessions = new SessionResource(transport, this.events, prepareModel);
6269
6372
  this.plugins = new PluginResource(transport);
6270
6373
  this.channels = new ChannelResource(transport);
6271
6374
  this.projects = new ProjectResource(transport);