@liveblocks/core 2.25.0-aiprivatebeta7 → 2.25.0-aiprivatebeta8

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/index.cjs CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "2.25.0-aiprivatebeta7";
9
+ var PKG_VERSION = "2.25.0-aiprivatebeta8";
10
10
  var PKG_FORMAT = "cjs";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -188,6 +188,9 @@ var warnWithTitle = wrapWithTitle("warn");
188
188
  var errorWithTitle = wrapWithTitle("error");
189
189
 
190
190
  // src/lib/guards.ts
191
+ function isDefined(value) {
192
+ return value !== null && value !== void 0;
193
+ }
191
194
  function isPlainObject(blob) {
192
195
  return blob !== null && typeof blob === "object" && Object.prototype.toString.call(blob) === "[object Object]";
193
196
  }
@@ -3685,11 +3688,6 @@ function parseAuthToken(rawTokenString) {
3685
3688
  function appendDelta(content, delta) {
3686
3689
  const lastPart = content[content.length - 1];
3687
3690
  switch (delta.type) {
3688
- case "reasoning":
3689
- case "text":
3690
- case "tool-call":
3691
- content.push(delta);
3692
- break;
3693
3691
  case "text-delta":
3694
3692
  if (_optionalChain([lastPart, 'optionalAccess', _55 => _55.type]) === "text") {
3695
3693
  lastPart.text += delta.textDelta;
@@ -3700,15 +3698,16 @@ function appendDelta(content, delta) {
3700
3698
  case "reasoning-delta":
3701
3699
  if (_optionalChain([lastPart, 'optionalAccess', _56 => _56.type]) === "reasoning") {
3702
3700
  lastPart.text += delta.textDelta;
3703
- lastPart.signature ??= delta.signature;
3704
3701
  } else {
3705
3702
  content.push({
3706
3703
  type: "reasoning",
3707
- text: _nullishCoalesce(delta.textDelta, () => ( "")),
3708
- signature: delta.signature
3704
+ text: _nullishCoalesce(delta.textDelta, () => ( ""))
3709
3705
  });
3710
3706
  }
3711
3707
  break;
3708
+ case "tool-invocation":
3709
+ content.push(delta);
3710
+ break;
3712
3711
  default:
3713
3712
  return assertNever(delta, "Unhandled case");
3714
3713
  }
@@ -3716,7 +3715,63 @@ function appendDelta(content, delta) {
3716
3715
 
3717
3716
  // src/ai.ts
3718
3717
  var DEFAULT_REQUEST_TIMEOUT = 4e3;
3719
- var DEFAULT_AI_TIMEOUT = 3e4;
3718
+ var KnowledgeStack = class {
3719
+ #_layers;
3720
+ #stack;
3721
+ // / \
3722
+ // knowledge key "layer" key
3723
+ // (random, or optionally (one entry per mounted component)
3724
+ // set by user)
3725
+ #_cache;
3726
+ constructor() {
3727
+ this.#_layers = /* @__PURE__ */ new Set();
3728
+ this.#stack = new DefaultMap(
3729
+ () => /* @__PURE__ */ new Map()
3730
+ );
3731
+ this.#_cache = void 0;
3732
+ }
3733
+ // Typically a useId()
3734
+ registerLayer(uniqueLayerId) {
3735
+ const layerKey = uniqueLayerId;
3736
+ if (this.#_layers.has(layerKey))
3737
+ raise(`Layer '${layerKey}' already exists, provide a unique layer id`);
3738
+ this.#_layers.add(layerKey);
3739
+ return layerKey;
3740
+ }
3741
+ deregisterLayer(layerKey) {
3742
+ this.#_layers.delete(layerKey);
3743
+ let deleted = false;
3744
+ for (const [key, knowledge] of this.#stack) {
3745
+ if (knowledge.delete(layerKey)) {
3746
+ deleted = true;
3747
+ }
3748
+ if (knowledge.size === 0)
3749
+ this.#stack.delete(key);
3750
+ }
3751
+ if (deleted) {
3752
+ this.invalidate();
3753
+ }
3754
+ }
3755
+ get() {
3756
+ return this.#_cache ??= this.#recompute();
3757
+ }
3758
+ invalidate() {
3759
+ this.#_cache = void 0;
3760
+ }
3761
+ #recompute() {
3762
+ return Array.from(this.#stack.values()).flatMap(
3763
+ (layer) => (
3764
+ // Return only the last item (returns [] when empty)
3765
+ Array.from(layer.values()).slice(-1).filter(isDefined)
3766
+ )
3767
+ );
3768
+ }
3769
+ updateKnowledge(layerKey, key, data) {
3770
+ if (!this.#_layers.has(layerKey)) raise(`Unknown layer key: ${layerKey}`);
3771
+ this.#stack.getOrCreate(key).set(layerKey, data);
3772
+ this.invalidate();
3773
+ }
3774
+ };
3720
3775
  function now() {
3721
3776
  return (/* @__PURE__ */ new Date()).toISOString();
3722
3777
  }
@@ -3730,6 +3785,11 @@ function createStore_forTools() {
3730
3785
  return toolsByChatId\u03A3.getOrCreate(chatId).getOrCreate(toolName);
3731
3786
  }
3732
3787
  function addToolDefinition(chatId, name, definition) {
3788
+ if (!definition.execute && !definition.render) {
3789
+ throw new Error(
3790
+ "A tool definition must have an execute() function, a render property, or both."
3791
+ );
3792
+ }
3733
3793
  toolsByChatId\u03A3.getOrCreate(chatId).getOrCreate(name).set(definition);
3734
3794
  }
3735
3795
  function removeToolDefinition(chatId, toolName) {
@@ -3751,13 +3811,14 @@ function createStore_forTools() {
3751
3811
  }).filter((tool) => tool !== null);
3752
3812
  }
3753
3813
  return {
3754
- getToolCallByName\u03A3: getToolDefinition\u03A3,
3814
+ getToolDefinition\u03A3,
3755
3815
  getToolsForChat,
3756
3816
  addToolDefinition,
3757
3817
  removeToolDefinition
3758
3818
  };
3759
3819
  }
3760
- function createStore_forChatMessages() {
3820
+ function createStore_forChatMessages(toolsStore, setToolResult) {
3821
+ const seenToolCallIds = /* @__PURE__ */ new Set();
3761
3822
  const messagePoolByChatId\u03A3 = new DefaultMap(
3762
3823
  (_chatId) => new MutableSignal(
3763
3824
  new TreePool(
@@ -3767,7 +3828,7 @@ function createStore_forChatMessages() {
3767
3828
  )
3768
3829
  )
3769
3830
  );
3770
- const pendingMessages\u03A3 = new MutableSignal(
3831
+ const generatingMessages\u03A3 = new MutableSignal(
3771
3832
  /* @__PURE__ */ new Map()
3772
3833
  );
3773
3834
  function createOptimistically(chatId, role, parentId, third) {
@@ -3791,7 +3852,7 @@ function createStore_forChatMessages() {
3791
3852
  role,
3792
3853
  parentId,
3793
3854
  createdAt,
3794
- status: "pending",
3855
+ status: "generating",
3795
3856
  contentSoFar: [],
3796
3857
  _optimistic: true
3797
3858
  });
@@ -3810,7 +3871,7 @@ function createStore_forChatMessages() {
3810
3871
  if (!chatMsgs\u03A3) return;
3811
3872
  const existing = chatMsgs\u03A3.get().get(messageId);
3812
3873
  if (!existing || existing.deletedAt) return;
3813
- if (existing.role === "assistant" && (existing.status === "pending" || existing.status === "failed")) {
3874
+ if (existing.role === "assistant" && existing.status !== "completed") {
3814
3875
  upsert({ ...existing, deletedAt: now(), contentSoFar: [] });
3815
3876
  } else {
3816
3877
  upsert({ ...existing, deletedAt: now(), content: [] });
@@ -3825,19 +3886,54 @@ function createStore_forChatMessages() {
3825
3886
  batch(() => {
3826
3887
  const chatMsgs\u03A3 = messagePoolByChatId\u03A3.getOrCreate(message.chatId);
3827
3888
  chatMsgs\u03A3.mutate((pool) => pool.upsert(message));
3828
- if (message.role === "assistant" && message.status === "pending") {
3829
- pendingMessages\u03A3.mutate((lut) => {
3889
+ if (message.role === "assistant" && message.status === "generating") {
3890
+ generatingMessages\u03A3.mutate((lut) => {
3830
3891
  lut.set(message.id, structuredClone(message));
3831
3892
  });
3832
3893
  } else {
3833
- pendingMessages\u03A3.mutate((lut) => {
3894
+ generatingMessages\u03A3.mutate((lut) => {
3834
3895
  lut.delete(message.id);
3835
3896
  });
3836
3897
  }
3898
+ if (message.role === "assistant" && message.status === "awaiting-tool") {
3899
+ for (const toolCall of message.contentSoFar.filter(
3900
+ (part) => part.type === "tool-invocation" && part.status === "executing"
3901
+ )) {
3902
+ if (seenToolCallIds.has(toolCall.toolCallId)) {
3903
+ continue;
3904
+ }
3905
+ seenToolCallIds.add(toolCall.toolCallId);
3906
+ const toolDef = toolsStore.getToolDefinition\u03A3(message.chatId, toolCall.toolName).get();
3907
+ const respondSync = (result) => {
3908
+ setToolResult(
3909
+ message.chatId,
3910
+ message.id,
3911
+ toolCall.toolCallId,
3912
+ result
3913
+ // TODO Pass in AiGenerationOptions here, or make the backend use the same options
3914
+ ).catch((err) => {
3915
+ error2(
3916
+ `Error trying to respond to tool-call: ${String(err)} (in respond())`
3917
+ );
3918
+ });
3919
+ };
3920
+ const executeFn = _optionalChain([toolDef, 'optionalAccess', _57 => _57.execute]);
3921
+ if (executeFn) {
3922
+ (async () => {
3923
+ const result = await executeFn(toolCall.args);
3924
+ respondSync(result);
3925
+ })().catch((err) => {
3926
+ error2(
3927
+ `Error trying to respond to tool-call: ${String(err)} (in execute())`
3928
+ );
3929
+ });
3930
+ }
3931
+ }
3932
+ }
3837
3933
  });
3838
3934
  }
3839
3935
  function addDelta(messageId, delta) {
3840
- pendingMessages\u03A3.mutate((lut) => {
3936
+ generatingMessages\u03A3.mutate((lut) => {
3841
3937
  const message = lut.get(messageId);
3842
3938
  if (message === void 0) return false;
3843
3939
  appendDelta(message.contentSoFar, delta);
@@ -3845,10 +3941,10 @@ function createStore_forChatMessages() {
3845
3941
  return true;
3846
3942
  });
3847
3943
  }
3848
- function* iterPendingMessages() {
3944
+ function* iterGeneratingMessages() {
3849
3945
  for (const chatMsgs\u03A3 of messagePoolByChatId\u03A3.values()) {
3850
3946
  for (const m of chatMsgs\u03A3.get()) {
3851
- if (m.role === "assistant" && m.status === "pending" && !m._optimistic) {
3947
+ if (m.role === "assistant" && m.status === "generating" && !m._optimistic) {
3852
3948
  yield m;
3853
3949
  }
3854
3950
  }
@@ -3856,7 +3952,7 @@ function createStore_forChatMessages() {
3856
3952
  }
3857
3953
  function failAllPending() {
3858
3954
  batch(() => {
3859
- pendingMessages\u03A3.mutate((lut) => {
3955
+ generatingMessages\u03A3.mutate((lut) => {
3860
3956
  let deleted = false;
3861
3957
  for (const [k, v] of lut) {
3862
3958
  if (!v._optimistic) {
@@ -3867,7 +3963,7 @@ function createStore_forChatMessages() {
3867
3963
  return deleted;
3868
3964
  });
3869
3965
  upsertMany(
3870
- Array.from(iterPendingMessages()).map(
3966
+ Array.from(iterGeneratingMessages()).map(
3871
3967
  (message) => ({
3872
3968
  ...message,
3873
3969
  status: "failed",
@@ -3902,11 +3998,20 @@ function createStore_forChatMessages() {
3902
3998
  }
3903
3999
  function selectSpine(leaf) {
3904
4000
  const spine = [];
4001
+ let lastVisitedMessage = null;
3905
4002
  for (const message2 of pool.walkUp(leaf.id)) {
3906
- const prev = _nullishCoalesce(_optionalChain([first, 'call', _57 => _57(pool.walkLeft(message2.id, isAlive)), 'optionalAccess', _58 => _58.id]), () => ( null));
3907
- const next = _nullishCoalesce(_optionalChain([first, 'call', _59 => _59(pool.walkRight(message2.id, isAlive)), 'optionalAccess', _60 => _60.id]), () => ( null));
4003
+ const prev = _nullishCoalesce(_optionalChain([first, 'call', _58 => _58(pool.walkLeft(message2.id, isAlive)), 'optionalAccess', _59 => _59.id]), () => ( null));
4004
+ const next = _nullishCoalesce(_optionalChain([first, 'call', _60 => _60(pool.walkRight(message2.id, isAlive)), 'optionalAccess', _61 => _61.id]), () => ( null));
3908
4005
  if (!message2.deletedAt || prev || next) {
3909
- spine.push({ ...message2, prev, next });
4006
+ const node = {
4007
+ ...message2,
4008
+ navigation: { parent: null, prev, next }
4009
+ };
4010
+ if (lastVisitedMessage !== null) {
4011
+ lastVisitedMessage.navigation.parent = node.id;
4012
+ }
4013
+ lastVisitedMessage = node;
4014
+ spine.push(node);
3910
4015
  }
3911
4016
  }
3912
4017
  return spine.reverse();
@@ -3932,18 +4037,6 @@ function createStore_forChatMessages() {
3932
4037
  }
3933
4038
  return fallback();
3934
4039
  }
3935
- function getLatestUserMessageAncestor(chatId, messageId) {
3936
- const pool = messagePoolByChatId\u03A3.getOrCreate(chatId).get();
3937
- const message = pool.get(messageId);
3938
- if (!message) return null;
3939
- if (message.role === "user") return message.id;
3940
- for (const m of pool.walkUp(message.id)) {
3941
- if (m.role === "user" && !m.deletedAt) {
3942
- return m.id;
3943
- }
3944
- }
3945
- return null;
3946
- }
3947
4040
  const immutableMessagesByBranch = new DefaultMap((chatId) => {
3948
4041
  return new DefaultMap((branchId) => {
3949
4042
  const messages\u03A3 = DerivedSignal.from(() => {
@@ -3951,16 +4044,16 @@ function createStore_forChatMessages() {
3951
4044
  return selectBranch(pool, branchId);
3952
4045
  }, shallow2);
3953
4046
  return DerivedSignal.from(() => {
3954
- const pendingMessages = pendingMessages\u03A3.get();
4047
+ const generatingMessages = generatingMessages\u03A3.get();
3955
4048
  return messages\u03A3.get().map((message) => {
3956
- if (message.role !== "assistant" || message.status !== "pending") {
4049
+ if (message.role !== "assistant" || message.status !== "generating") {
3957
4050
  return message;
3958
4051
  }
3959
- const pendingMessage = pendingMessages.get(message.id);
3960
- if (pendingMessage === void 0) return message;
4052
+ const generatingMessage = generatingMessages.get(message.id);
4053
+ if (generatingMessage === void 0) return message;
3961
4054
  return {
3962
4055
  ...message,
3963
- contentSoFar: pendingMessage.contentSoFar
4056
+ contentSoFar: generatingMessage.contentSoFar
3964
4057
  };
3965
4058
  });
3966
4059
  }, shallow);
@@ -3969,21 +4062,10 @@ function createStore_forChatMessages() {
3969
4062
  function getChatMessagesForBranch\u03A3(chatId, branch) {
3970
4063
  return immutableMessagesByBranch.getOrCreate(chatId).getOrCreate(branch || null);
3971
4064
  }
3972
- const messagesByChatId\u03A3 = new DefaultMap((chatId) => {
3973
- return DerivedSignal.from(() => {
3974
- const pool = messagePoolByChatId\u03A3.getOrCreate(chatId).get();
3975
- return Array.from(pool.sorted);
3976
- });
3977
- });
3978
- function getMessagesForChat\u03A3(chatId) {
3979
- return messagesByChatId\u03A3.getOrCreate(chatId);
3980
- }
3981
4065
  return {
3982
4066
  // Readers
3983
4067
  getMessageById,
3984
4068
  getChatMessagesForBranch\u03A3,
3985
- getMessagesForChat\u03A3,
3986
- getLatestUserMessageAncestor,
3987
4069
  // Mutations
3988
4070
  createOptimistically,
3989
4071
  upsert,
@@ -4015,8 +4097,12 @@ function createStore_forUserAiChats() {
4015
4097
  function remove(chatId) {
4016
4098
  mutable\u03A3.mutate((list) => list.removeBy((c) => c.id === chatId, 1));
4017
4099
  }
4100
+ function getChatById(chatId) {
4101
+ return Array.from(mutable\u03A3.get()).find((chat) => chat.id === chatId);
4102
+ }
4018
4103
  return {
4019
4104
  chats\u03A3,
4105
+ getChatById,
4020
4106
  // Mutations
4021
4107
  upsert,
4022
4108
  upsertMany,
@@ -4032,8 +4118,8 @@ function createAi(config) {
4032
4118
  );
4033
4119
  const clientId = nanoid(7);
4034
4120
  const chatsStore = createStore_forUserAiChats();
4035
- const messagesStore = createStore_forChatMessages();
4036
4121
  const toolsStore = createStore_forTools();
4122
+ const messagesStore = createStore_forChatMessages(toolsStore, setToolResult);
4037
4123
  const context = {
4038
4124
  staticSessionInfoSig: new Signal(null),
4039
4125
  dynamicSessionInfoSig: new Signal(null),
@@ -4041,7 +4127,7 @@ function createAi(config) {
4041
4127
  chatsStore,
4042
4128
  messagesStore,
4043
4129
  toolsStore,
4044
- knowledgeByChatId: /* @__PURE__ */ new Map()
4130
+ knowledge: new KnowledgeStack()
4045
4131
  };
4046
4132
  let lastTokenKey;
4047
4133
  function onStatusDidChange(_newStatus) {
@@ -4099,7 +4185,7 @@ function createAi(config) {
4099
4185
  if ("event" in msg) {
4100
4186
  switch (msg.event) {
4101
4187
  case "cmd-failed":
4102
- _optionalChain([pendingCmd, 'optionalAccess', _61 => _61.reject, 'call', _62 => _62(new Error(msg.error))]);
4188
+ _optionalChain([pendingCmd, 'optionalAccess', _62 => _62.reject, 'call', _63 => _63(new Error(msg.error))]);
4103
4189
  break;
4104
4190
  case "delta": {
4105
4191
  const { id, delta } = msg;
@@ -4168,11 +4254,16 @@ function createAi(config) {
4168
4254
  break;
4169
4255
  case "abort-ai":
4170
4256
  break;
4257
+ case "set-tool-result":
4258
+ if (msg.ok) {
4259
+ context.messagesStore.upsert(msg.message);
4260
+ }
4261
+ break;
4171
4262
  default:
4172
4263
  return assertNever(msg, "Unhandled case");
4173
4264
  }
4174
4265
  }
4175
- _optionalChain([pendingCmd, 'optionalAccess', _63 => _63.resolve, 'call', _64 => _64(msg)]);
4266
+ _optionalChain([pendingCmd, 'optionalAccess', _64 => _64.resolve, 'call', _65 => _65(msg)]);
4176
4267
  }
4177
4268
  managedSocket.events.onMessage.subscribe(handleServerMessage);
4178
4269
  managedSocket.events.statusDidChange.subscribe(onStatusDidChange);
@@ -4218,14 +4309,11 @@ function createAi(config) {
4218
4309
  cursor: options.cursor
4219
4310
  });
4220
4311
  }
4221
- function getOrCreateChat(id, title, options) {
4312
+ function getOrCreateChat(id, options) {
4222
4313
  return sendClientMsgWithResponse({
4223
4314
  cmd: "get-or-create-chat",
4224
4315
  id,
4225
- options: {
4226
- title,
4227
- metadata: _optionalChain([options, 'optionalAccess', _65 => _65.metadata])
4228
- }
4316
+ options
4229
4317
  });
4230
4318
  }
4231
4319
  function getMessageTree(chatId) {
@@ -4234,56 +4322,44 @@ function createAi(config) {
4234
4322
  chatId
4235
4323
  });
4236
4324
  }
4237
- function registerKnowledgeSource(chatId, data) {
4238
- const knowledge = context.knowledgeByChatId.get(chatId);
4239
- if (knowledge === void 0) {
4240
- context.knowledgeByChatId.set(chatId, /* @__PURE__ */ new Set([data]));
4241
- } else {
4242
- knowledge.add(data);
4243
- }
4244
- return () => {
4245
- const knowledge2 = context.knowledgeByChatId.get(chatId);
4246
- if (knowledge2 !== void 0) {
4247
- knowledge2.delete(data);
4248
- if (knowledge2.size === 0) {
4249
- context.knowledgeByChatId.delete(chatId);
4250
- }
4251
- }
4252
- };
4325
+ function registerKnowledgeLayer(uniqueLayerId) {
4326
+ return context.knowledge.registerLayer(uniqueLayerId);
4253
4327
  }
4254
- function ask(chatId, messageId, options) {
4255
- const targetMessageId = context.messagesStore.createOptimistically(
4256
- chatId,
4257
- "assistant",
4258
- messageId
4259
- );
4260
- const copilotId = _optionalChain([options, 'optionalAccess', _66 => _66.copilotId]);
4261
- const stream = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _67 => _67.stream]), () => ( false));
4262
- const timeout = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _68 => _68.timeout]), () => ( DEFAULT_AI_TIMEOUT));
4263
- const knowledge = context.knowledgeByChatId.get(chatId);
4328
+ function deregisterKnowledgeLayer(layerKey) {
4329
+ context.knowledge.deregisterLayer(layerKey);
4330
+ }
4331
+ function updateKnowledge(layerKey, data, key = nanoid()) {
4332
+ context.knowledge.updateKnowledge(layerKey, key, data);
4333
+ }
4334
+ function debug_getAllKnowledge() {
4335
+ return context.knowledge.get();
4336
+ }
4337
+ async function setToolResult(chatId, messageId, toolCallId, result, options) {
4338
+ const knowledge = context.knowledge.get();
4264
4339
  return sendClientMsgWithResponse({
4265
- cmd: "ask-in-chat",
4340
+ cmd: "set-tool-result",
4266
4341
  chatId,
4267
- sourceMessage: messageId,
4268
- targetMessageId,
4342
+ messageId,
4343
+ toolCallId,
4269
4344
  clientId,
4345
+ result,
4270
4346
  generationOptions: {
4271
- copilotId,
4272
- stream,
4273
- knowledge: knowledge ? Array.from(knowledge) : void 0,
4347
+ copilotId: _optionalChain([options, 'optionalAccess', _66 => _66.copilotId]),
4348
+ stream: _optionalChain([options, 'optionalAccess', _67 => _67.stream]),
4349
+ timeout: _optionalChain([options, 'optionalAccess', _68 => _68.timeout]),
4350
+ knowledge: knowledge.length > 0 ? knowledge : void 0,
4274
4351
  tools: context.toolsStore.getToolsForChat(chatId).map((tool) => ({
4275
4352
  name: tool.name,
4276
4353
  description: tool.definition.description,
4277
4354
  parameters: tool.definition.parameters
4278
- })),
4279
- timeout
4355
+ }))
4280
4356
  }
4281
4357
  });
4282
4358
  }
4283
4359
  return Object.defineProperty(
4284
4360
  {
4285
4361
  [kInternal]: {
4286
- debugContext: () => context
4362
+ context
4287
4363
  },
4288
4364
  connect: () => managedSocket.connect(),
4289
4365
  reconnect: () => managedSocket.reconnect(),
@@ -4299,60 +4375,40 @@ function createAi(config) {
4299
4375
  getMessageTree,
4300
4376
  deleteMessage: (chatId, messageId) => sendClientMsgWithResponse({ cmd: "delete-message", chatId, messageId }),
4301
4377
  clearChat: (chatId) => sendClientMsgWithResponse({ cmd: "clear-chat", chatId }),
4302
- regenerateMessage: (chatId, messageId, options) => {
4303
- const parentUserMessageId = context.messagesStore.getLatestUserMessageAncestor(chatId, messageId);
4304
- if (parentUserMessageId === null) {
4305
- throw new Error(
4306
- `Unable to find user message ancestor for messageId: ${messageId}`
4307
- );
4308
- }
4309
- return ask(chatId, parentUserMessageId, options);
4310
- },
4311
- addUserMessageAndAsk: async (chatId, parentMessageId, message, options) => {
4312
- const content = [{ type: "text", text: message }];
4313
- const newMessageId = context.messagesStore.createOptimistically(
4314
- chatId,
4315
- "user",
4316
- parentMessageId,
4317
- content
4318
- );
4319
- const targetMessageId = context.messagesStore.createOptimistically(
4320
- chatId,
4321
- "assistant",
4322
- newMessageId
4323
- );
4324
- const copilotId = _optionalChain([options, 'optionalAccess', _69 => _69.copilotId]);
4325
- const stream = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _70 => _70.stream]), () => ( false));
4326
- const timeout = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _71 => _71.timeout]), () => ( DEFAULT_AI_TIMEOUT));
4327
- const knowledge = context.knowledgeByChatId.get(chatId);
4378
+ askUserMessageInChat: async (chatId, userMessage, targetMessageId, options) => {
4379
+ const knowledge = context.knowledge.get();
4328
4380
  return sendClientMsgWithResponse({
4329
4381
  cmd: "ask-in-chat",
4330
4382
  chatId,
4331
- sourceMessage: { id: newMessageId, parentMessageId, content },
4383
+ sourceMessage: userMessage,
4332
4384
  targetMessageId,
4333
4385
  clientId,
4334
4386
  generationOptions: {
4335
- copilotId,
4336
- stream,
4337
- knowledge: knowledge ? Array.from(knowledge) : void 0,
4387
+ copilotId: _optionalChain([options, 'optionalAccess', _69 => _69.copilotId]),
4388
+ stream: _optionalChain([options, 'optionalAccess', _70 => _70.stream]),
4389
+ timeout: _optionalChain([options, 'optionalAccess', _71 => _71.timeout]),
4390
+ knowledge: knowledge.length > 0 ? knowledge : void 0,
4338
4391
  tools: context.toolsStore.getToolsForChat(chatId).map((tool) => ({
4339
4392
  name: tool.name,
4340
4393
  description: tool.definition.description,
4341
4394
  parameters: tool.definition.parameters
4342
- })),
4343
- timeout
4395
+ }))
4344
4396
  }
4345
4397
  });
4346
4398
  },
4347
4399
  abort: (messageId) => sendClientMsgWithResponse({ cmd: "abort-ai", messageId }),
4400
+ setToolResult,
4348
4401
  getStatus: () => managedSocket.getStatus(),
4349
4402
  signals: {
4350
4403
  chats\u03A3: context.chatsStore.chats\u03A3,
4351
4404
  getChatMessagesForBranch\u03A3: context.messagesStore.getChatMessagesForBranch\u03A3,
4352
- getToolDefinition\u03A3: context.toolsStore.getToolCallByName\u03A3,
4353
- getMessagesForChat\u03A3: context.messagesStore.getMessagesForChat\u03A3
4405
+ getChatById: context.chatsStore.getChatById,
4406
+ getToolDefinition\u03A3: context.toolsStore.getToolDefinition\u03A3
4354
4407
  },
4355
- registerKnowledgeSource,
4408
+ registerKnowledgeLayer,
4409
+ deregisterKnowledgeLayer,
4410
+ updateKnowledge,
4411
+ debug_getAllKnowledge,
4356
4412
  registerChatTool: context.toolsStore.addToolDefinition,
4357
4413
  unregisterChatTool: context.toolsStore.removeToolDefinition
4358
4414
  },