@diegoaltoworks/chatter 0.43.0 → 0.44.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/core/messages.d.ts +38 -0
- package/dist/core/messages.d.ts.map +1 -0
- package/dist/core/messages.test.d.ts +8 -0
- package/dist/core/messages.test.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -50
- package/dist/index.mjs +60 -50
- package/dist/routes/demo.d.ts.map +1 -1
- package/dist/routes/openai.d.ts +0 -7
- 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/package.json +1 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound chat message normalization — the trust boundary for conversations.
|
|
3
|
+
*
|
|
4
|
+
* Every HTTP chat surface accepts a caller-supplied conversation, and the
|
|
5
|
+
* server owns the system prompt (guardrails + persona + retrieved context).
|
|
6
|
+
* Nothing a client sends may add to, or masquerade as, that prompt, so the
|
|
7
|
+
* only roles that survive normalization are `user` and `assistant`:
|
|
8
|
+
* `system`, `developer` and `tool` messages are dropped, not forwarded.
|
|
9
|
+
*
|
|
10
|
+
* Content is reduced to a plain string: strings pass through and OpenAI-style
|
|
11
|
+
* content-part arrays are flattened to their text parts. Anything else (image
|
|
12
|
+
* parts, tool calls, objects, numbers) carries no usable text and is skipped.
|
|
13
|
+
*/
|
|
14
|
+
import type { PipelineMessage } from "./pipeline";
|
|
15
|
+
/**
|
|
16
|
+
* Normalize a raw `messages` array to the pipeline format.
|
|
17
|
+
*
|
|
18
|
+
* Returns `null` when the input is not a non-empty array, when any entry is
|
|
19
|
+
* not an object, or when nothing usable remains after filtering.
|
|
20
|
+
*/
|
|
21
|
+
export declare function normalizeMessages(raw: unknown): PipelineMessage[] | null;
|
|
22
|
+
/** Outcome of normalizing a widget chat request body. */
|
|
23
|
+
export type NormalizedChatBody = {
|
|
24
|
+
ok: true;
|
|
25
|
+
messages: PipelineMessage[];
|
|
26
|
+
} | {
|
|
27
|
+
ok: false;
|
|
28
|
+
error: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Normalize the widget chat body shape, which accepts either a single
|
|
32
|
+
* `message` string or a `messages` conversation array.
|
|
33
|
+
*
|
|
34
|
+
* The resulting messages have been through `normalizeMessages`, so a client
|
|
35
|
+
* cannot inject a system turn on these surfaces either.
|
|
36
|
+
*/
|
|
37
|
+
export declare function normalizeChatBody(body: unknown): NormalizedChatBody;
|
|
38
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/core/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAWlD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,eAAe,EAAE,GAAG,IAAI,CA8BxE;AAED,yDAAyD;AACzD,MAAM,MAAM,kBAAkB,GAC1B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,eAAe,EAAE,CAAA;CAAE,GACzC;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjC;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,kBAAkB,CA2BnE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.test.d.ts","sourceRoot":"","sources":["../../src/core/messages.test.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export { defaultBuckets, resolveBuckets } from "./core/buckets";
|
|
|
22
22
|
export { detectLeakage, guardOutput, scrubOutput } from "./core/guardrails";
|
|
23
23
|
export { completeOnce, completeStream, DEFAULT_MODEL } from "./core/llm";
|
|
24
24
|
export { loadKnowledge } from "./core/loaders";
|
|
25
|
+
export type { NormalizedChatBody } from "./core/messages";
|
|
26
|
+
export { normalizeChatBody, normalizeMessages } from "./core/messages";
|
|
25
27
|
export type { PipelineMessage, PipelineMode, PreparedChat } from "./core/pipeline";
|
|
26
28
|
export { prepareChat } from "./core/pipeline";
|
|
27
29
|
export { PromptLoader } from "./core/prompts";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEzE,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAIvC,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1C,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,QAAQ,EACR,eAAe,GAChB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACrD,YAAY,EACV,QAAQ,EACR,aAAa,EACb,cAAc,EACd,aAAa,EACb,WAAW,GACZ,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACzD,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEpE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAA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CAAC;AAE9C,YAAY,EACV,WAAW,EACX,aAAa,EACb,WAAW,EACX,aAAa,EACb,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7559,6 +7559,8 @@ __export(index_exports, {
|
|
|
7559
7559
|
guardOutput: () => guardOutput,
|
|
7560
7560
|
jwtSubject: () => jwtSubject,
|
|
7561
7561
|
loadKnowledge: () => loadKnowledge,
|
|
7562
|
+
normalizeChatBody: () => normalizeChatBody,
|
|
7563
|
+
normalizeMessages: () => normalizeMessages,
|
|
7562
7564
|
openaiRoutes: () => openaiRoutes,
|
|
7563
7565
|
prepareChat: () => prepareChat,
|
|
7564
7566
|
privateRoutes: () => privateRoutes,
|
|
@@ -7845,6 +7847,52 @@ function loadKnowledge(root = "config/knowledge") {
|
|
|
7845
7847
|
return docs;
|
|
7846
7848
|
}
|
|
7847
7849
|
|
|
7850
|
+
// src/core/messages.ts
|
|
7851
|
+
function isClientRole(role) {
|
|
7852
|
+
return role === "user" || role === "assistant";
|
|
7853
|
+
}
|
|
7854
|
+
function normalizeMessages(raw) {
|
|
7855
|
+
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
7856
|
+
const messages = [];
|
|
7857
|
+
for (const entry of raw) {
|
|
7858
|
+
if (!entry || typeof entry !== "object") return null;
|
|
7859
|
+
const { role, content } = entry;
|
|
7860
|
+
if (!isClientRole(role)) continue;
|
|
7861
|
+
let text = null;
|
|
7862
|
+
if (typeof content === "string") {
|
|
7863
|
+
text = content;
|
|
7864
|
+
} else if (Array.isArray(content)) {
|
|
7865
|
+
text = content.filter(
|
|
7866
|
+
(part) => !!part && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
7867
|
+
).map((part) => part.text).join("");
|
|
7868
|
+
}
|
|
7869
|
+
if (text === null) continue;
|
|
7870
|
+
messages.push({ role, content: text });
|
|
7871
|
+
}
|
|
7872
|
+
return messages.length > 0 ? messages : null;
|
|
7873
|
+
}
|
|
7874
|
+
function normalizeChatBody(body) {
|
|
7875
|
+
const source = body && typeof body === "object" ? body : {};
|
|
7876
|
+
if (Array.isArray(source.messages)) {
|
|
7877
|
+
if (source.messages.length === 0) {
|
|
7878
|
+
return { ok: false, error: "messages array cannot be empty" };
|
|
7879
|
+
}
|
|
7880
|
+
const messages = normalizeMessages(source.messages);
|
|
7881
|
+
if (!messages) {
|
|
7882
|
+
return {
|
|
7883
|
+
ok: false,
|
|
7884
|
+
error: "messages must contain user/assistant messages with text content"
|
|
7885
|
+
};
|
|
7886
|
+
}
|
|
7887
|
+
return { ok: true, messages };
|
|
7888
|
+
}
|
|
7889
|
+
const { message } = source;
|
|
7890
|
+
if (message && typeof message !== "object") {
|
|
7891
|
+
return { ok: true, messages: [{ role: "user", content: String(message) }] };
|
|
7892
|
+
}
|
|
7893
|
+
return { ok: false, error: "either 'message' or 'messages' required" };
|
|
7894
|
+
}
|
|
7895
|
+
|
|
7848
7896
|
// src/core/pipeline.ts
|
|
7849
7897
|
var MODE_SETTINGS = {
|
|
7850
7898
|
public: { topK: 6, label: "Context" },
|
|
@@ -32840,17 +32888,11 @@ function demoRoutes(deps) {
|
|
|
32840
32888
|
app.post("/api/demo/chat", limitDemo(), async (c) => {
|
|
32841
32889
|
try {
|
|
32842
32890
|
const body = await c.req.json().catch(() => ({}));
|
|
32843
|
-
|
|
32844
|
-
if (
|
|
32845
|
-
|
|
32846
|
-
if (messages.length === 0) {
|
|
32847
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
32848
|
-
}
|
|
32849
|
-
} else if (body?.message) {
|
|
32850
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
32851
|
-
} else {
|
|
32852
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
32891
|
+
const normalized = normalizeChatBody(body);
|
|
32892
|
+
if (!normalized.ok) {
|
|
32893
|
+
return c.json({ error: normalized.error }, 400);
|
|
32853
32894
|
}
|
|
32895
|
+
const { messages } = normalized;
|
|
32854
32896
|
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
|
|
32855
32897
|
if (!lastUserMsg) {
|
|
32856
32898
|
return c.json({ error: "no user message found in conversation" }, 400);
|
|
@@ -32901,26 +32943,6 @@ ${ctx.join("\n\n")}`
|
|
|
32901
32943
|
// src/routes/openai.ts
|
|
32902
32944
|
var import_hono2 = require("hono");
|
|
32903
32945
|
var import_streaming2 = require("hono/streaming");
|
|
32904
|
-
function normalizeMessages(raw) {
|
|
32905
|
-
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
32906
|
-
const messages = [];
|
|
32907
|
-
for (const entry of raw) {
|
|
32908
|
-
if (!entry || typeof entry !== "object") return null;
|
|
32909
|
-
const { role, content } = entry;
|
|
32910
|
-
if (role !== "user" && role !== "assistant") continue;
|
|
32911
|
-
let text = null;
|
|
32912
|
-
if (typeof content === "string") {
|
|
32913
|
-
text = content;
|
|
32914
|
-
} else if (Array.isArray(content)) {
|
|
32915
|
-
text = content.filter(
|
|
32916
|
-
(part) => !!part && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
32917
|
-
).map((part) => part.text).join("");
|
|
32918
|
-
}
|
|
32919
|
-
if (text === null) continue;
|
|
32920
|
-
messages.push({ role, content: text });
|
|
32921
|
-
}
|
|
32922
|
-
return messages.length > 0 ? messages : null;
|
|
32923
|
-
}
|
|
32924
32946
|
function errorJson(c, status, message) {
|
|
32925
32947
|
return c.json(
|
|
32926
32948
|
{ error: { message, type: status === 401 ? "authentication_error" : "invalid_request_error" } },
|
|
@@ -33055,17 +33077,11 @@ function privateRoutes(deps) {
|
|
|
33055
33077
|
app.use("/api/private/*", limitPrivate2());
|
|
33056
33078
|
app.post("/api/private/chat", async (c) => {
|
|
33057
33079
|
const body = await c.req.json().catch(() => ({}));
|
|
33058
|
-
|
|
33059
|
-
if (
|
|
33060
|
-
|
|
33061
|
-
if (messages.length === 0) {
|
|
33062
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
33063
|
-
}
|
|
33064
|
-
} else if (body?.message) {
|
|
33065
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
33066
|
-
} else {
|
|
33067
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
33080
|
+
const normalized = normalizeChatBody(body);
|
|
33081
|
+
if (!normalized.ok) {
|
|
33082
|
+
return c.json({ error: normalized.error }, 400);
|
|
33068
33083
|
}
|
|
33084
|
+
const { messages } = normalized;
|
|
33069
33085
|
const buckets = await resolveBuckets({
|
|
33070
33086
|
mode: "private",
|
|
33071
33087
|
sender: jwtSubject(c),
|
|
@@ -33123,17 +33139,11 @@ function publicRoutes(deps) {
|
|
|
33123
33139
|
app.use("/api/public/*", limitPublic2());
|
|
33124
33140
|
app.post("/api/public/chat", async (c) => {
|
|
33125
33141
|
const body = await c.req.json().catch(() => ({}));
|
|
33126
|
-
|
|
33127
|
-
if (
|
|
33128
|
-
|
|
33129
|
-
if (messages.length === 0) {
|
|
33130
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
33131
|
-
}
|
|
33132
|
-
} else if (body?.message) {
|
|
33133
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
33134
|
-
} else {
|
|
33135
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
33142
|
+
const normalized = normalizeChatBody(body);
|
|
33143
|
+
if (!normalized.ok) {
|
|
33144
|
+
return c.json({ error: normalized.error }, 400);
|
|
33136
33145
|
}
|
|
33146
|
+
const { messages } = normalized;
|
|
33137
33147
|
const buckets = await resolveBuckets({ mode: "public", bucketsFor: config2.bucketsFor });
|
|
33138
33148
|
let system;
|
|
33139
33149
|
try {
|
|
@@ -33462,6 +33472,8 @@ function createShortener(config2, fetchImpl = fetch) {
|
|
|
33462
33472
|
guardOutput,
|
|
33463
33473
|
jwtSubject,
|
|
33464
33474
|
loadKnowledge,
|
|
33475
|
+
normalizeChatBody,
|
|
33476
|
+
normalizeMessages,
|
|
33465
33477
|
openaiRoutes,
|
|
33466
33478
|
prepareChat,
|
|
33467
33479
|
privateRoutes,
|
package/dist/index.mjs
CHANGED
|
@@ -7807,6 +7807,52 @@ function loadKnowledge(root = "config/knowledge") {
|
|
|
7807
7807
|
return docs;
|
|
7808
7808
|
}
|
|
7809
7809
|
|
|
7810
|
+
// src/core/messages.ts
|
|
7811
|
+
function isClientRole(role) {
|
|
7812
|
+
return role === "user" || role === "assistant";
|
|
7813
|
+
}
|
|
7814
|
+
function normalizeMessages(raw) {
|
|
7815
|
+
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
7816
|
+
const messages = [];
|
|
7817
|
+
for (const entry of raw) {
|
|
7818
|
+
if (!entry || typeof entry !== "object") return null;
|
|
7819
|
+
const { role, content } = entry;
|
|
7820
|
+
if (!isClientRole(role)) continue;
|
|
7821
|
+
let text = null;
|
|
7822
|
+
if (typeof content === "string") {
|
|
7823
|
+
text = content;
|
|
7824
|
+
} else if (Array.isArray(content)) {
|
|
7825
|
+
text = content.filter(
|
|
7826
|
+
(part) => !!part && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
7827
|
+
).map((part) => part.text).join("");
|
|
7828
|
+
}
|
|
7829
|
+
if (text === null) continue;
|
|
7830
|
+
messages.push({ role, content: text });
|
|
7831
|
+
}
|
|
7832
|
+
return messages.length > 0 ? messages : null;
|
|
7833
|
+
}
|
|
7834
|
+
function normalizeChatBody(body) {
|
|
7835
|
+
const source = body && typeof body === "object" ? body : {};
|
|
7836
|
+
if (Array.isArray(source.messages)) {
|
|
7837
|
+
if (source.messages.length === 0) {
|
|
7838
|
+
return { ok: false, error: "messages array cannot be empty" };
|
|
7839
|
+
}
|
|
7840
|
+
const messages = normalizeMessages(source.messages);
|
|
7841
|
+
if (!messages) {
|
|
7842
|
+
return {
|
|
7843
|
+
ok: false,
|
|
7844
|
+
error: "messages must contain user/assistant messages with text content"
|
|
7845
|
+
};
|
|
7846
|
+
}
|
|
7847
|
+
return { ok: true, messages };
|
|
7848
|
+
}
|
|
7849
|
+
const { message } = source;
|
|
7850
|
+
if (message && typeof message !== "object") {
|
|
7851
|
+
return { ok: true, messages: [{ role: "user", content: String(message) }] };
|
|
7852
|
+
}
|
|
7853
|
+
return { ok: false, error: "either 'message' or 'messages' required" };
|
|
7854
|
+
}
|
|
7855
|
+
|
|
7810
7856
|
// src/core/pipeline.ts
|
|
7811
7857
|
var MODE_SETTINGS = {
|
|
7812
7858
|
public: { topK: 6, label: "Context" },
|
|
@@ -32802,17 +32848,11 @@ function demoRoutes(deps) {
|
|
|
32802
32848
|
app.post("/api/demo/chat", limitDemo(), async (c) => {
|
|
32803
32849
|
try {
|
|
32804
32850
|
const body = await c.req.json().catch(() => ({}));
|
|
32805
|
-
|
|
32806
|
-
if (
|
|
32807
|
-
|
|
32808
|
-
if (messages.length === 0) {
|
|
32809
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
32810
|
-
}
|
|
32811
|
-
} else if (body?.message) {
|
|
32812
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
32813
|
-
} else {
|
|
32814
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
32851
|
+
const normalized = normalizeChatBody(body);
|
|
32852
|
+
if (!normalized.ok) {
|
|
32853
|
+
return c.json({ error: normalized.error }, 400);
|
|
32815
32854
|
}
|
|
32855
|
+
const { messages } = normalized;
|
|
32816
32856
|
const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
|
|
32817
32857
|
if (!lastUserMsg) {
|
|
32818
32858
|
return c.json({ error: "no user message found in conversation" }, 400);
|
|
@@ -32863,26 +32903,6 @@ ${ctx.join("\n\n")}`
|
|
|
32863
32903
|
// src/routes/openai.ts
|
|
32864
32904
|
import { Hono as Hono2 } from "hono";
|
|
32865
32905
|
import { stream as stream2 } from "hono/streaming";
|
|
32866
|
-
function normalizeMessages(raw) {
|
|
32867
|
-
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
32868
|
-
const messages = [];
|
|
32869
|
-
for (const entry of raw) {
|
|
32870
|
-
if (!entry || typeof entry !== "object") return null;
|
|
32871
|
-
const { role, content } = entry;
|
|
32872
|
-
if (role !== "user" && role !== "assistant") continue;
|
|
32873
|
-
let text = null;
|
|
32874
|
-
if (typeof content === "string") {
|
|
32875
|
-
text = content;
|
|
32876
|
-
} else if (Array.isArray(content)) {
|
|
32877
|
-
text = content.filter(
|
|
32878
|
-
(part) => !!part && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
32879
|
-
).map((part) => part.text).join("");
|
|
32880
|
-
}
|
|
32881
|
-
if (text === null) continue;
|
|
32882
|
-
messages.push({ role, content: text });
|
|
32883
|
-
}
|
|
32884
|
-
return messages.length > 0 ? messages : null;
|
|
32885
|
-
}
|
|
32886
32906
|
function errorJson(c, status, message) {
|
|
32887
32907
|
return c.json(
|
|
32888
32908
|
{ error: { message, type: status === 401 ? "authentication_error" : "invalid_request_error" } },
|
|
@@ -33017,17 +33037,11 @@ function privateRoutes(deps) {
|
|
|
33017
33037
|
app.use("/api/private/*", limitPrivate2());
|
|
33018
33038
|
app.post("/api/private/chat", async (c) => {
|
|
33019
33039
|
const body = await c.req.json().catch(() => ({}));
|
|
33020
|
-
|
|
33021
|
-
if (
|
|
33022
|
-
|
|
33023
|
-
if (messages.length === 0) {
|
|
33024
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
33025
|
-
}
|
|
33026
|
-
} else if (body?.message) {
|
|
33027
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
33028
|
-
} else {
|
|
33029
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
33040
|
+
const normalized = normalizeChatBody(body);
|
|
33041
|
+
if (!normalized.ok) {
|
|
33042
|
+
return c.json({ error: normalized.error }, 400);
|
|
33030
33043
|
}
|
|
33044
|
+
const { messages } = normalized;
|
|
33031
33045
|
const buckets = await resolveBuckets({
|
|
33032
33046
|
mode: "private",
|
|
33033
33047
|
sender: jwtSubject(c),
|
|
@@ -33085,17 +33099,11 @@ function publicRoutes(deps) {
|
|
|
33085
33099
|
app.use("/api/public/*", limitPublic2());
|
|
33086
33100
|
app.post("/api/public/chat", async (c) => {
|
|
33087
33101
|
const body = await c.req.json().catch(() => ({}));
|
|
33088
|
-
|
|
33089
|
-
if (
|
|
33090
|
-
|
|
33091
|
-
if (messages.length === 0) {
|
|
33092
|
-
return c.json({ error: "messages array cannot be empty" }, 400);
|
|
33093
|
-
}
|
|
33094
|
-
} else if (body?.message) {
|
|
33095
|
-
messages = [{ role: "user", content: String(body.message) }];
|
|
33096
|
-
} else {
|
|
33097
|
-
return c.json({ error: "either 'message' or 'messages' required" }, 400);
|
|
33102
|
+
const normalized = normalizeChatBody(body);
|
|
33103
|
+
if (!normalized.ok) {
|
|
33104
|
+
return c.json({ error: normalized.error }, 400);
|
|
33098
33105
|
}
|
|
33106
|
+
const { messages } = normalized;
|
|
33099
33107
|
const buckets = await resolveBuckets({ mode: "public", bucketsFor: config2.bucketsFor });
|
|
33100
33108
|
let system;
|
|
33101
33109
|
try {
|
|
@@ -33425,6 +33433,8 @@ export {
|
|
|
33425
33433
|
guardOutput,
|
|
33426
33434
|
jwtSubject,
|
|
33427
33435
|
loadKnowledge,
|
|
33436
|
+
normalizeChatBody,
|
|
33437
|
+
normalizeMessages,
|
|
33428
33438
|
openaiRoutes,
|
|
33429
33439
|
prepareChat,
|
|
33430
33440
|
privateRoutes,
|
|
@@ -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;
|
|
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;AAM5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAsHnD,wBAAgB,UAAU,CAAC,IAAI,EAAE,kBAAkB,8EA+GlD"}
|
package/dist/routes/openai.d.ts
CHANGED
|
@@ -22,13 +22,6 @@
|
|
|
22
22
|
* Bearer` or `x-api-key`; widget-only session/demo keys are not valid.
|
|
23
23
|
*/
|
|
24
24
|
import { Hono } from "hono";
|
|
25
|
-
import { type PipelineMessage } from "../core/pipeline";
|
|
26
25
|
import type { ServerDependencies } from "../types";
|
|
27
|
-
/**
|
|
28
|
-
* Normalize OpenAI-format messages to the pipeline format.
|
|
29
|
-
* Accepts string content and text content-part arrays; drops system/tool
|
|
30
|
-
* messages. Returns null when nothing usable remains.
|
|
31
|
-
*/
|
|
32
|
-
export declare function normalizeMessages(raw: unknown): PipelineMessage[] | null;
|
|
33
26
|
export declare function openaiRoutes(deps: ServerDependencies): Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
|
|
34
27
|
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -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;
|
|
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;AAS5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,8EAwIpD"}
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"private.d.ts","sourceRoot":"","sources":["../../src/routes/private.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAQ5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,aAAa,CAAC,IAAI,EAAE,kBAAkB,8EA4DrD"}
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"public.d.ts","sourceRoot":"","sources":["../../src/routes/public.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAU5B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASnD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB,8EAkEpD"}
|
package/package.json
CHANGED