@diegoaltoworks/chatter 0.55.0 → 0.57.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/channels/index.js +46 -4
- package/dist/channels/index.mjs +46 -4
- package/dist/channels/pipeline.d.ts +16 -1
- package/dist/channels/pipeline.d.ts.map +1 -1
- package/dist/channels/telegram/channel.d.ts +8 -1
- package/dist/channels/telegram/channel.d.ts.map +1 -1
- package/dist/channels/telegram/index.js +52 -5
- package/dist/channels/telegram/index.mjs +52 -5
- package/dist/channels/whatsapp/inbound.d.ts +11 -1
- package/dist/channels/whatsapp/inbound.d.ts.map +1 -1
- package/dist/channels/whatsapp/index.js +52 -5
- package/dist/channels/whatsapp/index.mjs +52 -5
- package/dist/core/answer.d.ts +31 -0
- package/dist/core/answer.d.ts.map +1 -1
- package/dist/core/pipeline.d.ts +52 -1
- package/dist/core/pipeline.d.ts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +128 -19
- package/dist/index.mjs +127 -19
- package/dist/mcp-server.d.ts.map +1 -1
- package/dist/mcp-server.js +57 -8
- package/dist/mcp-server.mjs +57 -8
- package/dist/routes/demo.d.ts.map +1 -1
- package/dist/routes/openai.d.ts.map +1 -1
- package/dist/routes/private.d.ts.map +1 -1
- package/dist/routes/public.d.ts.map +1 -1
- package/dist/server.js +104 -13
- package/dist/server.mjs +104 -13
- package/dist/types.d.ts +42 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -209,6 +209,18 @@ async function answerOnce({
|
|
|
209
209
|
}
|
|
210
210
|
return completeOnce({ client, system, messages, temperature, model });
|
|
211
211
|
}
|
|
212
|
+
async function applyTransformReply(transformReply, input, logger) {
|
|
213
|
+
if (!transformReply) return input.text;
|
|
214
|
+
try {
|
|
215
|
+
return await transformReply(input);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
logger?.error(
|
|
218
|
+
`transformReply threw for channel "${input.channel}"; sending the original reply`,
|
|
219
|
+
error
|
|
220
|
+
);
|
|
221
|
+
return input.text;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
212
224
|
async function* answerStream({
|
|
213
225
|
answerFn,
|
|
214
226
|
client,
|
|
@@ -372,14 +384,35 @@ async function prepareChat({
|
|
|
372
384
|
messages,
|
|
373
385
|
personaLayer,
|
|
374
386
|
channelHint,
|
|
375
|
-
buckets
|
|
387
|
+
buckets,
|
|
388
|
+
sender,
|
|
389
|
+
rewriteQuery,
|
|
390
|
+
rerankContext,
|
|
391
|
+
logger
|
|
376
392
|
}) {
|
|
377
393
|
const lastUserMsg = lastUserMessage(messages);
|
|
378
394
|
if (!lastUserMsg) {
|
|
379
395
|
throw new Error("no user message found in conversation");
|
|
380
396
|
}
|
|
381
397
|
const { topK, label } = MODE_SETTINGS[mode];
|
|
382
|
-
|
|
398
|
+
let query = lastUserMsg.content;
|
|
399
|
+
if (rewriteQuery) {
|
|
400
|
+
try {
|
|
401
|
+
const rewritten = await rewriteQuery({ query, mode, sender });
|
|
402
|
+
if (typeof rewritten === "string" && rewritten.trim()) query = rewritten;
|
|
403
|
+
} catch (error) {
|
|
404
|
+
logger?.error("rewriteQuery threw; falling back to the original query", error);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
let ctx = await store.query(query, topK, buckets ?? defaultBuckets(mode));
|
|
408
|
+
if (rerankContext) {
|
|
409
|
+
try {
|
|
410
|
+
const reranked = await rerankContext({ query, chunks: ctx });
|
|
411
|
+
if (Array.isArray(reranked) && reranked.every((c) => typeof c === "string")) ctx = reranked;
|
|
412
|
+
} catch (error) {
|
|
413
|
+
logger?.error("rerankContext threw; falling back to the original chunks", error);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
383
416
|
const persona = personaLayer?.trim() || (mode === "private" ? prompts.privatePersona : prompts.publicPersona);
|
|
384
417
|
const hint = channelHint?.trim();
|
|
385
418
|
const system = [
|
|
@@ -918,7 +951,10 @@ async function createMCPServer(config) {
|
|
|
918
951
|
prompts,
|
|
919
952
|
mode: "public",
|
|
920
953
|
messages: conversationMessages,
|
|
921
|
-
buckets
|
|
954
|
+
buckets,
|
|
955
|
+
rewriteQuery: config.rewriteQuery,
|
|
956
|
+
rerankContext: config.rerankContext,
|
|
957
|
+
logger: log
|
|
922
958
|
});
|
|
923
959
|
const result = await answerOnce({
|
|
924
960
|
answerFn: config.answerFn,
|
|
@@ -929,6 +965,11 @@ async function createMCPServer(config) {
|
|
|
929
965
|
conversationId: convId,
|
|
930
966
|
model
|
|
931
967
|
});
|
|
968
|
+
const replyText = await applyTransformReply(
|
|
969
|
+
config.transformReply,
|
|
970
|
+
{ channel: "mcp-public", conversationId: convId, text: result.content },
|
|
971
|
+
log
|
|
972
|
+
);
|
|
932
973
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
933
974
|
const duration = Date.now() - startTime;
|
|
934
975
|
await logger.logChatInteraction(
|
|
@@ -936,7 +977,7 @@ async function createMCPServer(config) {
|
|
|
936
977
|
convId,
|
|
937
978
|
conversationMessages,
|
|
938
979
|
ctx,
|
|
939
|
-
|
|
980
|
+
replyText ?? "",
|
|
940
981
|
duration,
|
|
941
982
|
cost
|
|
942
983
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -944,7 +985,7 @@ async function createMCPServer(config) {
|
|
|
944
985
|
content: [
|
|
945
986
|
{
|
|
946
987
|
type: "text",
|
|
947
|
-
text:
|
|
988
|
+
text: replyText ?? ""
|
|
948
989
|
}
|
|
949
990
|
],
|
|
950
991
|
_meta: {
|
|
@@ -999,7 +1040,10 @@ async function createMCPServer(config) {
|
|
|
999
1040
|
prompts,
|
|
1000
1041
|
mode: "private",
|
|
1001
1042
|
messages: conversationMessages,
|
|
1002
|
-
buckets
|
|
1043
|
+
buckets,
|
|
1044
|
+
rewriteQuery: config.rewriteQuery,
|
|
1045
|
+
rerankContext: config.rerankContext,
|
|
1046
|
+
logger: log
|
|
1003
1047
|
});
|
|
1004
1048
|
const result = await answerOnce({
|
|
1005
1049
|
answerFn: config.answerFn,
|
|
@@ -1010,6 +1054,11 @@ async function createMCPServer(config) {
|
|
|
1010
1054
|
conversationId: convId,
|
|
1011
1055
|
model
|
|
1012
1056
|
});
|
|
1057
|
+
const replyText = await applyTransformReply(
|
|
1058
|
+
config.transformReply,
|
|
1059
|
+
{ channel: "mcp-private", conversationId: convId, text: result.content },
|
|
1060
|
+
log
|
|
1061
|
+
);
|
|
1013
1062
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
1014
1063
|
const duration = Date.now() - startTime;
|
|
1015
1064
|
await logger.logChatInteraction(
|
|
@@ -1017,7 +1066,7 @@ async function createMCPServer(config) {
|
|
|
1017
1066
|
convId,
|
|
1018
1067
|
conversationMessages,
|
|
1019
1068
|
ctx,
|
|
1020
|
-
|
|
1069
|
+
replyText ?? "",
|
|
1021
1070
|
duration,
|
|
1022
1071
|
cost
|
|
1023
1072
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -1025,7 +1074,7 @@ async function createMCPServer(config) {
|
|
|
1025
1074
|
content: [
|
|
1026
1075
|
{
|
|
1027
1076
|
type: "text",
|
|
1028
|
-
text:
|
|
1077
|
+
text: replyText ?? ""
|
|
1029
1078
|
}
|
|
1030
1079
|
],
|
|
1031
1080
|
_meta: {
|
|
@@ -1469,7 +1518,16 @@ function demoRoutes(deps) {
|
|
|
1469
1518
|
return c.json({ error: "no user message found in conversation" }, 400);
|
|
1470
1519
|
}
|
|
1471
1520
|
const buckets = await resolveBuckets({ mode: "public", bucketsFor: config.bucketsFor });
|
|
1472
|
-
const { system } = await prepareChat({
|
|
1521
|
+
const { system } = await prepareChat({
|
|
1522
|
+
store,
|
|
1523
|
+
prompts,
|
|
1524
|
+
mode: "public",
|
|
1525
|
+
messages,
|
|
1526
|
+
buckets,
|
|
1527
|
+
rewriteQuery: config.rewriteQuery,
|
|
1528
|
+
rerankContext: config.rerankContext,
|
|
1529
|
+
logger
|
|
1530
|
+
});
|
|
1473
1531
|
const model = config.openai.model || DEFAULT_MODEL;
|
|
1474
1532
|
const url = new URL(c.req.url);
|
|
1475
1533
|
const wantsStream3 = url.searchParams.get("stream") === "1";
|
|
@@ -1498,7 +1556,12 @@ function demoRoutes(deps) {
|
|
|
1498
1556
|
mode: "public",
|
|
1499
1557
|
model
|
|
1500
1558
|
});
|
|
1501
|
-
|
|
1559
|
+
const reply = await applyTransformReply(
|
|
1560
|
+
config.transformReply,
|
|
1561
|
+
{ channel: "widget-demo", text: out.content },
|
|
1562
|
+
logger
|
|
1563
|
+
);
|
|
1564
|
+
return c.json({ reply: reply ?? "" });
|
|
1502
1565
|
} catch (error) {
|
|
1503
1566
|
const err = error;
|
|
1504
1567
|
logger.error("Demo chat error:", err);
|
|
@@ -1541,7 +1604,7 @@ function oversizedBodyError(message) {
|
|
|
1541
1604
|
return { error: { message, type: "invalid_request_error" } };
|
|
1542
1605
|
}
|
|
1543
1606
|
function openaiRoutes(deps) {
|
|
1544
|
-
const { client, store, config, prompts, apiKeyManager } = deps;
|
|
1607
|
+
const { client, store, config, prompts, apiKeyManager, logger } = deps;
|
|
1545
1608
|
const app = new Hono2();
|
|
1546
1609
|
const { limitPublic, limitPrivate } = createRateLimiter2(config);
|
|
1547
1610
|
const requirePrivateJWT = createJWTMiddleware(config);
|
|
@@ -1583,13 +1646,23 @@ function openaiRoutes(deps) {
|
|
|
1583
1646
|
sender: jwtSubject(c),
|
|
1584
1647
|
bucketsFor: config.bucketsFor
|
|
1585
1648
|
});
|
|
1649
|
+
const sender = jwtSubject(c) ?? apiKeySubject(c);
|
|
1586
1650
|
let system;
|
|
1587
1651
|
try {
|
|
1588
|
-
({ system } = await prepareChat({
|
|
1652
|
+
({ system } = await prepareChat({
|
|
1653
|
+
store,
|
|
1654
|
+
prompts,
|
|
1655
|
+
mode,
|
|
1656
|
+
messages,
|
|
1657
|
+
buckets,
|
|
1658
|
+
sender,
|
|
1659
|
+
rewriteQuery: config.rewriteQuery,
|
|
1660
|
+
rerankContext: config.rerankContext,
|
|
1661
|
+
logger
|
|
1662
|
+
}));
|
|
1589
1663
|
} catch {
|
|
1590
1664
|
return errorJson(c, 400, "Conversation must contain at least one user message");
|
|
1591
1665
|
}
|
|
1592
|
-
const sender = jwtSubject(c) ?? apiKeySubject(c);
|
|
1593
1666
|
const conversationId = clientConversationId(c, body) ?? crypto.randomUUID();
|
|
1594
1667
|
c.header("x-conversation-id", conversationId);
|
|
1595
1668
|
const id = `chatcmpl-${crypto.randomUUID()}`;
|
|
@@ -1636,13 +1709,18 @@ function openaiRoutes(deps) {
|
|
|
1636
1709
|
temperature,
|
|
1637
1710
|
model: serverModel
|
|
1638
1711
|
});
|
|
1712
|
+
const reply = await applyTransformReply(
|
|
1713
|
+
config.transformReply,
|
|
1714
|
+
{ channel: `openai-compat-${mode}`, sender, conversationId, text: out.content },
|
|
1715
|
+
logger
|
|
1716
|
+
);
|
|
1639
1717
|
return c.json({
|
|
1640
1718
|
id,
|
|
1641
1719
|
object: "chat.completion",
|
|
1642
1720
|
created,
|
|
1643
1721
|
model: serverModel,
|
|
1644
1722
|
choices: [
|
|
1645
|
-
{ index: 0, message: { role: "assistant", content:
|
|
1723
|
+
{ index: 0, message: { role: "assistant", content: reply ?? "" }, finish_reason: "stop" }
|
|
1646
1724
|
],
|
|
1647
1725
|
usage: out.usage
|
|
1648
1726
|
});
|
|
@@ -1672,7 +1750,7 @@ function wantsStream(c) {
|
|
|
1672
1750
|
return acc.includes("text/event-stream");
|
|
1673
1751
|
}
|
|
1674
1752
|
function privateRoutes(deps) {
|
|
1675
|
-
const { client, store, config, prompts } = deps;
|
|
1753
|
+
const { client, store, config, prompts, logger } = deps;
|
|
1676
1754
|
const app = new Hono3();
|
|
1677
1755
|
const requirePrivateJWT = createJWTMiddleware(config);
|
|
1678
1756
|
const { limitPrivate } = createRateLimiter2(config);
|
|
@@ -1694,7 +1772,17 @@ function privateRoutes(deps) {
|
|
|
1694
1772
|
});
|
|
1695
1773
|
let system;
|
|
1696
1774
|
try {
|
|
1697
|
-
({ system } = await prepareChat({
|
|
1775
|
+
({ system } = await prepareChat({
|
|
1776
|
+
store,
|
|
1777
|
+
prompts,
|
|
1778
|
+
mode: "private",
|
|
1779
|
+
messages,
|
|
1780
|
+
buckets,
|
|
1781
|
+
sender,
|
|
1782
|
+
rewriteQuery: config.rewriteQuery,
|
|
1783
|
+
rerankContext: config.rerankContext,
|
|
1784
|
+
logger
|
|
1785
|
+
}));
|
|
1698
1786
|
} catch {
|
|
1699
1787
|
return c.json({ error: "no user message found in conversation" }, 400);
|
|
1700
1788
|
}
|
|
@@ -1727,7 +1815,12 @@ function privateRoutes(deps) {
|
|
|
1727
1815
|
sender,
|
|
1728
1816
|
model
|
|
1729
1817
|
});
|
|
1730
|
-
|
|
1818
|
+
const reply = await applyTransformReply(
|
|
1819
|
+
config.transformReply,
|
|
1820
|
+
{ channel: "widget-private", sender, text: out.content },
|
|
1821
|
+
logger
|
|
1822
|
+
);
|
|
1823
|
+
return c.json({ reply: reply ?? "" });
|
|
1731
1824
|
});
|
|
1732
1825
|
return app;
|
|
1733
1826
|
}
|
|
@@ -1765,7 +1858,16 @@ function publicRoutes(deps) {
|
|
|
1765
1858
|
const buckets = await resolveBuckets({ mode: "public", bucketsFor: config.bucketsFor });
|
|
1766
1859
|
let system;
|
|
1767
1860
|
try {
|
|
1768
|
-
({ system } = await prepareChat({
|
|
1861
|
+
({ system } = await prepareChat({
|
|
1862
|
+
store,
|
|
1863
|
+
prompts,
|
|
1864
|
+
mode: "public",
|
|
1865
|
+
messages,
|
|
1866
|
+
buckets,
|
|
1867
|
+
rewriteQuery: config.rewriteQuery,
|
|
1868
|
+
rerankContext: config.rerankContext,
|
|
1869
|
+
logger
|
|
1870
|
+
}));
|
|
1769
1871
|
} catch {
|
|
1770
1872
|
return c.json({ error: "no user message found in conversation" }, 400);
|
|
1771
1873
|
}
|
|
@@ -1789,7 +1891,12 @@ function publicRoutes(deps) {
|
|
|
1789
1891
|
});
|
|
1790
1892
|
}
|
|
1791
1893
|
const out = await answerOnce({ answerFn, client, system, messages, mode: "public", model });
|
|
1792
|
-
|
|
1894
|
+
const reply = await applyTransformReply(
|
|
1895
|
+
config.transformReply,
|
|
1896
|
+
{ channel: "widget-public", text: out.content },
|
|
1897
|
+
logger
|
|
1898
|
+
);
|
|
1899
|
+
return c.json({ reply: reply ?? "" });
|
|
1793
1900
|
});
|
|
1794
1901
|
return app;
|
|
1795
1902
|
}
|
|
@@ -2126,6 +2233,7 @@ export {
|
|
|
2126
2233
|
VectorStore,
|
|
2127
2234
|
answerOnce,
|
|
2128
2235
|
answerStream,
|
|
2236
|
+
applyTransformReply,
|
|
2129
2237
|
chatBodyLimit,
|
|
2130
2238
|
completeOnce,
|
|
2131
2239
|
completeStream,
|
package/dist/mcp-server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAoBH,YAAY,EACV,WAAW,EACX,QAAQ,EACR,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAoBH,YAAY,EACV,WAAW,EACX,QAAQ,EACR,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,gBAAgB,qEA0T7D"}
|
package/dist/mcp-server.js
CHANGED
|
@@ -104,6 +104,18 @@ async function answerOnce({
|
|
|
104
104
|
}
|
|
105
105
|
return completeOnce({ client, system, messages, temperature, model });
|
|
106
106
|
}
|
|
107
|
+
async function applyTransformReply(transformReply, input, logger) {
|
|
108
|
+
if (!transformReply) return input.text;
|
|
109
|
+
try {
|
|
110
|
+
return await transformReply(input);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
logger?.error(
|
|
113
|
+
`transformReply threw for channel "${input.channel}"; sending the original reply`,
|
|
114
|
+
error
|
|
115
|
+
);
|
|
116
|
+
return input.text;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
107
119
|
|
|
108
120
|
// src/core/buckets.ts
|
|
109
121
|
function defaultBuckets(mode) {
|
|
@@ -172,14 +184,35 @@ async function prepareChat({
|
|
|
172
184
|
messages,
|
|
173
185
|
personaLayer,
|
|
174
186
|
channelHint,
|
|
175
|
-
buckets
|
|
187
|
+
buckets,
|
|
188
|
+
sender,
|
|
189
|
+
rewriteQuery,
|
|
190
|
+
rerankContext,
|
|
191
|
+
logger
|
|
176
192
|
}) {
|
|
177
193
|
const lastUserMsg = lastUserMessage(messages);
|
|
178
194
|
if (!lastUserMsg) {
|
|
179
195
|
throw new Error("no user message found in conversation");
|
|
180
196
|
}
|
|
181
197
|
const { topK, label } = MODE_SETTINGS[mode];
|
|
182
|
-
|
|
198
|
+
let query = lastUserMsg.content;
|
|
199
|
+
if (rewriteQuery) {
|
|
200
|
+
try {
|
|
201
|
+
const rewritten = await rewriteQuery({ query, mode, sender });
|
|
202
|
+
if (typeof rewritten === "string" && rewritten.trim()) query = rewritten;
|
|
203
|
+
} catch (error) {
|
|
204
|
+
logger?.error("rewriteQuery threw; falling back to the original query", error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
let ctx = await store.query(query, topK, buckets ?? defaultBuckets(mode));
|
|
208
|
+
if (rerankContext) {
|
|
209
|
+
try {
|
|
210
|
+
const reranked = await rerankContext({ query, chunks: ctx });
|
|
211
|
+
if (Array.isArray(reranked) && reranked.every((c) => typeof c === "string")) ctx = reranked;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
logger?.error("rerankContext threw; falling back to the original chunks", error);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
183
216
|
const persona = personaLayer?.trim() || (mode === "private" ? prompts.privatePersona : prompts.publicPersona);
|
|
184
217
|
const hint = channelHint?.trim();
|
|
185
218
|
const system = [
|
|
@@ -693,7 +726,10 @@ async function createMCPServer(config) {
|
|
|
693
726
|
prompts,
|
|
694
727
|
mode: "public",
|
|
695
728
|
messages: conversationMessages,
|
|
696
|
-
buckets
|
|
729
|
+
buckets,
|
|
730
|
+
rewriteQuery: config.rewriteQuery,
|
|
731
|
+
rerankContext: config.rerankContext,
|
|
732
|
+
logger: log
|
|
697
733
|
});
|
|
698
734
|
const result = await answerOnce({
|
|
699
735
|
answerFn: config.answerFn,
|
|
@@ -704,6 +740,11 @@ async function createMCPServer(config) {
|
|
|
704
740
|
conversationId: convId,
|
|
705
741
|
model
|
|
706
742
|
});
|
|
743
|
+
const replyText = await applyTransformReply(
|
|
744
|
+
config.transformReply,
|
|
745
|
+
{ channel: "mcp-public", conversationId: convId, text: result.content },
|
|
746
|
+
log
|
|
747
|
+
);
|
|
707
748
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
708
749
|
const duration = Date.now() - startTime;
|
|
709
750
|
await logger.logChatInteraction(
|
|
@@ -711,7 +752,7 @@ async function createMCPServer(config) {
|
|
|
711
752
|
convId,
|
|
712
753
|
conversationMessages,
|
|
713
754
|
ctx,
|
|
714
|
-
|
|
755
|
+
replyText ?? "",
|
|
715
756
|
duration,
|
|
716
757
|
cost
|
|
717
758
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -719,7 +760,7 @@ async function createMCPServer(config) {
|
|
|
719
760
|
content: [
|
|
720
761
|
{
|
|
721
762
|
type: "text",
|
|
722
|
-
text:
|
|
763
|
+
text: replyText ?? ""
|
|
723
764
|
}
|
|
724
765
|
],
|
|
725
766
|
_meta: {
|
|
@@ -774,7 +815,10 @@ async function createMCPServer(config) {
|
|
|
774
815
|
prompts,
|
|
775
816
|
mode: "private",
|
|
776
817
|
messages: conversationMessages,
|
|
777
|
-
buckets
|
|
818
|
+
buckets,
|
|
819
|
+
rewriteQuery: config.rewriteQuery,
|
|
820
|
+
rerankContext: config.rerankContext,
|
|
821
|
+
logger: log
|
|
778
822
|
});
|
|
779
823
|
const result = await answerOnce({
|
|
780
824
|
answerFn: config.answerFn,
|
|
@@ -785,6 +829,11 @@ async function createMCPServer(config) {
|
|
|
785
829
|
conversationId: convId,
|
|
786
830
|
model
|
|
787
831
|
});
|
|
832
|
+
const replyText = await applyTransformReply(
|
|
833
|
+
config.transformReply,
|
|
834
|
+
{ channel: "mcp-private", conversationId: convId, text: result.content },
|
|
835
|
+
log
|
|
836
|
+
);
|
|
788
837
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
789
838
|
const duration = Date.now() - startTime;
|
|
790
839
|
await logger.logChatInteraction(
|
|
@@ -792,7 +841,7 @@ async function createMCPServer(config) {
|
|
|
792
841
|
convId,
|
|
793
842
|
conversationMessages,
|
|
794
843
|
ctx,
|
|
795
|
-
|
|
844
|
+
replyText ?? "",
|
|
796
845
|
duration,
|
|
797
846
|
cost
|
|
798
847
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -800,7 +849,7 @@ async function createMCPServer(config) {
|
|
|
800
849
|
content: [
|
|
801
850
|
{
|
|
802
851
|
type: "text",
|
|
803
|
-
text:
|
|
852
|
+
text: replyText ?? ""
|
|
804
853
|
}
|
|
805
854
|
],
|
|
806
855
|
_meta: {
|
package/dist/mcp-server.mjs
CHANGED
|
@@ -70,6 +70,18 @@ async function answerOnce({
|
|
|
70
70
|
}
|
|
71
71
|
return completeOnce({ client, system, messages, temperature, model });
|
|
72
72
|
}
|
|
73
|
+
async function applyTransformReply(transformReply, input, logger) {
|
|
74
|
+
if (!transformReply) return input.text;
|
|
75
|
+
try {
|
|
76
|
+
return await transformReply(input);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
logger?.error(
|
|
79
|
+
`transformReply threw for channel "${input.channel}"; sending the original reply`,
|
|
80
|
+
error
|
|
81
|
+
);
|
|
82
|
+
return input.text;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
73
85
|
|
|
74
86
|
// src/core/buckets.ts
|
|
75
87
|
function defaultBuckets(mode) {
|
|
@@ -138,14 +150,35 @@ async function prepareChat({
|
|
|
138
150
|
messages,
|
|
139
151
|
personaLayer,
|
|
140
152
|
channelHint,
|
|
141
|
-
buckets
|
|
153
|
+
buckets,
|
|
154
|
+
sender,
|
|
155
|
+
rewriteQuery,
|
|
156
|
+
rerankContext,
|
|
157
|
+
logger
|
|
142
158
|
}) {
|
|
143
159
|
const lastUserMsg = lastUserMessage(messages);
|
|
144
160
|
if (!lastUserMsg) {
|
|
145
161
|
throw new Error("no user message found in conversation");
|
|
146
162
|
}
|
|
147
163
|
const { topK, label } = MODE_SETTINGS[mode];
|
|
148
|
-
|
|
164
|
+
let query = lastUserMsg.content;
|
|
165
|
+
if (rewriteQuery) {
|
|
166
|
+
try {
|
|
167
|
+
const rewritten = await rewriteQuery({ query, mode, sender });
|
|
168
|
+
if (typeof rewritten === "string" && rewritten.trim()) query = rewritten;
|
|
169
|
+
} catch (error) {
|
|
170
|
+
logger?.error("rewriteQuery threw; falling back to the original query", error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
let ctx = await store.query(query, topK, buckets ?? defaultBuckets(mode));
|
|
174
|
+
if (rerankContext) {
|
|
175
|
+
try {
|
|
176
|
+
const reranked = await rerankContext({ query, chunks: ctx });
|
|
177
|
+
if (Array.isArray(reranked) && reranked.every((c) => typeof c === "string")) ctx = reranked;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
logger?.error("rerankContext threw; falling back to the original chunks", error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
149
182
|
const persona = personaLayer?.trim() || (mode === "private" ? prompts.privatePersona : prompts.publicPersona);
|
|
150
183
|
const hint = channelHint?.trim();
|
|
151
184
|
const system = [
|
|
@@ -659,7 +692,10 @@ async function createMCPServer(config) {
|
|
|
659
692
|
prompts,
|
|
660
693
|
mode: "public",
|
|
661
694
|
messages: conversationMessages,
|
|
662
|
-
buckets
|
|
695
|
+
buckets,
|
|
696
|
+
rewriteQuery: config.rewriteQuery,
|
|
697
|
+
rerankContext: config.rerankContext,
|
|
698
|
+
logger: log
|
|
663
699
|
});
|
|
664
700
|
const result = await answerOnce({
|
|
665
701
|
answerFn: config.answerFn,
|
|
@@ -670,6 +706,11 @@ async function createMCPServer(config) {
|
|
|
670
706
|
conversationId: convId,
|
|
671
707
|
model
|
|
672
708
|
});
|
|
709
|
+
const replyText = await applyTransformReply(
|
|
710
|
+
config.transformReply,
|
|
711
|
+
{ channel: "mcp-public", conversationId: convId, text: result.content },
|
|
712
|
+
log
|
|
713
|
+
);
|
|
673
714
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
674
715
|
const duration = Date.now() - startTime;
|
|
675
716
|
await logger.logChatInteraction(
|
|
@@ -677,7 +718,7 @@ async function createMCPServer(config) {
|
|
|
677
718
|
convId,
|
|
678
719
|
conversationMessages,
|
|
679
720
|
ctx,
|
|
680
|
-
|
|
721
|
+
replyText ?? "",
|
|
681
722
|
duration,
|
|
682
723
|
cost
|
|
683
724
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -685,7 +726,7 @@ async function createMCPServer(config) {
|
|
|
685
726
|
content: [
|
|
686
727
|
{
|
|
687
728
|
type: "text",
|
|
688
|
-
text:
|
|
729
|
+
text: replyText ?? ""
|
|
689
730
|
}
|
|
690
731
|
],
|
|
691
732
|
_meta: {
|
|
@@ -740,7 +781,10 @@ async function createMCPServer(config) {
|
|
|
740
781
|
prompts,
|
|
741
782
|
mode: "private",
|
|
742
783
|
messages: conversationMessages,
|
|
743
|
-
buckets
|
|
784
|
+
buckets,
|
|
785
|
+
rewriteQuery: config.rewriteQuery,
|
|
786
|
+
rerankContext: config.rerankContext,
|
|
787
|
+
logger: log
|
|
744
788
|
});
|
|
745
789
|
const result = await answerOnce({
|
|
746
790
|
answerFn: config.answerFn,
|
|
@@ -751,6 +795,11 @@ async function createMCPServer(config) {
|
|
|
751
795
|
conversationId: convId,
|
|
752
796
|
model
|
|
753
797
|
});
|
|
798
|
+
const replyText = await applyTransformReply(
|
|
799
|
+
config.transformReply,
|
|
800
|
+
{ channel: "mcp-private", conversationId: convId, text: result.content },
|
|
801
|
+
log
|
|
802
|
+
);
|
|
754
803
|
const cost = calculateCost(result.usage, config.openai.pricing);
|
|
755
804
|
const duration = Date.now() - startTime;
|
|
756
805
|
await logger.logChatInteraction(
|
|
@@ -758,7 +807,7 @@ async function createMCPServer(config) {
|
|
|
758
807
|
convId,
|
|
759
808
|
conversationMessages,
|
|
760
809
|
ctx,
|
|
761
|
-
|
|
810
|
+
replyText ?? "",
|
|
762
811
|
duration,
|
|
763
812
|
cost
|
|
764
813
|
).catch((err) => log.error("Logging error:", err));
|
|
@@ -766,7 +815,7 @@ async function createMCPServer(config) {
|
|
|
766
815
|
content: [
|
|
767
816
|
{
|
|
768
817
|
type: "text",
|
|
769
|
-
text:
|
|
818
|
+
text: replyText ?? ""
|
|
770
819
|
}
|
|
771
820
|
],
|
|
772
821
|
_meta: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"demo.d.ts","sourceRoot":"","sources":["../../src/routes/demo.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAY5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAuFnD,wBAAgB,UAAU,CAAC,IAAI,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"demo.d.ts","sourceRoot":"","sources":["../../src/routes/demo.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAY5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAuFnD,wBAAgB,UAAU,CAAC,IAAI,EAAE,kBAAkB,8EAkIlD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/routes/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAU5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAsDnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/routes/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAU5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAsDnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,8EA+KpD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"private.d.ts","sourceRoot":"","sources":["../../src/routes/private.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAS5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,aAAa,CAAC,IAAI,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"private.d.ts","sourceRoot":"","sources":["../../src/routes/private.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAS5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,aAAa,CAAC,IAAI,EAAE,kBAAkB,8EAsFrD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/routes/public.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAW5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/routes/public.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAW5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,8EAqFpD"}
|