@ykurilov/agent-chat-server 0.98.1 → 0.100.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/CHANGELOG.md +33 -0
- package/README.md +8 -0
- package/dist/index.js +410 -3
- package/dist/index.js.map +1 -1
- package/dist/types/browser-chat/codec.d.ts +10 -0
- package/dist/types/browser-chat/index.d.ts +4 -0
- package/dist/types/browser-chat/input.d.ts +5 -0
- package/dist/types/browser-chat/types.d.ts +52 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# @ykurilov/agent-chat-server
|
|
2
2
|
|
|
3
|
+
## 0.100.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2f96ad1: Автосжатие по умолчанию включено у всех агентов с порогом 250000 токенов (ранее 200000), но не выше 80% окна модели. Хвост/сводка остаются по 20000 токенов. Выключение только явным compaction: false.
|
|
8
|
+
|
|
9
|
+
### Как обновить приложение
|
|
10
|
+
|
|
11
|
+
Без настроек, с пустым compaction и undefined-полями действует новый порог. Собственный compaction.thresholdTokens сохраняет приоритет с прежним ограничением 80% окна. Явно закреплённые в приложении 200000 не меняются сами; для стандартного поведения убрать это переопределение.
|
|
12
|
+
- 79b85b8: Стандартный порядок провайдеров: DeepSeek API → OpenRouter → Fireworks → Together. Он согласован для нового выбора, меню и запасных. Модель по умолчанию — DeepSeek 4.1 Flash; первый запасной прямого DeepSeek — OpenRouter с deepseek/deepseek-v4.1-flash-1017. Для GLM без прямого DeepSeek первым становится OpenRouter.
|
|
13
|
+
|
|
14
|
+
### Как обновить приложение
|
|
15
|
+
|
|
16
|
+
Без явных настроек действует новый приоритет доступных провайдеров. Явные defaultChoice и providerFallback.byModel сохраняют приоритет; сохранённый доступный выбор чата не заменяется. Отключение запасных через false прежнее.
|
|
17
|
+
- Updated dependencies [2f96ad1]
|
|
18
|
+
- Updated dependencies [79b85b8]
|
|
19
|
+
- @ykurilov/agent-chat-core@0.100.0
|
|
20
|
+
|
|
21
|
+
## 0.99.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- 5e688b2: Добавлен opt-in чат с историей только в браузере: отдельный createBrowserChatHandler создаёт RAM-хранилище на запрос, а createBrowserChatTransport сохраняет owner-bound шифрованный снимок до показа потока. Обычный AgentChat/Одуванчик поддерживает текст, модели, Stop, ветки и серверные инструменты. После reload/обрыва показанный текст остаётся; продолжение явное. Обычные серверные чаты и idle watch по умолчанию не меняются.
|
|
26
|
+
|
|
27
|
+
### Как обновить приложение
|
|
28
|
+
|
|
29
|
+
Подключите createBrowserChatHandler из @ykurilov/agent-chat-server к отдельному POST endpoint, с resolveUser, серверными catalog/agent, постоянными namespace и snapshotKey (32 случайных байта). createBrowserChatTransport из @ykurilov/agent-chat-react получает endpoint/ownerId/chatId/loadSnapshot/saveSnapshot; передайте его chatProps в обычный AgentChat, вызывайте dispose при замене. Приложение сохраняет весь снимок атомарно в IndexedDB с revision guards. Не подключайте обычные attachments/voice/subject/appBridge: первая версия без файлов, голоса, памяти, helpers, widgets, browser RPC, фона и approval-инструментов. Биллинг/лимиты между запросами — внешние numeric callbacks, не transient ledger. Контракт и ограничения: docs/browser-owned-chat.md. Старым приложениям менять подключение не требуется.
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- Updated dependencies [5e688b2]
|
|
34
|
+
- @ykurilov/agent-chat-core@0.99.0
|
|
35
|
+
|
|
3
36
|
## 0.98.1
|
|
4
37
|
|
|
5
38
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# @ykurilov/agent-chat-server
|
|
2
2
|
|
|
3
|
+
Автосжатие истории включено по умолчанию у всех агентов: порог 250 000 токенов, но не выше 80% окна выбранной модели. Пропущенный `compaction`, `{}` и поля со значением `undefined` сохраняют это поведение; только явный `compaction: false` выключает сжатие. Приложение может задать свой `compaction.thresholdTokens`; хвост и потолок сводки по умолчанию остаются по 20 000 токенов.
|
|
4
|
+
|
|
5
|
+
Стандартный приоритет провайдеров: `deepseek` → `openrouter` → `fireworks` → `together`. Без явного `defaultChoice` выбирается DeepSeek 4.1 Flash через прямой DeepSeek API; если его ключа нет, используется первый доступный провайдер в этом порядке. Тот же порядок задаёт меню и стандартных запасных (для модели берутся только поддерживаемые провайдеры с ключами, выбранный пропускается). Первый запасной прямого DeepSeek — OpenRouter с `deepseek/deepseek-v4.1-flash-1017`. Явные `defaultChoice` и `providerFallback.byModel` имеют приоритет; `providerFallback: false` по-прежнему отключает запасных.
|
|
6
|
+
|
|
3
7
|
HTTP-адреса чата с агентом на стандартных `Request`/`Response` и подключения к Hono и Next.js.
|
|
4
8
|
|
|
5
9
|
```ts
|
|
@@ -121,3 +125,7 @@ await chat.flush({ waitForAnswersMs: 20_000 }); // при штатной ост
|
|
|
121
125
|
### Правка ответа в обычном чате (с 0.97.0)
|
|
122
126
|
|
|
123
127
|
Серверный `answerEditing: true` и право `chatCapabilities.answerEditing` включают адрес правки ответа для Next/Hono. В `AgentChat` со скином «Одуванчик» передайте `answerEditor={true}` либо объект с `renderFields({ data, setData, disabled, message })` для своих полей. `onAnswerEdited` получает id исходного и нового ответа, оба текста, владельца, чат и JSON `data` приложения. Прежний пользовательский `answerEditing={{ edits, renderEditor }}` совместим и имеет приоритет. [Контракт, пределы доставки хука и обновление приложения](../../docs/answer-editing.md).
|
|
128
|
+
|
|
129
|
+
## История только в браузере
|
|
130
|
+
|
|
131
|
+
`createBrowserChatHandler` из главного входа пакета — отдельный opt-in POST handler со свежим RAM-хранилищем на запрос и owner-bound шифрованным снимком. Не принимает ChatStorage/SQL, не подключается к обычным чатам. Настройки, лимиты, биллинг, отмена и граница первой версии: [browser-owned-chat.md](../../docs/browser-owned-chat.md). Инструменты с approval, файлы, голос, widgets и фон в этом режиме не поддержаны; обычный обработчик не меняется.
|
package/dist/index.js
CHANGED
|
@@ -842,10 +842,417 @@ function historyVersion(history) {
|
|
|
842
842
|
}
|
|
843
843
|
|
|
844
844
|
// src/index.ts
|
|
845
|
-
import { accessRefusedResponse as
|
|
845
|
+
import { accessRefusedResponse as accessRefusedResponse3, allowedUser as allowedUser3 } from "@ykurilov/agent-chat-core";
|
|
846
|
+
|
|
847
|
+
// src/browser-chat/index.ts
|
|
848
|
+
import "crypto";
|
|
849
|
+
import { ActionForbiddenError as ActionForbiddenError2, ChatBusyError as ChatBusyError2, ChatNotFoundError as ChatNotFoundError2, DuplicateMessageError as DuplicateMessageError2, InvalidMessageError as InvalidMessageError2, InvalidModelChoiceError as InvalidModelChoiceError2, NothingToRetryError as NothingToRetryError2, accessRefusedResponse as accessRefusedResponse2, allowedUser as allowedUser2, getTransientChatHistory, createChatService as createChatService2, createRequestOriginCheck as createRequestOriginCheck2, createTransientChatStorage, CURRENT_FORMAT_VERSION, loadCurrentBranch } from "@ykurilov/agent-chat-core";
|
|
850
|
+
|
|
851
|
+
// src/browser-chat/codec.ts
|
|
852
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
|
|
853
|
+
import { gzipSync, gunzipSync } from "zlib";
|
|
854
|
+
function createBrowserChatCodec(keyInput, namespace, maxBytes) {
|
|
855
|
+
const key = typeof keyInput === "string" ? Buffer.from(keyInput, "base64url") : Buffer.from(keyInput);
|
|
856
|
+
if (key.length !== 32 || !namespace || namespace.length > 200) throw new TypeError("browser chat requires a 32-byte snapshot key and a namespace");
|
|
857
|
+
const zip = (value) => {
|
|
858
|
+
const bytes = Buffer.from(JSON.stringify(value));
|
|
859
|
+
if (bytes.byteLength > maxBytes) throw new RangeError("browser chat snapshot limit exceeded");
|
|
860
|
+
return gzipSync(bytes, { level: 1 });
|
|
861
|
+
};
|
|
862
|
+
const unzip = (bytes) => JSON.parse(gunzipSync(bytes, { maxOutputLength: maxBytes }).toString("utf8"));
|
|
863
|
+
const aad = (ownerId, chatId, view) => Buffer.from(JSON.stringify([1, namespace, ownerId, chatId, view]));
|
|
864
|
+
return {
|
|
865
|
+
encode(ownerId, chatId, payload, history) {
|
|
866
|
+
const view = zip({ ...history, chat: { ...history.chat, id: chatId }, backgroundTasks: false }).toString("base64");
|
|
867
|
+
const iv = randomBytes(12);
|
|
868
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
869
|
+
cipher.setAAD(aad(ownerId, chatId, view));
|
|
870
|
+
const ciphertext = Buffer.concat([cipher.update(zip(payload)), cipher.final()]);
|
|
871
|
+
return { version: 1, ownerId, chatId, view, sealed: [iv, ciphertext, cipher.getAuthTag()].map((part) => part.toString("base64url")).join(".") };
|
|
872
|
+
},
|
|
873
|
+
decode(value, ownerId, chatId) {
|
|
874
|
+
if (!value || typeof value !== "object") throw new TypeError("invalid browser snapshot");
|
|
875
|
+
const snapshot = value;
|
|
876
|
+
if (snapshot.version !== 1 || snapshot.ownerId !== ownerId || snapshot.chatId !== chatId || typeof snapshot.view !== "string" || typeof snapshot.sealed !== "string" || snapshot.view.length > maxBytes * 2 || snapshot.sealed.length > maxBytes * 2) throw new TypeError("invalid browser snapshot");
|
|
877
|
+
const parts = snapshot.sealed.split(".");
|
|
878
|
+
if (parts.length !== 3 || parts.some((part) => !/^[a-zA-Z0-9_-]+$/.test(part))) throw new TypeError("invalid browser snapshot");
|
|
879
|
+
const [iv, ciphertext, tag] = parts.map((part) => Buffer.from(part, "base64url"));
|
|
880
|
+
if (iv.length !== 12 || tag.length !== 16) throw new TypeError("invalid browser snapshot");
|
|
881
|
+
const cipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
882
|
+
cipher.setAAD(aad(ownerId, chatId, snapshot.view));
|
|
883
|
+
cipher.setAuthTag(tag);
|
|
884
|
+
const payload = unzip(Buffer.concat([cipher.update(ciphertext), cipher.final()]));
|
|
885
|
+
if (payload.version !== 1 || typeof payload.internalChatId !== "string" || payload.storage?.version !== 1 || payload.storage.chats.length !== 1 || payload.storage.chats[0]?.id !== payload.internalChatId || payload.storage.chats[0]?.ownerId !== ownerId || payload.storage.messages.some((message) => message.chatId !== payload.internalChatId) || payload.storage.compactions.some((row) => row.chatId !== payload.internalChatId) || payload.storage.branchSelections.some((row) => row.chatId !== payload.internalChatId) || payload.storage.usageRecords.some((row) => row.ownerId !== ownerId || row.chatId !== payload.internalChatId)) throw new TypeError("invalid browser snapshot");
|
|
886
|
+
return payload;
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// src/browser-chat/input.ts
|
|
892
|
+
var identifier = (value) => typeof value === "string" && value.length > 0 && value.length <= 200 && !/[\p{Cc}]/u.test(value);
|
|
893
|
+
var object = (value) => Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
894
|
+
function plainUserMessage(value) {
|
|
895
|
+
if (!object(value) || !identifier(value.id) || value.role !== "user" || !Array.isArray(value.parts) || Object.keys(value).some((key) => !["id", "role", "parts"].includes(key)) || value.parts.length > 100) throw new TypeError("invalid browser message");
|
|
896
|
+
const parts = value.parts.map((part) => {
|
|
897
|
+
if (!object(part) || part.type !== "text" || typeof part.text !== "string" || part.text.length > 1e5) throw new TypeError("only text is supported in browser chat");
|
|
898
|
+
return { type: "text", text: part.text };
|
|
899
|
+
});
|
|
900
|
+
if (!parts.length || parts.reduce((sum, part) => sum + part.text.length, 0) > 1e5) throw new TypeError("invalid browser message");
|
|
901
|
+
return { id: value.id, role: "user", parts };
|
|
902
|
+
}
|
|
903
|
+
async function readBrowserRequest(request, maxBytes, signal) {
|
|
904
|
+
const reader = request.body?.getReader();
|
|
905
|
+
if (!reader || !request.headers.get("content-type")?.toLowerCase().startsWith("application/json")) throw new TypeError("invalid browser request");
|
|
906
|
+
const abort = () => {
|
|
907
|
+
void reader.cancel().catch(() => {
|
|
908
|
+
});
|
|
909
|
+
};
|
|
910
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
911
|
+
if (signal?.aborted) abort();
|
|
912
|
+
let size = 0;
|
|
913
|
+
const chunks = [];
|
|
914
|
+
try {
|
|
915
|
+
for (; ; ) {
|
|
916
|
+
const next = await reader.read();
|
|
917
|
+
if (next.done) break;
|
|
918
|
+
size += next.value.byteLength;
|
|
919
|
+
if (size > maxBytes) {
|
|
920
|
+
await reader.cancel();
|
|
921
|
+
throw new RangeError("request limit");
|
|
922
|
+
}
|
|
923
|
+
chunks.push(next.value);
|
|
924
|
+
}
|
|
925
|
+
} finally {
|
|
926
|
+
signal?.removeEventListener("abort", abort);
|
|
927
|
+
reader.releaseLock();
|
|
928
|
+
}
|
|
929
|
+
if (signal?.aborted) throw new DOMException("Aborted", "AbortError");
|
|
930
|
+
const bytes = new Uint8Array(size);
|
|
931
|
+
let at = 0;
|
|
932
|
+
for (const chunk of chunks) {
|
|
933
|
+
bytes.set(chunk, at);
|
|
934
|
+
at += chunk.byteLength;
|
|
935
|
+
}
|
|
936
|
+
const body = JSON.parse(new TextDecoder().decode(bytes));
|
|
937
|
+
if (!object(body) || !identifier(body.chatId) || !object(body.operation) || !("snapshot" in body) || Object.keys(body).some((key) => !["chatId", "snapshot", "operation"].includes(key))) throw new TypeError("invalid browser request");
|
|
938
|
+
const raw = body.operation;
|
|
939
|
+
let operation;
|
|
940
|
+
if (["bootstrap", "models", "retry"].includes(String(raw.type))) operation = { type: raw.type };
|
|
941
|
+
else if (raw.type === "message") operation = { type: "message", message: plainUserMessage(raw.message) };
|
|
942
|
+
else if (raw.type === "edit" && identifier(raw.messageId)) operation = { type: "edit", messageId: raw.messageId, message: plainUserMessage(raw.message) };
|
|
943
|
+
else if ((raw.type === "regenerate" || raw.type === "selectBranch") && identifier(raw.messageId)) operation = { type: raw.type, messageId: raw.messageId };
|
|
944
|
+
else throw new TypeError("invalid browser operation");
|
|
945
|
+
return { chatId: body.chatId, snapshot: body.snapshot, operation };
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// src/browser-chat/index.ts
|
|
949
|
+
var headers = { "cache-control": "no-store", "x-content-type-options": "nosniff" };
|
|
950
|
+
var errorResponse3 = (status, kind = "other") => Response.json({ error: { kind } }, { status, headers });
|
|
951
|
+
var abortable = (value, signal) => new Promise((resolve, reject) => {
|
|
952
|
+
const abort = () => reject(new DOMException("Aborted", "AbortError"));
|
|
953
|
+
if (signal.aborted) {
|
|
954
|
+
value.catch(() => {
|
|
955
|
+
});
|
|
956
|
+
abort();
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
960
|
+
value.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort)).catch(() => {
|
|
961
|
+
});
|
|
962
|
+
});
|
|
963
|
+
var bounded = (value, fallback, min, max) => {
|
|
964
|
+
const result = value ?? fallback;
|
|
965
|
+
if (!Number.isInteger(result) || result < min || result > max) throw new TypeError("Invalid browser chat resource limit");
|
|
966
|
+
return result;
|
|
967
|
+
};
|
|
968
|
+
function createBrowserChatHandler(options) {
|
|
969
|
+
const maxRequest = bounded(options.maxRequestBytes, 8 * 1024 * 1024, 4096, 64 * 1024 * 1024);
|
|
970
|
+
const maxSnapshot = bounded(options.maxSnapshotBytes, 32 * 1024 * 1024, 4096, 128 * 1024 * 1024);
|
|
971
|
+
const maxTurn = bounded(options.maxTurnMs, 18e4, 100, 9e5);
|
|
972
|
+
const interval = bounded(options.checkpointIntervalMs, 100, 10, 1e3);
|
|
973
|
+
const concurrency = bounded(options.maxConcurrentTurnsPerUser, 3, 1, 20);
|
|
974
|
+
const codec = createBrowserChatCodec(options.snapshotKey, options.namespace, maxSnapshot);
|
|
975
|
+
const allowsOrigin = createRequestOriginCheck2({ allowedOrigins: options.allowedOrigins });
|
|
976
|
+
const active = /* @__PURE__ */ new Set();
|
|
977
|
+
const owners = /* @__PURE__ */ new Map();
|
|
978
|
+
let closing = false;
|
|
979
|
+
const report = (code) => {
|
|
980
|
+
try {
|
|
981
|
+
options.onError?.(code);
|
|
982
|
+
} catch {
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
return {
|
|
986
|
+
get activeRequests() {
|
|
987
|
+
return active.size;
|
|
988
|
+
},
|
|
989
|
+
async close() {
|
|
990
|
+
closing = true;
|
|
991
|
+
for (const item of active) item.abort();
|
|
992
|
+
await Promise.allSettled([...active].map((item) => item.done));
|
|
993
|
+
},
|
|
994
|
+
async fetch(request) {
|
|
995
|
+
if (closing) return errorResponse3(503);
|
|
996
|
+
if (request.method !== "POST") return errorResponse3(405, "invalid");
|
|
997
|
+
if (!allowsOrigin(request)) return errorResponse3(403, "forbidden_origin");
|
|
998
|
+
const control = new AbortController();
|
|
999
|
+
let service, reader;
|
|
1000
|
+
let ownerId, counted = false, handedOff = false;
|
|
1001
|
+
let disposal;
|
|
1002
|
+
let stopping;
|
|
1003
|
+
const stopService = () => {
|
|
1004
|
+
if (service) stopping ??= service.flush().catch(() => {
|
|
1005
|
+
});
|
|
1006
|
+
return stopping;
|
|
1007
|
+
};
|
|
1008
|
+
let finish;
|
|
1009
|
+
const done = new Promise((resolve) => {
|
|
1010
|
+
finish = resolve;
|
|
1011
|
+
});
|
|
1012
|
+
const abort = () => {
|
|
1013
|
+
control.abort();
|
|
1014
|
+
void stopService();
|
|
1015
|
+
};
|
|
1016
|
+
const entry = { abort, done };
|
|
1017
|
+
active.add(entry);
|
|
1018
|
+
const timer = setTimeout(abort, maxTurn);
|
|
1019
|
+
request.signal.addEventListener("abort", abort, { once: true });
|
|
1020
|
+
if (request.signal.aborted) abort();
|
|
1021
|
+
const dispose = () => disposal ??= (async () => {
|
|
1022
|
+
control.abort();
|
|
1023
|
+
clearTimeout(timer);
|
|
1024
|
+
request.signal.removeEventListener("abort", abort);
|
|
1025
|
+
try {
|
|
1026
|
+
await reader?.cancel().catch(() => {
|
|
1027
|
+
});
|
|
1028
|
+
await stopService();
|
|
1029
|
+
} finally {
|
|
1030
|
+
if (counted && ownerId) {
|
|
1031
|
+
const next = (owners.get(ownerId) ?? 1) - 1;
|
|
1032
|
+
if (next) owners.set(ownerId, next);
|
|
1033
|
+
else owners.delete(ownerId);
|
|
1034
|
+
}
|
|
1035
|
+
active.delete(entry);
|
|
1036
|
+
finish();
|
|
1037
|
+
}
|
|
1038
|
+
})();
|
|
1039
|
+
let stage = "configuration";
|
|
1040
|
+
try {
|
|
1041
|
+
const resolved = await abortable(options.resolveUser(request), control.signal);
|
|
1042
|
+
const refusal = accessRefusedResponse2(resolved, headers);
|
|
1043
|
+
if (refusal) return refusal;
|
|
1044
|
+
const user = allowedUser2(resolved);
|
|
1045
|
+
if (!user) return errorResponse3(401, "invalid");
|
|
1046
|
+
ownerId = user.id;
|
|
1047
|
+
const input = await readBrowserRequest(request, maxRequest, control.signal);
|
|
1048
|
+
if (input.operation.type === "models" && input.snapshot !== null) return errorResponse3(400, "invalid");
|
|
1049
|
+
const settings = await abortable(Promise.resolve(options.configure(ownerId)), control.signal);
|
|
1050
|
+
for (const key of ["helpers", "browserTools", "widgets", "voices"]) if (key in settings.agent) throw new TypeError("Unsupported browser chat agent extension");
|
|
1051
|
+
const turn = !["bootstrap", "models", "selectBranch"].includes(input.operation.type);
|
|
1052
|
+
if (turn) {
|
|
1053
|
+
if ((owners.get(ownerId) ?? 0) >= concurrency) return errorResponse3(429, "too_many_turns");
|
|
1054
|
+
owners.set(ownerId, (owners.get(ownerId) ?? 0) + 1);
|
|
1055
|
+
counted = true;
|
|
1056
|
+
try {
|
|
1057
|
+
await abortable(settings.assertWithinLimit?.(ownerId, control.signal) ?? Promise.resolve(), control.signal);
|
|
1058
|
+
} catch {
|
|
1059
|
+
report("limit");
|
|
1060
|
+
return errorResponse3(429, "spending_limit_reached");
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
stage = "snapshot";
|
|
1064
|
+
const payload = input.snapshot === null ? null : codec.decode(input.snapshot, ownerId, input.chatId);
|
|
1065
|
+
const storage = createTransientChatStorage(payload?.storage);
|
|
1066
|
+
const insertUsage = storage.insertUsageRecord.bind(storage);
|
|
1067
|
+
let liveUsage = false;
|
|
1068
|
+
storage.insertUsageRecord = async (record) => {
|
|
1069
|
+
if (await storage.hasUsageRecord?.(record.id)) return;
|
|
1070
|
+
if (!liveUsage) {
|
|
1071
|
+
await insertUsage(record);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const { chatId: _chat, messageId: _message, ...usage } = record;
|
|
1075
|
+
const billing = new AbortController();
|
|
1076
|
+
const deadline = setTimeout(() => billing.abort(), 5e3);
|
|
1077
|
+
try {
|
|
1078
|
+
await abortable(settings.onUsage?.(usage, billing.signal) ?? Promise.resolve(), billing.signal);
|
|
1079
|
+
} catch (error) {
|
|
1080
|
+
report("usage");
|
|
1081
|
+
throw error;
|
|
1082
|
+
} finally {
|
|
1083
|
+
clearTimeout(deadline);
|
|
1084
|
+
}
|
|
1085
|
+
await insertUsage(record);
|
|
1086
|
+
};
|
|
1087
|
+
const catalog = { ...settings.catalog, providers: settings.catalog.providers.map(({ generationUsage: _later, ...provider }) => provider) };
|
|
1088
|
+
service = createChatService2({
|
|
1089
|
+
storage,
|
|
1090
|
+
catalog,
|
|
1091
|
+
agent: { ...settings.agent, tools: checkedTools(settings.agent.tools), cacheMissLog: false },
|
|
1092
|
+
...settings.defaultChoice && { defaultChoice: settings.defaultChoice },
|
|
1093
|
+
...settings.env && { env: settings.env },
|
|
1094
|
+
...settings.compaction !== void 0 && { compaction: settings.compaction },
|
|
1095
|
+
logError: () => {
|
|
1096
|
+
},
|
|
1097
|
+
chatCapabilities: () => ({ ...settings.capabilities, attachments: false, voice: false, steer: false, chatList: false, compact: false, answerEditing: false, errorDetails: false, appNotices: false })
|
|
1098
|
+
});
|
|
1099
|
+
const current = service;
|
|
1100
|
+
if (input.operation.type === "models") {
|
|
1101
|
+
if (input.snapshot !== null) return errorResponse3(400, "invalid");
|
|
1102
|
+
const menu = await abortable(current.getModelMenu(ownerId), control.signal);
|
|
1103
|
+
return Response.json({ type: "models", data: menu }, { headers });
|
|
1104
|
+
}
|
|
1105
|
+
const internalId = payload?.internalChatId ?? (await current.createChat(ownerId)).id;
|
|
1106
|
+
await current.recoverInterruptedAnswers({ soleServer: true });
|
|
1107
|
+
liveUsage = true;
|
|
1108
|
+
if (control.signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
1109
|
+
const snapshot = async () => {
|
|
1110
|
+
const history = await getTransientChatHistory(current, ownerId, internalId);
|
|
1111
|
+
const state = storage.exportSnapshot();
|
|
1112
|
+
const value = codec.encode(ownerId, input.chatId, { version: 1, internalChatId: internalId, storage: state }, history);
|
|
1113
|
+
if (JSON.stringify(value).length > maxRequest - 1024) throw new RangeError("browser chat snapshot request limit");
|
|
1114
|
+
const pendingIds = new Set(input.snapshot?.pending?.map((row) => row.id) ?? []);
|
|
1115
|
+
return { snapshot: value, acknowledged: state.messages.filter((row) => row.role === "user" && pendingIds.has(row.id)).map((row) => row.id) };
|
|
1116
|
+
};
|
|
1117
|
+
stage = "turn";
|
|
1118
|
+
if (input.snapshot?.choice) await current.setModel(ownerId, internalId, checkedChoice(input.snapshot.choice));
|
|
1119
|
+
await recoverPending(storage, internalId, input.snapshot, input.operation);
|
|
1120
|
+
let stream;
|
|
1121
|
+
switch (input.operation.type) {
|
|
1122
|
+
case "bootstrap":
|
|
1123
|
+
break;
|
|
1124
|
+
case "message":
|
|
1125
|
+
stream = await current.sendMessage(ownerId, internalId, input.operation.message);
|
|
1126
|
+
break;
|
|
1127
|
+
case "retry":
|
|
1128
|
+
stream = await current.retry(ownerId, internalId);
|
|
1129
|
+
break;
|
|
1130
|
+
case "regenerate":
|
|
1131
|
+
stream = await current.regenerate(ownerId, internalId, input.operation.messageId);
|
|
1132
|
+
break;
|
|
1133
|
+
case "edit":
|
|
1134
|
+
stream = await current.editMessage(ownerId, internalId, input.operation.messageId, input.operation.message);
|
|
1135
|
+
break;
|
|
1136
|
+
case "selectBranch":
|
|
1137
|
+
await current.selectBranch(ownerId, internalId, input.operation.messageId);
|
|
1138
|
+
break;
|
|
1139
|
+
}
|
|
1140
|
+
if (!stream) return Response.json({ type: "result", ...await snapshot() }, { headers });
|
|
1141
|
+
reader = stream.getReader();
|
|
1142
|
+
handedOff = true;
|
|
1143
|
+
const encoder = new TextEncoder();
|
|
1144
|
+
const body = new ReadableStream({
|
|
1145
|
+
async start(out) {
|
|
1146
|
+
const emit = (value) => {
|
|
1147
|
+
if (control.signal.aborted) return;
|
|
1148
|
+
if ((out.desiredSize ?? 0) <= 0) throw new RangeError("Browser chat consumer is too slow");
|
|
1149
|
+
out.enqueue(encoder.encode(JSON.stringify(value) + "\n"));
|
|
1150
|
+
};
|
|
1151
|
+
let pending;
|
|
1152
|
+
try {
|
|
1153
|
+
emit({ type: "ready", ...await snapshot() });
|
|
1154
|
+
let chunks = [], ended = false, batchAt = Date.now();
|
|
1155
|
+
while (!ended && !control.signal.aborted) {
|
|
1156
|
+
pending ??= reader.read();
|
|
1157
|
+
let tick;
|
|
1158
|
+
const next = await Promise.race([pending, new Promise((resolve) => {
|
|
1159
|
+
tick = setTimeout(() => resolve(null), chunks.length ? Math.max(1, batchAt + interval - Date.now()) : 1e3);
|
|
1160
|
+
})]);
|
|
1161
|
+
clearTimeout(tick);
|
|
1162
|
+
if (next !== null) {
|
|
1163
|
+
pending = void 0;
|
|
1164
|
+
if (next.done) ended = true;
|
|
1165
|
+
else chunks.push(next.value);
|
|
1166
|
+
}
|
|
1167
|
+
if (chunks.length && (ended || next === null || Date.now() - batchAt >= interval || chunks.length >= 256)) {
|
|
1168
|
+
emit({ type: "data", chunks, ...await snapshot() });
|
|
1169
|
+
chunks = [];
|
|
1170
|
+
batchAt = Date.now();
|
|
1171
|
+
} else if (next === null) emit({ type: "ping" });
|
|
1172
|
+
}
|
|
1173
|
+
if (control.signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
1174
|
+
await current.flush({ waitForAnswersMs: 5e3 });
|
|
1175
|
+
emit({ type: "end", ...await snapshot() });
|
|
1176
|
+
await dispose();
|
|
1177
|
+
out.close();
|
|
1178
|
+
} catch {
|
|
1179
|
+
report("turn");
|
|
1180
|
+
await dispose();
|
|
1181
|
+
try {
|
|
1182
|
+
out.error(new Error("Browser chat interrupted"));
|
|
1183
|
+
} catch {
|
|
1184
|
+
}
|
|
1185
|
+
} finally {
|
|
1186
|
+
pending?.catch(() => {
|
|
1187
|
+
});
|
|
1188
|
+
await dispose();
|
|
1189
|
+
}
|
|
1190
|
+
},
|
|
1191
|
+
async cancel() {
|
|
1192
|
+
await dispose();
|
|
1193
|
+
}
|
|
1194
|
+
}, { highWaterMark: maxRequest * 2, size: (chunk) => chunk.byteLength });
|
|
1195
|
+
return new Response(body, { headers: { ...headers, "content-type": "application/x-ndjson", "x-accel-buffering": "no" } });
|
|
1196
|
+
} catch (error) {
|
|
1197
|
+
report(stage);
|
|
1198
|
+
if (error instanceof ActionForbiddenError2) return errorResponse3(403, "action_forbidden");
|
|
1199
|
+
if (error instanceof ChatNotFoundError2) return errorResponse3(404, "not_found");
|
|
1200
|
+
if (error instanceof ChatBusyError2) return errorResponse3(409, "busy");
|
|
1201
|
+
if (error instanceof DuplicateMessageError2 || error instanceof NothingToRetryError2) return errorResponse3(409, "invalid");
|
|
1202
|
+
if (error instanceof InvalidMessageError2 || error instanceof InvalidModelChoiceError2) return errorResponse3(400, "invalid");
|
|
1203
|
+
return errorResponse3(error instanceof RangeError ? 413 : stage === "snapshot" || error instanceof TypeError ? 400 : 500, error instanceof RangeError ? "too_large" : "other");
|
|
1204
|
+
} finally {
|
|
1205
|
+
if (!handedOff) await dispose();
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
function checkedChoice(value) {
|
|
1211
|
+
if (!value || !identifier(value.modelId) || !identifier(value.providerId) || value.reasoning !== void 0 && !identifier(value.reasoning)) throw new TypeError("Invalid browser model choice");
|
|
1212
|
+
return { modelId: value.modelId, providerId: value.providerId, ...value.reasoning !== void 0 && { reasoning: value.reasoning } };
|
|
1213
|
+
}
|
|
1214
|
+
async function recoverPending(storage, chatId, snapshot, operation) {
|
|
1215
|
+
const pending = snapshot?.pending ?? [];
|
|
1216
|
+
if (!Array.isArray(pending) || pending.length > 50) throw new TypeError("Invalid recovered messages");
|
|
1217
|
+
for (const message of pending) {
|
|
1218
|
+
if (!message || !identifier(message.id) || typeof message.text !== "string" || message.text.length > 1e5) throw new TypeError("Invalid recovered message");
|
|
1219
|
+
if ((operation.type === "message" || operation.type === "edit") && operation.message.id === message.id) continue;
|
|
1220
|
+
if (message.editedMessageId !== void 0) throw new TypeError("Retry the pending edit before another operation");
|
|
1221
|
+
const existing = await storage.getMessage(message.id);
|
|
1222
|
+
if (existing) {
|
|
1223
|
+
if (existing.role !== "user" || existing.parts.some((part) => part.type !== "text") || existing.parts.map((part) => part.type === "text" ? part.text : "").join("") !== message.text) throw new TypeError("Recovered message collision");
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
const branch = await loadCurrentBranch(storage, chatId);
|
|
1227
|
+
const at = new Date(Math.max(Date.now(), ...branch.map((row) => Date.parse(row.createdAt) + 1))).toISOString();
|
|
1228
|
+
await storage.insertMessage({
|
|
1229
|
+
id: message.id,
|
|
1230
|
+
chatId,
|
|
1231
|
+
parentId: branch.at(-1)?.id ?? null,
|
|
1232
|
+
role: "user",
|
|
1233
|
+
parts: [{ type: "text", text: message.text }],
|
|
1234
|
+
formatVersion: CURRENT_FORMAT_VERSION,
|
|
1235
|
+
status: "complete",
|
|
1236
|
+
modelId: null,
|
|
1237
|
+
providerId: null,
|
|
1238
|
+
usage: null,
|
|
1239
|
+
reasoningDurationsMs: {},
|
|
1240
|
+
error: null,
|
|
1241
|
+
createdAt: at
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
function checkedTools(source) {
|
|
1246
|
+
const check = (tools) => {
|
|
1247
|
+
if (Object.values(tools).some((tool) => tool.needsApproval)) throw new TypeError("Approval tools are not supported in browser chat");
|
|
1248
|
+
return tools;
|
|
1249
|
+
};
|
|
1250
|
+
return typeof source === "function" ? (model) => check(source(model)) : source ? check(source) : void 0;
|
|
1251
|
+
}
|
|
846
1252
|
export {
|
|
847
|
-
|
|
848
|
-
|
|
1253
|
+
accessRefusedResponse3 as accessRefusedResponse,
|
|
1254
|
+
allowedUser3 as allowedUser,
|
|
1255
|
+
createBrowserChatHandler,
|
|
849
1256
|
createChatHandler,
|
|
850
1257
|
createChatServer,
|
|
851
1258
|
turnRefusalResponse
|