@liveblocks/core 2.25.0-aiprivatebeta0 → 2.25.0-aiprivatebeta10
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 +325 -271
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +258 -112
- package/dist/index.d.ts +258 -112
- package/dist/index.js +241 -187
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
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-
|
|
9
|
+
var PKG_VERSION = "2.25.0-aiprivatebeta10";
|
|
10
10
|
var PKG_FORMAT = "esm";
|
|
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 (lastPart?.type === "text") {
|
|
3695
3693
|
lastPart.text += delta.textDelta;
|
|
@@ -3700,15 +3698,16 @@ function appendDelta(content, delta) {
|
|
|
3700
3698
|
case "reasoning-delta":
|
|
3701
3699
|
if (lastPart?.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: delta.textDelta ?? ""
|
|
3708
|
-
signature: delta.signature
|
|
3704
|
+
text: 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,68 @@ function appendDelta(content, delta) {
|
|
|
3716
3715
|
|
|
3717
3716
|
// src/ai.ts
|
|
3718
3717
|
var DEFAULT_REQUEST_TIMEOUT = 4e3;
|
|
3719
|
-
|
|
3718
|
+
function defineAiTool() {
|
|
3719
|
+
return (def) => {
|
|
3720
|
+
return def;
|
|
3721
|
+
};
|
|
3722
|
+
}
|
|
3723
|
+
var KnowledgeStack = class {
|
|
3724
|
+
#_layers;
|
|
3725
|
+
#stack;
|
|
3726
|
+
// / \
|
|
3727
|
+
// knowledge key "layer" key
|
|
3728
|
+
// (random, or optionally (one entry per mounted component)
|
|
3729
|
+
// set by user)
|
|
3730
|
+
#_cache;
|
|
3731
|
+
constructor() {
|
|
3732
|
+
this.#_layers = /* @__PURE__ */ new Set();
|
|
3733
|
+
this.#stack = new DefaultMap(
|
|
3734
|
+
() => /* @__PURE__ */ new Map()
|
|
3735
|
+
);
|
|
3736
|
+
this.#_cache = void 0;
|
|
3737
|
+
}
|
|
3738
|
+
// Typically a useId()
|
|
3739
|
+
registerLayer(uniqueLayerId) {
|
|
3740
|
+
const layerKey = uniqueLayerId;
|
|
3741
|
+
if (this.#_layers.has(layerKey))
|
|
3742
|
+
raise(`Layer '${layerKey}' already exists, provide a unique layer id`);
|
|
3743
|
+
this.#_layers.add(layerKey);
|
|
3744
|
+
return layerKey;
|
|
3745
|
+
}
|
|
3746
|
+
deregisterLayer(layerKey) {
|
|
3747
|
+
this.#_layers.delete(layerKey);
|
|
3748
|
+
let deleted = false;
|
|
3749
|
+
for (const [key, knowledge] of this.#stack) {
|
|
3750
|
+
if (knowledge.delete(layerKey)) {
|
|
3751
|
+
deleted = true;
|
|
3752
|
+
}
|
|
3753
|
+
if (knowledge.size === 0)
|
|
3754
|
+
this.#stack.delete(key);
|
|
3755
|
+
}
|
|
3756
|
+
if (deleted) {
|
|
3757
|
+
this.invalidate();
|
|
3758
|
+
}
|
|
3759
|
+
}
|
|
3760
|
+
get() {
|
|
3761
|
+
return this.#_cache ??= this.#recompute();
|
|
3762
|
+
}
|
|
3763
|
+
invalidate() {
|
|
3764
|
+
this.#_cache = void 0;
|
|
3765
|
+
}
|
|
3766
|
+
#recompute() {
|
|
3767
|
+
return Array.from(this.#stack.values()).flatMap(
|
|
3768
|
+
(layer) => (
|
|
3769
|
+
// Return only the last item (returns [] when empty)
|
|
3770
|
+
Array.from(layer.values()).slice(-1).filter(isDefined)
|
|
3771
|
+
)
|
|
3772
|
+
);
|
|
3773
|
+
}
|
|
3774
|
+
updateKnowledge(layerKey, key, data) {
|
|
3775
|
+
if (!this.#_layers.has(layerKey)) raise(`Unknown layer key: ${layerKey}`);
|
|
3776
|
+
this.#stack.getOrCreate(key).set(layerKey, data);
|
|
3777
|
+
this.invalidate();
|
|
3778
|
+
}
|
|
3779
|
+
};
|
|
3720
3780
|
function now() {
|
|
3721
3781
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
3722
3782
|
}
|
|
@@ -3730,6 +3790,11 @@ function createStore_forTools() {
|
|
|
3730
3790
|
return toolsByChatId\u03A3.getOrCreate(chatId).getOrCreate(toolName);
|
|
3731
3791
|
}
|
|
3732
3792
|
function addToolDefinition(chatId, name, definition) {
|
|
3793
|
+
if (!definition.execute && !definition.render) {
|
|
3794
|
+
throw new Error(
|
|
3795
|
+
"A tool definition must have an execute() function, a render property, or both."
|
|
3796
|
+
);
|
|
3797
|
+
}
|
|
3733
3798
|
toolsByChatId\u03A3.getOrCreate(chatId).getOrCreate(name).set(definition);
|
|
3734
3799
|
}
|
|
3735
3800
|
function removeToolDefinition(chatId, toolName) {
|
|
@@ -3751,13 +3816,15 @@ function createStore_forTools() {
|
|
|
3751
3816
|
}).filter((tool) => tool !== null);
|
|
3752
3817
|
}
|
|
3753
3818
|
return {
|
|
3754
|
-
|
|
3819
|
+
getToolDefinition\u03A3,
|
|
3755
3820
|
getToolsForChat,
|
|
3756
3821
|
addToolDefinition,
|
|
3757
3822
|
removeToolDefinition
|
|
3758
3823
|
};
|
|
3759
3824
|
}
|
|
3760
|
-
function createStore_forChatMessages() {
|
|
3825
|
+
function createStore_forChatMessages(toolsStore, setToolResult) {
|
|
3826
|
+
const autoExecutableMessages = /* @__PURE__ */ new Set();
|
|
3827
|
+
const seenToolCallIds = /* @__PURE__ */ new Set();
|
|
3761
3828
|
const messagePoolByChatId\u03A3 = new DefaultMap(
|
|
3762
3829
|
(_chatId) => new MutableSignal(
|
|
3763
3830
|
new TreePool(
|
|
@@ -3767,7 +3834,7 @@ function createStore_forChatMessages() {
|
|
|
3767
3834
|
)
|
|
3768
3835
|
)
|
|
3769
3836
|
);
|
|
3770
|
-
const
|
|
3837
|
+
const generatingMessages\u03A3 = new MutableSignal(
|
|
3771
3838
|
/* @__PURE__ */ new Map()
|
|
3772
3839
|
);
|
|
3773
3840
|
function createOptimistically(chatId, role, parentId, third) {
|
|
@@ -3781,7 +3848,8 @@ function createStore_forChatMessages() {
|
|
|
3781
3848
|
role,
|
|
3782
3849
|
parentId,
|
|
3783
3850
|
createdAt,
|
|
3784
|
-
content
|
|
3851
|
+
content,
|
|
3852
|
+
_optimistic: true
|
|
3785
3853
|
});
|
|
3786
3854
|
} else {
|
|
3787
3855
|
upsert({
|
|
@@ -3790,8 +3858,9 @@ function createStore_forChatMessages() {
|
|
|
3790
3858
|
role,
|
|
3791
3859
|
parentId,
|
|
3792
3860
|
createdAt,
|
|
3793
|
-
status: "
|
|
3794
|
-
contentSoFar: []
|
|
3861
|
+
status: "generating",
|
|
3862
|
+
contentSoFar: [],
|
|
3863
|
+
_optimistic: true
|
|
3795
3864
|
});
|
|
3796
3865
|
}
|
|
3797
3866
|
return id;
|
|
@@ -3808,7 +3877,7 @@ function createStore_forChatMessages() {
|
|
|
3808
3877
|
if (!chatMsgs\u03A3) return;
|
|
3809
3878
|
const existing = chatMsgs\u03A3.get().get(messageId);
|
|
3810
3879
|
if (!existing || existing.deletedAt) return;
|
|
3811
|
-
if (existing.role === "assistant" &&
|
|
3880
|
+
if (existing.role === "assistant" && existing.status !== "completed") {
|
|
3812
3881
|
upsert({ ...existing, deletedAt: now(), contentSoFar: [] });
|
|
3813
3882
|
} else {
|
|
3814
3883
|
upsert({ ...existing, deletedAt: now(), content: [] });
|
|
@@ -3823,19 +3892,59 @@ function createStore_forChatMessages() {
|
|
|
3823
3892
|
batch(() => {
|
|
3824
3893
|
const chatMsgs\u03A3 = messagePoolByChatId\u03A3.getOrCreate(message.chatId);
|
|
3825
3894
|
chatMsgs\u03A3.mutate((pool) => pool.upsert(message));
|
|
3826
|
-
if (message.role === "assistant" && message.status === "
|
|
3827
|
-
|
|
3895
|
+
if (message.role === "assistant" && message.status === "generating") {
|
|
3896
|
+
generatingMessages\u03A3.mutate((lut) => {
|
|
3828
3897
|
lut.set(message.id, structuredClone(message));
|
|
3829
3898
|
});
|
|
3830
3899
|
} else {
|
|
3831
|
-
|
|
3900
|
+
generatingMessages\u03A3.mutate((lut) => {
|
|
3832
3901
|
lut.delete(message.id);
|
|
3833
3902
|
});
|
|
3834
3903
|
}
|
|
3904
|
+
if (message.role === "assistant" && message.status === "awaiting-tool") {
|
|
3905
|
+
for (const toolCall of message.contentSoFar.filter(
|
|
3906
|
+
(part) => part.type === "tool-invocation" && part.status === "executing"
|
|
3907
|
+
)) {
|
|
3908
|
+
if (seenToolCallIds.has(toolCall.toolCallId)) {
|
|
3909
|
+
continue;
|
|
3910
|
+
}
|
|
3911
|
+
seenToolCallIds.add(toolCall.toolCallId);
|
|
3912
|
+
const toolDef = toolsStore.getToolDefinition\u03A3(message.chatId, toolCall.toolName).get();
|
|
3913
|
+
const respondSync = (result) => {
|
|
3914
|
+
setToolResult(
|
|
3915
|
+
message.chatId,
|
|
3916
|
+
message.id,
|
|
3917
|
+
toolCall.toolCallId,
|
|
3918
|
+
result
|
|
3919
|
+
// TODO Pass in AiGenerationOptions here, or make the backend use the same options
|
|
3920
|
+
).catch((err) => {
|
|
3921
|
+
error2(
|
|
3922
|
+
`Error trying to respond to tool-call: ${String(err)} (in respond())`
|
|
3923
|
+
);
|
|
3924
|
+
});
|
|
3925
|
+
};
|
|
3926
|
+
const executeFn = toolDef?.execute;
|
|
3927
|
+
if (executeFn && autoExecutableMessages.has(message.id)) {
|
|
3928
|
+
(async () => {
|
|
3929
|
+
const result = await executeFn(toolCall.args, {
|
|
3930
|
+
toolName: toolCall.toolName,
|
|
3931
|
+
toolCallId: toolCall.toolCallId
|
|
3932
|
+
});
|
|
3933
|
+
respondSync(result);
|
|
3934
|
+
})().catch((err) => {
|
|
3935
|
+
error2(
|
|
3936
|
+
`Error trying to respond to tool-call: ${String(err)} (in execute())`
|
|
3937
|
+
);
|
|
3938
|
+
});
|
|
3939
|
+
}
|
|
3940
|
+
}
|
|
3941
|
+
} else {
|
|
3942
|
+
autoExecutableMessages.delete(message.id);
|
|
3943
|
+
}
|
|
3835
3944
|
});
|
|
3836
3945
|
}
|
|
3837
3946
|
function addDelta(messageId, delta) {
|
|
3838
|
-
|
|
3947
|
+
generatingMessages\u03A3.mutate((lut) => {
|
|
3839
3948
|
const message = lut.get(messageId);
|
|
3840
3949
|
if (message === void 0) return false;
|
|
3841
3950
|
appendDelta(message.contentSoFar, delta);
|
|
@@ -3843,10 +3952,10 @@ function createStore_forChatMessages() {
|
|
|
3843
3952
|
return true;
|
|
3844
3953
|
});
|
|
3845
3954
|
}
|
|
3846
|
-
function*
|
|
3955
|
+
function* iterGeneratingMessages() {
|
|
3847
3956
|
for (const chatMsgs\u03A3 of messagePoolByChatId\u03A3.values()) {
|
|
3848
3957
|
for (const m of chatMsgs\u03A3.get()) {
|
|
3849
|
-
if (m.role === "assistant" && m.status === "
|
|
3958
|
+
if (m.role === "assistant" && m.status === "generating" && !m._optimistic) {
|
|
3850
3959
|
yield m;
|
|
3851
3960
|
}
|
|
3852
3961
|
}
|
|
@@ -3854,9 +3963,18 @@ function createStore_forChatMessages() {
|
|
|
3854
3963
|
}
|
|
3855
3964
|
function failAllPending() {
|
|
3856
3965
|
batch(() => {
|
|
3857
|
-
|
|
3966
|
+
generatingMessages\u03A3.mutate((lut) => {
|
|
3967
|
+
let deleted = false;
|
|
3968
|
+
for (const [k, v] of lut) {
|
|
3969
|
+
if (!v._optimistic) {
|
|
3970
|
+
lut.delete(k);
|
|
3971
|
+
deleted = true;
|
|
3972
|
+
}
|
|
3973
|
+
}
|
|
3974
|
+
return deleted;
|
|
3975
|
+
});
|
|
3858
3976
|
upsertMany(
|
|
3859
|
-
Array.from(
|
|
3977
|
+
Array.from(iterGeneratingMessages()).map(
|
|
3860
3978
|
(message) => ({
|
|
3861
3979
|
...message,
|
|
3862
3980
|
status: "failed",
|
|
@@ -3891,11 +4009,20 @@ function createStore_forChatMessages() {
|
|
|
3891
4009
|
}
|
|
3892
4010
|
function selectSpine(leaf) {
|
|
3893
4011
|
const spine = [];
|
|
4012
|
+
let lastVisitedMessage = null;
|
|
3894
4013
|
for (const message2 of pool.walkUp(leaf.id)) {
|
|
3895
4014
|
const prev = first(pool.walkLeft(message2.id, isAlive))?.id ?? null;
|
|
3896
4015
|
const next = first(pool.walkRight(message2.id, isAlive))?.id ?? null;
|
|
3897
4016
|
if (!message2.deletedAt || prev || next) {
|
|
3898
|
-
|
|
4017
|
+
const node = {
|
|
4018
|
+
...message2,
|
|
4019
|
+
navigation: { parent: null, prev, next }
|
|
4020
|
+
};
|
|
4021
|
+
if (lastVisitedMessage !== null) {
|
|
4022
|
+
lastVisitedMessage.navigation.parent = node.id;
|
|
4023
|
+
}
|
|
4024
|
+
lastVisitedMessage = node;
|
|
4025
|
+
spine.push(node);
|
|
3899
4026
|
}
|
|
3900
4027
|
}
|
|
3901
4028
|
return spine.reverse();
|
|
@@ -3921,18 +4048,6 @@ function createStore_forChatMessages() {
|
|
|
3921
4048
|
}
|
|
3922
4049
|
return fallback();
|
|
3923
4050
|
}
|
|
3924
|
-
function getLatestUserMessageAncestor(chatId, messageId) {
|
|
3925
|
-
const pool = messagePoolByChatId\u03A3.getOrCreate(chatId).get();
|
|
3926
|
-
const message = pool.get(messageId);
|
|
3927
|
-
if (!message) return null;
|
|
3928
|
-
if (message.role === "user") return message.id;
|
|
3929
|
-
for (const m of pool.walkUp(message.id)) {
|
|
3930
|
-
if (m.role === "user" && !m.deletedAt) {
|
|
3931
|
-
return m.id;
|
|
3932
|
-
}
|
|
3933
|
-
}
|
|
3934
|
-
return null;
|
|
3935
|
-
}
|
|
3936
4051
|
const immutableMessagesByBranch = new DefaultMap((chatId) => {
|
|
3937
4052
|
return new DefaultMap((branchId) => {
|
|
3938
4053
|
const messages\u03A3 = DerivedSignal.from(() => {
|
|
@@ -3940,16 +4055,16 @@ function createStore_forChatMessages() {
|
|
|
3940
4055
|
return selectBranch(pool, branchId);
|
|
3941
4056
|
}, shallow2);
|
|
3942
4057
|
return DerivedSignal.from(() => {
|
|
3943
|
-
const
|
|
4058
|
+
const generatingMessages = generatingMessages\u03A3.get();
|
|
3944
4059
|
return messages\u03A3.get().map((message) => {
|
|
3945
|
-
if (message.role !== "assistant" || message.status !== "
|
|
4060
|
+
if (message.role !== "assistant" || message.status !== "generating") {
|
|
3946
4061
|
return message;
|
|
3947
4062
|
}
|
|
3948
|
-
const
|
|
3949
|
-
if (
|
|
4063
|
+
const generatingMessage = generatingMessages.get(message.id);
|
|
4064
|
+
if (generatingMessage === void 0) return message;
|
|
3950
4065
|
return {
|
|
3951
4066
|
...message,
|
|
3952
|
-
contentSoFar:
|
|
4067
|
+
contentSoFar: generatingMessage.contentSoFar
|
|
3953
4068
|
};
|
|
3954
4069
|
});
|
|
3955
4070
|
}, shallow);
|
|
@@ -3958,21 +4073,10 @@ function createStore_forChatMessages() {
|
|
|
3958
4073
|
function getChatMessagesForBranch\u03A3(chatId, branch) {
|
|
3959
4074
|
return immutableMessagesByBranch.getOrCreate(chatId).getOrCreate(branch || null);
|
|
3960
4075
|
}
|
|
3961
|
-
const messagesByChatId\u03A3 = new DefaultMap((chatId) => {
|
|
3962
|
-
return DerivedSignal.from(() => {
|
|
3963
|
-
const pool = messagePoolByChatId\u03A3.getOrCreate(chatId).get();
|
|
3964
|
-
return Array.from(pool.sorted);
|
|
3965
|
-
});
|
|
3966
|
-
});
|
|
3967
|
-
function getMessagesForChat\u03A3(chatId) {
|
|
3968
|
-
return messagesByChatId\u03A3.getOrCreate(chatId);
|
|
3969
|
-
}
|
|
3970
4076
|
return {
|
|
3971
4077
|
// Readers
|
|
3972
4078
|
getMessageById,
|
|
3973
4079
|
getChatMessagesForBranch\u03A3,
|
|
3974
|
-
getMessagesForChat\u03A3,
|
|
3975
|
-
getLatestUserMessageAncestor,
|
|
3976
4080
|
// Mutations
|
|
3977
4081
|
createOptimistically,
|
|
3978
4082
|
upsert,
|
|
@@ -3980,7 +4084,10 @@ function createStore_forChatMessages() {
|
|
|
3980
4084
|
remove,
|
|
3981
4085
|
removeByChatId,
|
|
3982
4086
|
addDelta,
|
|
3983
|
-
failAllPending
|
|
4087
|
+
failAllPending,
|
|
4088
|
+
allowAutoExecuteToolCall(messageId) {
|
|
4089
|
+
autoExecutableMessages.add(messageId);
|
|
4090
|
+
}
|
|
3984
4091
|
};
|
|
3985
4092
|
}
|
|
3986
4093
|
function createStore_forUserAiChats() {
|
|
@@ -3988,7 +4095,7 @@ function createStore_forUserAiChats() {
|
|
|
3988
4095
|
SortedList.with((x, y) => y.createdAt < x.createdAt)
|
|
3989
4096
|
);
|
|
3990
4097
|
const chats\u03A3 = DerivedSignal.from(
|
|
3991
|
-
() => Array.from(mutable\u03A3.get()).filter((c) => !c.
|
|
4098
|
+
() => Array.from(mutable\u03A3.get()).filter((c) => !c.deletedAt)
|
|
3992
4099
|
);
|
|
3993
4100
|
function upsertMany(chats) {
|
|
3994
4101
|
mutable\u03A3.mutate((list) => {
|
|
@@ -4004,8 +4111,12 @@ function createStore_forUserAiChats() {
|
|
|
4004
4111
|
function remove(chatId) {
|
|
4005
4112
|
mutable\u03A3.mutate((list) => list.removeBy((c) => c.id === chatId, 1));
|
|
4006
4113
|
}
|
|
4114
|
+
function getChatById(chatId) {
|
|
4115
|
+
return Array.from(mutable\u03A3.get()).find((chat) => chat.id === chatId);
|
|
4116
|
+
}
|
|
4007
4117
|
return {
|
|
4008
4118
|
chats\u03A3,
|
|
4119
|
+
getChatById,
|
|
4009
4120
|
// Mutations
|
|
4010
4121
|
upsert,
|
|
4011
4122
|
upsertMany,
|
|
@@ -4019,10 +4130,9 @@ function createAi(config) {
|
|
|
4019
4130
|
false
|
|
4020
4131
|
// AI doesn't have actors (yet, but it will)
|
|
4021
4132
|
);
|
|
4022
|
-
const clientId = nanoid(7);
|
|
4023
4133
|
const chatsStore = createStore_forUserAiChats();
|
|
4024
|
-
const messagesStore = createStore_forChatMessages();
|
|
4025
4134
|
const toolsStore = createStore_forTools();
|
|
4135
|
+
const messagesStore = createStore_forChatMessages(toolsStore, setToolResult);
|
|
4026
4136
|
const context = {
|
|
4027
4137
|
staticSessionInfoSig: new Signal(null),
|
|
4028
4138
|
dynamicSessionInfoSig: new Signal(null),
|
|
@@ -4030,11 +4140,10 @@ function createAi(config) {
|
|
|
4030
4140
|
chatsStore,
|
|
4031
4141
|
messagesStore,
|
|
4032
4142
|
toolsStore,
|
|
4033
|
-
|
|
4143
|
+
knowledge: new KnowledgeStack()
|
|
4034
4144
|
};
|
|
4035
4145
|
let lastTokenKey;
|
|
4036
|
-
function onStatusDidChange(
|
|
4037
|
-
warn("onStatusDidChange", newStatus);
|
|
4146
|
+
function onStatusDidChange(_newStatus) {
|
|
4038
4147
|
const authValue = managedSocket.authValue;
|
|
4039
4148
|
if (authValue !== null) {
|
|
4040
4149
|
const tokenKey = getBearerTokenFromAuthValue(authValue);
|
|
@@ -4070,7 +4179,6 @@ function createAi(config) {
|
|
|
4070
4179
|
}
|
|
4071
4180
|
}
|
|
4072
4181
|
function onDidConnect() {
|
|
4073
|
-
warn("onDidConnect");
|
|
4074
4182
|
}
|
|
4075
4183
|
function onDidDisconnect() {
|
|
4076
4184
|
warn("onDidDisconnect");
|
|
@@ -4134,7 +4242,7 @@ function createAi(config) {
|
|
|
4134
4242
|
case "get-chats":
|
|
4135
4243
|
context.chatsStore.upsertMany(msg.chats);
|
|
4136
4244
|
break;
|
|
4137
|
-
case "create-chat":
|
|
4245
|
+
case "get-or-create-chat":
|
|
4138
4246
|
context.chatsStore.upsert(msg.chat);
|
|
4139
4247
|
break;
|
|
4140
4248
|
case "delete-chat":
|
|
@@ -4145,23 +4253,25 @@ function createAi(config) {
|
|
|
4145
4253
|
context.chatsStore.upsert(msg.chat);
|
|
4146
4254
|
context.messagesStore.upsertMany(msg.messages);
|
|
4147
4255
|
break;
|
|
4148
|
-
case "add-user-message":
|
|
4149
|
-
context.messagesStore.upsert(msg.message);
|
|
4150
|
-
break;
|
|
4151
4256
|
case "delete-message":
|
|
4152
4257
|
context.messagesStore.remove(msg.chatId, msg.messageId);
|
|
4153
4258
|
break;
|
|
4154
4259
|
case "clear-chat":
|
|
4155
4260
|
context.messagesStore.removeByChatId(msg.chatId);
|
|
4156
4261
|
break;
|
|
4157
|
-
case "ask-
|
|
4158
|
-
if (msg.
|
|
4159
|
-
context.messagesStore.upsert(msg.
|
|
4160
|
-
} else {
|
|
4262
|
+
case "ask-in-chat":
|
|
4263
|
+
if (msg.sourceMessage) {
|
|
4264
|
+
context.messagesStore.upsert(msg.sourceMessage);
|
|
4161
4265
|
}
|
|
4266
|
+
context.messagesStore.upsert(msg.targetMessage);
|
|
4162
4267
|
break;
|
|
4163
4268
|
case "abort-ai":
|
|
4164
4269
|
break;
|
|
4270
|
+
case "set-tool-result":
|
|
4271
|
+
if (msg.ok) {
|
|
4272
|
+
context.messagesStore.upsert(msg.message);
|
|
4273
|
+
}
|
|
4274
|
+
break;
|
|
4165
4275
|
default:
|
|
4166
4276
|
return assertNever(msg, "Unhandled case");
|
|
4167
4277
|
}
|
|
@@ -4212,13 +4322,11 @@ function createAi(config) {
|
|
|
4212
4322
|
cursor: options.cursor
|
|
4213
4323
|
});
|
|
4214
4324
|
}
|
|
4215
|
-
function
|
|
4325
|
+
function getOrCreateChat(id, options) {
|
|
4216
4326
|
return sendClientMsgWithResponse({
|
|
4217
|
-
cmd: "create-chat",
|
|
4327
|
+
cmd: "get-or-create-chat",
|
|
4218
4328
|
id,
|
|
4219
|
-
|
|
4220
|
-
ephemeral: options?.ephemeral ?? false,
|
|
4221
|
-
metadata: options?.metadata ?? {}
|
|
4329
|
+
options
|
|
4222
4330
|
});
|
|
4223
4331
|
}
|
|
4224
4332
|
function getMessageTree(chatId) {
|
|
@@ -4227,145 +4335,90 @@ function createAi(config) {
|
|
|
4227
4335
|
chatId
|
|
4228
4336
|
});
|
|
4229
4337
|
}
|
|
4230
|
-
function
|
|
4231
|
-
|
|
4232
|
-
if (chatContext === void 0) {
|
|
4233
|
-
context.contextByChatId.set(chatId, /* @__PURE__ */ new Set([data]));
|
|
4234
|
-
} else {
|
|
4235
|
-
chatContext.add(data);
|
|
4236
|
-
}
|
|
4237
|
-
return () => {
|
|
4238
|
-
const chatContext2 = context.contextByChatId.get(chatId);
|
|
4239
|
-
if (chatContext2 !== void 0) {
|
|
4240
|
-
chatContext2.delete(data);
|
|
4241
|
-
if (chatContext2.size === 0) {
|
|
4242
|
-
context.contextByChatId.delete(chatId);
|
|
4243
|
-
}
|
|
4244
|
-
}
|
|
4245
|
-
};
|
|
4338
|
+
function registerKnowledgeLayer(uniqueLayerId) {
|
|
4339
|
+
return context.knowledge.registerLayer(uniqueLayerId);
|
|
4246
4340
|
}
|
|
4247
|
-
function
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
const
|
|
4255
|
-
const
|
|
4256
|
-
|
|
4257
|
-
return sendClientMsgWithResponse({
|
|
4258
|
-
cmd: "ask-ai",
|
|
4341
|
+
function deregisterKnowledgeLayer(layerKey) {
|
|
4342
|
+
context.knowledge.deregisterLayer(layerKey);
|
|
4343
|
+
}
|
|
4344
|
+
function updateKnowledge(layerKey, data, key = nanoid()) {
|
|
4345
|
+
context.knowledge.updateKnowledge(layerKey, key, data);
|
|
4346
|
+
}
|
|
4347
|
+
async function setToolResult(chatId, messageId, toolCallId, result, options) {
|
|
4348
|
+
const knowledge = context.knowledge.get();
|
|
4349
|
+
const resp = await sendClientMsgWithResponse({
|
|
4350
|
+
cmd: "set-tool-result",
|
|
4259
4351
|
chatId,
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4352
|
+
messageId,
|
|
4353
|
+
toolCallId,
|
|
4354
|
+
result,
|
|
4355
|
+
generationOptions: {
|
|
4356
|
+
copilotId: options?.copilotId,
|
|
4357
|
+
stream: options?.stream,
|
|
4358
|
+
timeout: options?.timeout,
|
|
4359
|
+
knowledge: knowledge.length > 0 ? knowledge : void 0,
|
|
4360
|
+
tools: context.toolsStore.getToolsForChat(chatId).map((tool) => ({
|
|
4361
|
+
name: tool.name,
|
|
4362
|
+
description: tool.definition.description,
|
|
4363
|
+
parameters: tool.definition.parameters
|
|
4364
|
+
}))
|
|
4365
|
+
}
|
|
4272
4366
|
});
|
|
4367
|
+
if (resp.ok) {
|
|
4368
|
+
messagesStore.allowAutoExecuteToolCall(resp.message.id);
|
|
4369
|
+
}
|
|
4370
|
+
return resp;
|
|
4273
4371
|
}
|
|
4274
4372
|
return Object.defineProperty(
|
|
4275
4373
|
{
|
|
4276
4374
|
[kInternal]: {
|
|
4277
|
-
|
|
4375
|
+
context
|
|
4278
4376
|
},
|
|
4279
4377
|
connect: () => managedSocket.connect(),
|
|
4280
4378
|
reconnect: () => managedSocket.reconnect(),
|
|
4281
4379
|
disconnect: () => managedSocket.disconnect(),
|
|
4282
4380
|
getChats,
|
|
4283
|
-
|
|
4381
|
+
getOrCreateChat,
|
|
4284
4382
|
deleteChat: (chatId) => {
|
|
4285
|
-
return sendClientMsgWithResponse({
|
|
4286
|
-
cmd: "delete-chat",
|
|
4287
|
-
chatId
|
|
4288
|
-
});
|
|
4383
|
+
return sendClientMsgWithResponse({ cmd: "delete-chat", chatId });
|
|
4289
4384
|
},
|
|
4290
4385
|
getMessageTree,
|
|
4291
4386
|
deleteMessage: (chatId, messageId) => sendClientMsgWithResponse({ cmd: "delete-message", chatId, messageId }),
|
|
4292
4387
|
clearChat: (chatId) => sendClientMsgWithResponse({ cmd: "clear-chat", chatId }),
|
|
4293
|
-
|
|
4294
|
-
const
|
|
4295
|
-
const
|
|
4296
|
-
|
|
4297
|
-
"user",
|
|
4298
|
-
parentMessageId,
|
|
4299
|
-
content
|
|
4300
|
-
);
|
|
4301
|
-
return sendClientMsgWithResponse({
|
|
4302
|
-
cmd: "add-user-message",
|
|
4303
|
-
id: newMessageId,
|
|
4304
|
-
chatId,
|
|
4305
|
-
parentMessageId,
|
|
4306
|
-
content
|
|
4307
|
-
});
|
|
4308
|
-
},
|
|
4309
|
-
ask,
|
|
4310
|
-
regenerateMessage: (chatId, messageId, options) => {
|
|
4311
|
-
const parentUserMessageId = context.messagesStore.getLatestUserMessageAncestor(chatId, messageId);
|
|
4312
|
-
if (parentUserMessageId === null) {
|
|
4313
|
-
throw new Error(
|
|
4314
|
-
`Unable to find user message ancestor for messageId: ${messageId}`
|
|
4315
|
-
);
|
|
4316
|
-
}
|
|
4317
|
-
return ask(chatId, parentUserMessageId, options);
|
|
4318
|
-
},
|
|
4319
|
-
addUserMessageAndAsk: async (chatId, parentMessageId, message, options) => {
|
|
4320
|
-
const content = [{ type: "text", text: message }];
|
|
4321
|
-
const newMessageId = context.messagesStore.createOptimistically(
|
|
4322
|
-
chatId,
|
|
4323
|
-
"user",
|
|
4324
|
-
parentMessageId,
|
|
4325
|
-
content
|
|
4326
|
-
);
|
|
4327
|
-
const targetMessageId = context.messagesStore.createOptimistically(
|
|
4388
|
+
askUserMessageInChat: async (chatId, userMessage, targetMessageId, options) => {
|
|
4389
|
+
const knowledge = context.knowledge.get();
|
|
4390
|
+
const resp = await sendClientMsgWithResponse({
|
|
4391
|
+
cmd: "ask-in-chat",
|
|
4328
4392
|
chatId,
|
|
4329
|
-
|
|
4330
|
-
newMessageId
|
|
4331
|
-
);
|
|
4332
|
-
await sendClientMsgWithResponse({
|
|
4333
|
-
cmd: "add-user-message",
|
|
4334
|
-
id: newMessageId,
|
|
4335
|
-
chatId,
|
|
4336
|
-
parentMessageId,
|
|
4337
|
-
content
|
|
4338
|
-
});
|
|
4339
|
-
const copilotId = options?.copilotId;
|
|
4340
|
-
const stream = options?.stream ?? false;
|
|
4341
|
-
const timeout = options?.timeout ?? DEFAULT_AI_TIMEOUT;
|
|
4342
|
-
const chatContext = context.contextByChatId.get(chatId);
|
|
4343
|
-
return sendClientMsgWithResponse({
|
|
4344
|
-
cmd: "ask-ai",
|
|
4345
|
-
chatId,
|
|
4346
|
-
sourceMessageId: newMessageId,
|
|
4393
|
+
sourceMessage: userMessage,
|
|
4347
4394
|
targetMessageId,
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4395
|
+
generationOptions: {
|
|
4396
|
+
copilotId: options?.copilotId,
|
|
4397
|
+
stream: options?.stream,
|
|
4398
|
+
timeout: options?.timeout,
|
|
4399
|
+
knowledge: knowledge.length > 0 ? knowledge : void 0,
|
|
4400
|
+
tools: context.toolsStore.getToolsForChat(chatId).map((tool) => ({
|
|
4401
|
+
name: tool.name,
|
|
4402
|
+
description: tool.definition.description,
|
|
4403
|
+
parameters: tool.definition.parameters
|
|
4404
|
+
}))
|
|
4405
|
+
}
|
|
4358
4406
|
});
|
|
4407
|
+
messagesStore.allowAutoExecuteToolCall(resp.targetMessage.id);
|
|
4408
|
+
return resp;
|
|
4359
4409
|
},
|
|
4360
4410
|
abort: (messageId) => sendClientMsgWithResponse({ cmd: "abort-ai", messageId }),
|
|
4411
|
+
setToolResult,
|
|
4361
4412
|
getStatus: () => managedSocket.getStatus(),
|
|
4362
4413
|
signals: {
|
|
4363
4414
|
chats\u03A3: context.chatsStore.chats\u03A3,
|
|
4364
4415
|
getChatMessagesForBranch\u03A3: context.messagesStore.getChatMessagesForBranch\u03A3,
|
|
4365
|
-
getToolDefinition\u03A3: context.toolsStore.
|
|
4366
|
-
getMessagesForChat\u03A3: context.messagesStore.getMessagesForChat\u03A3
|
|
4416
|
+
getToolDefinition\u03A3: context.toolsStore.getToolDefinition\u03A3
|
|
4367
4417
|
},
|
|
4368
|
-
|
|
4418
|
+
getChatById: context.chatsStore.getChatById,
|
|
4419
|
+
registerKnowledgeLayer,
|
|
4420
|
+
deregisterKnowledgeLayer,
|
|
4421
|
+
updateKnowledge,
|
|
4369
4422
|
registerChatTool: context.toolsStore.addToolDefinition,
|
|
4370
4423
|
unregisterChatTool: context.toolsStore.removeToolDefinition
|
|
4371
4424
|
},
|
|
@@ -10531,6 +10584,7 @@ export {
|
|
|
10531
10584
|
createManagedPool,
|
|
10532
10585
|
createNotificationSettings,
|
|
10533
10586
|
createThreadId,
|
|
10587
|
+
defineAiTool,
|
|
10534
10588
|
deprecate,
|
|
10535
10589
|
deprecateIf,
|
|
10536
10590
|
detectDupes,
|