@vinaverse/client 0.1.0-alpha.11 → 0.1.0-alpha.13
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/README.md +3 -1
- package/dist/agent/index.cjs +187 -24
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +3 -3
- package/dist/agent/index.d.ts +3 -3
- package/dist/agent/index.js +3 -3
- package/dist/backend/index.cjs +3 -0
- package/dist/backend/index.cjs.map +1 -1
- package/dist/backend/index.d.cts +3 -3
- package/dist/backend/index.d.ts +3 -3
- package/dist/backend/index.js +6 -3
- package/dist/backend/index.js.map +1 -1
- package/dist/{chunk-46D3FSLU.js → chunk-5HKZB2CJ.js} +14 -6
- package/dist/chunk-5HKZB2CJ.js.map +1 -0
- package/dist/{chunk-GWLLUOFY.js → chunk-LAB2FPXF.js} +31 -8
- package/dist/chunk-LAB2FPXF.js.map +1 -0
- package/dist/{chunk-JI6HUXH3.js → chunk-WOMAGTI5.js} +149 -20
- package/dist/chunk-WOMAGTI5.js.map +1 -0
- package/dist/conversation/index.cjs +40 -9
- package/dist/conversation/index.cjs.map +1 -1
- package/dist/conversation/index.d.cts +4 -4
- package/dist/conversation/index.d.ts +4 -4
- package/dist/conversation/index.js +3 -3
- package/dist/history/index.cjs +7 -1
- package/dist/history/index.cjs.map +1 -1
- package/dist/history/index.d.cts +10 -4
- package/dist/history/index.d.ts +10 -4
- package/dist/history/index.js +10 -5
- package/dist/history/index.js.map +1 -1
- package/dist/index.cjs +29 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{runtime-CNgz2_8G.d.ts → runtime-44JlV32O.d.ts} +92 -14
- package/dist/{runtime-6O0qIPmo.d.cts → runtime-DWiB-XHX.d.cts} +92 -14
- package/dist/{session-D0ijOQX3.d.ts → session-lPGrKdhf.d.cts} +5 -0
- package/dist/{session-D0ijOQX3.d.cts → session-lPGrKdhf.d.ts} +5 -0
- package/dist/{store-CGRSxVcL.d.ts → store-Cjw9YmXA.d.ts} +6 -1
- package/dist/{store-CxoHwd64.d.cts → store-D-an5vch.d.cts} +6 -1
- package/dist/testing/index.cjs.map +1 -1
- package/dist/testing/index.d.cts +3 -3
- package/dist/testing/index.d.ts +3 -3
- package/dist/testing/index.js +3 -3
- package/package.json +2 -2
- package/dist/chunk-46D3FSLU.js.map +0 -1
- package/dist/chunk-GWLLUOFY.js.map +0 -1
- package/dist/chunk-JI6HUXH3.js.map +0 -1
package/dist/history/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isRefusal } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
3
|
-
import '../chunk-
|
|
1
|
+
import { isRefusal } from '../chunk-WOMAGTI5.js';
|
|
2
|
+
import '../chunk-5HKZB2CJ.js';
|
|
3
|
+
import '../chunk-LAB2FPXF.js';
|
|
4
4
|
|
|
5
5
|
// src/history/index.ts
|
|
6
6
|
function continueKey(conversation, subject, thread) {
|
|
@@ -11,6 +11,10 @@ function continueKey(conversation, subject, thread) {
|
|
|
11
11
|
...thread ? { thread } : {}
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
+
function summaryTitle(conversation, own) {
|
|
15
|
+
const anchor = conversation.anchor;
|
|
16
|
+
return typeof anchor?.title === "string" && anchor.title || own || conversation.title;
|
|
17
|
+
}
|
|
14
18
|
function historyItems(data) {
|
|
15
19
|
return data.map((item, seq) => {
|
|
16
20
|
if (item.type === "card") {
|
|
@@ -39,7 +43,8 @@ function historyItems(data) {
|
|
|
39
43
|
text: item.text,
|
|
40
44
|
state: "sent",
|
|
41
45
|
...item.interrupted ? { partial: "interrupted" } : {},
|
|
42
|
-
...item.refusal ? { refusal: true } : {}
|
|
46
|
+
...item.refusal ? { refusal: true } : {},
|
|
47
|
+
at: item.at
|
|
43
48
|
};
|
|
44
49
|
return message;
|
|
45
50
|
});
|
|
@@ -256,6 +261,6 @@ function createAgentTranscript(options) {
|
|
|
256
261
|
};
|
|
257
262
|
}
|
|
258
263
|
|
|
259
|
-
export { continueKey, createAgentHistory, createAgentTranscript, historyItems };
|
|
264
|
+
export { continueKey, createAgentHistory, createAgentTranscript, historyItems, summaryTitle };
|
|
260
265
|
//# sourceMappingURL=index.js.map
|
|
261
266
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/history/index.ts"],"names":[],"mappings":";;;;;AA0BO,SAAS,WAAA,CACd,YAAA,EACA,OAAA,EACA,MAAA,EACU;AACV,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA,MAAM,YAAA,CAAa,eAAA;AAAA,IACnB,KAAA,EAAO,aAAa,iBAAA,IAAqB,IAAA;AAAA,IACzC,GAAI,MAAA,GAAS,EAAE,MAAA,KAAW;AAAC,GAC7B;AACF;AAOO,SAAS,aAAa,IAAA,EAA8C;AACzE,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,CAAC,IAAA,EAAM,GAAA,KAAsB;AAC3C,IAAA,IAAI,IAAA,CAAK,SAAS,MAAA,EAAQ;AACxB,MAAA,MAAM,UAAU,IAAA,CAAK,gBAAA;AACrB,MAAA,MAAM,IAAA,GAAiB;AAAA,QACrB,IAAA,EAAM,MAAA;AAAA,QACN,GAAA,EAAK,KAAK,IAAA,CAAK,EAAA;AAAA,QACf,IAAA,EAAM,KAAA;AAAA,QACN,QAAA,EAAU,IAAA;AAAA,QACV,IAAA,EAAM;AAAA,UACJ,EAAA,EAAI,KAAK,IAAA,CAAK,EAAA;AAAA,UACd,IAAA,EAAM,KAAK,IAAA,CAAK,IAAA;AAAA,UAChB,MAAM,IAAA,CAAK,IAAA;AAAA,UACX,QAAQ,EAAC;AAAA,UACT,QAAQ,IAAA,CAAK,OAAA;AAAA,UACb,OAAA,EAAS,OAAA,IAAW,OAAA,KAAY,SAAA,GAAY,OAAA,GAAU,IAAA;AAAA,UACtD;AAAA;A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,EAAG,GAAG,IAAI,CAAA;AAAA,UAC3D,OAAA,EAAS,KAAA;AAAA,UACT,OAAA,EAAS,IAAA;AAAA,UACT,MAAM,MAAA,KAAW;AAAA,SAClB,CAAA;AAAA,MACH,CAAA;AAAA,MACA,CAAC,KAAA,KAAU;AAET,QAAA,IAAI,SAAS,IAAA,IAAQ,WAAA,CAAY,KAAK,CAAA,IAAK,CAAC,SAAA,EAAW;AACrD,UAAA,SAAA,GAAY,IAAA;AACZ,UAAA,MAAA,GAAS,IAAA;AACT,UAAA,IAAA,CAAK,IAAI,CAAA;AACT,UAAA;AAAA,QACF;AACA,QAAA,KAAA,CAAM,GAAA,CAAI,EAAE,OAAA,EAAS,KAAA,EAAO,SAAS,QAAA,CAAS,KAAK,GAAG,CAAA;AAAA,MACxD;AAAA,KACF;AAAA,EACF,CAAA;AACA,EAAA,MAAM,OAAO,MAAM;AACjB,IAAA,MAAA,GAAS,IAAA;AACT,IAAA,SAAA,GAAY,KAAA;AACZ,IAAA,KAAA,CAAM,GAAA,CAAI,EAAE,OAAA,EAAS,IAAA,EAAM,CAAA;AAC3B,IAAA,IAAA,CAAK,IAAI,CAAA;AAAA,EACX,CAAA;AACA,EAAA,MAAM,cAAc,MAAM;AACxB,IAAA,MAAM,CAAA,GAAI,MAAM,GAAA,EAAI;AACpB,IAAA,IAAI,CAAC,EAAE,OAAA,IAAW,CAAA,CAAE,WAAW,CAAA,CAAE,IAAA,IAAQ,WAAW,IAAA,EAAM;AAC1D,IAAA,IAAA,CAAK,MAAM,CAAA;AAAA,EACb,CAAA;AACA,EAAA,OAAO;AAAA,IACL,aAAa,KAAA,CAAM,GAAA;AAAA,IACnB,WAAW,KAAA,CAAM,SAAA;AAAA,IACjB,IAAA;AAAA,IACA,WAAA;AAAA,IACA,OAAO,MAAO,MAAA,KAAW,IAAA,GAAO,IAAA,KAAS,WAAA;AAAY,GACvD;AACF","file":"index.js","sourcesContent":["// @vinaverse/client/history: a person's past conversations with the agent (C2), through the tenant's backend\n// (`AgentBackend.listConversations`, `readConversation`): the list, newest activity first, a page at a time; a past\n// conversation's transcript, newest page first, earlier pages prepended; delete through the runtime. No React: the\n// kits bind to it. Nothing begun before a sign-out lands after it (the runtime's `epoch`).\nimport {\n type AgentBackend,\n type AgentFailureReason,\n type AgentKey,\n type AgentRuntime,\n isRefusal,\n} from '../agent/runtime.js';\nimport type {\n CardItem,\n MessageItem,\n TimelineItem,\n} from '../conversation/view.js';\nimport type { Schemas } from '../events.js';\n\nexport type ConversationSummary = Schemas['ConversationSummary'];\ntype HistoryItem = Schemas['TimelineItem'];\n\n/**\n * The key a past conversation continues under for this person: its side and business as it was bound when it started\n * (`experience_mode`, `business_scope_id`), the `thread` given (default `chat`). For `runtime.continue(key, id)` and\n * `deleteConversation(key, id)`, so the device forgets it only where that key still continues it.\n */\nexport function continueKey(\n conversation: ConversationSummary,\n subject: string | null,\n thread?: string,\n): AgentKey {\n return {\n subject,\n mode: conversation.experience_mode,\n scope: conversation.business_scope_id ?? null,\n ...(thread ? { thread } : {}),\n };\n}\n\n/**\n * A past conversation's items as the kits draw them: messages as sent (an `assistant` reply is the agent's, cut-off\n * replies and refusals marked), cards restored (drawn as they were, taking no answer or action), a proposal with the\n * outcome it had. Keyed by the server's ids, so pages merge without doubles.\n */\nexport function historyItems(data: readonly HistoryItem[]): TimelineItem[] {\n return data.map((item, seq): TimelineItem => {\n if (item.type === 'card') {\n const outcome = item.proposal_outcome;\n const card: CardItem = {\n type: 'card',\n key: item.card.id,\n busy: false,\n restored: true,\n card: {\n id: item.card.id,\n kind: item.card.kind,\n card: item.card as unknown as Readonly<Record<string, unknown>>,\n locked: [],\n turnId: item.turn_id,\n outcome: outcome && outcome !== 'pending' ? outcome : null,\n seq,\n },\n };\n return card;\n }\n const message: MessageItem = {\n type: 'message',\n key: `h:${item.id}`,\n role: item.role === 'assistant' ? 'agent' : item.role,\n text: item.text,\n state: 'sent',\n ...(item.interrupted ? { partial: 'interrupted' as const } : {}),\n ...(item.refusal ? { refusal: true as const } : {}),\n };\n return message;\n });\n}\n\n/** Why a page could not be read, as the runtime says it. */\nfunction reasonOf(error: unknown): AgentFailureReason {\n if (isRefusal(error)) return error.reason;\n const given = (error as { reason?: unknown } | null)?.reason;\n return typeof given === 'string' ? (given as AgentFailureReason) : 'unknown';\n}\n\n/**\n * The cursor restart (`vinaverse.agent-backend/1`): a page asked with a cursor, refused `unavailable`, not retryable,\n * with the gateway's `invalid_request`, had a cursor the gateway no longer takes (stale, or of other filters). Asked\n * again from the first page, once. Any other refusal is said as it is.\n */\nfunction staleCursor(error: unknown): boolean {\n const e = error as {\n outcome?: unknown;\n gatewayCode?: unknown;\n retryable?: unknown;\n } | null;\n return (\n e?.outcome === 'unavailable' &&\n e.gatewayCode === 'invalid_request' &&\n e.retryable === false\n );\n}\n\n/** A paged store: one page read at a time, dropped when it lands after a newer read or a sign-out. */\nfunction paged<S extends { readonly loading: boolean }>(\n runtime: AgentRuntime,\n empty: S,\n) {\n let state = empty;\n let epoch = runtime.epoch();\n let asked = 0;\n const listeners = new Set<() => void>();\n const set = (next: Partial<S>) => {\n state = { ...state, ...next };\n for (const l of [...listeners]) l();\n };\n // A sign-out empties what the last person had, as soon as anyone listens again.\n const fresh = () => {\n if (runtime.epoch() === epoch) return;\n epoch = runtime.epoch();\n asked++;\n state = empty;\n };\n let off: (() => void) | null = null;\n return {\n get: () => {\n fresh();\n return state;\n },\n set,\n subscribe(listener: () => void) {\n listeners.add(listener);\n off ??= runtime.subscribe(() => {\n const before = state;\n fresh();\n if (state !== before) for (const l of [...listeners]) l();\n });\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n off?.();\n off = null;\n }\n };\n },\n /** Reads a page; `apply` lands only for the newest read, under the same person. */\n read<T>(\n call: () => Promise<T>,\n apply: (value: T) => void,\n failed: (error: unknown) => void,\n ) {\n fresh();\n const mine = ++asked;\n const under = epoch;\n set({ loading: true } as Partial<S>);\n call().then(\n (value) => {\n if (mine === asked && under === runtime.epoch()) apply(value);\n },\n (error: unknown) => {\n if (mine === asked && under === runtime.epoch()) failed(error);\n },\n );\n },\n };\n}\n\nexport interface AgentHistoryState {\n readonly conversations: readonly ConversationSummary[];\n /** A page is being read. */\n readonly loading: boolean;\n /** Why the last page could not be read; null when it was. `off` when the backend serves no history. */\n readonly failure: AgentFailureReason | null;\n /** Every page is read. */\n readonly done: boolean;\n /** The first page was asked for. */\n readonly started: boolean;\n}\n\nexport interface AgentHistory {\n getSnapshot(): AgentHistoryState;\n subscribe(listener: () => void): () => void;\n /** Reads the first page anew: the first time, after a failure, or to see what changed. */\n refresh(): void;\n /** The next older page, when there is one and none is being read. */\n loadMore(): void;\n /** Reads again the page that failed: the first (as `refresh`), or the next. */\n retry(): void;\n /**\n * Deletes a past conversation (the runtime's `deleteConversation` for the key that holds it, so the device forgets\n * it only where the key still continues it) and drops its row. One already gone (`not_found`) is dropped too.\n */\n remove(key: AgentKey, conversationId: string): Promise<void>;\n}\n\nexport interface AgentHistoryOptions {\n readonly runtime: AgentRuntime;\n /** Default: the runtime's. */\n readonly backend?: AgentBackend;\n /** The app's side: `customer` or `business`. */\n readonly mode: string;\n /** The business acted for, with `business`. */\n readonly scope?: string | null;\n /** The agent, for a backend that serves more than one. */\n readonly agent?: string | null;\n /** Conversations per page (1–50). Default 20. */\n readonly pageSize?: number;\n}\n\nconst NO_HISTORY: AgentHistoryState = {\n conversations: [],\n loading: false,\n failure: null,\n done: false,\n started: false,\n};\n\n/** A person's past conversations, newest activity first. */\nexport function createAgentHistory(options: AgentHistoryOptions): AgentHistory {\n const { runtime } = options;\n const backend = options.backend ?? runtime.backend;\n const store = paged(runtime, NO_HISTORY);\n let cursor: string | null = null;\n let restarted = false;\n const page = (from: string | null) => {\n const list = backend.listConversations?.bind(backend);\n if (!list) {\n store.set({ started: true, failure: 'off', done: true });\n return;\n }\n store.read(\n () =>\n list({\n mode: options.mode,\n scope: options.scope ?? null,\n agent: options.agent ?? null,\n limit: options.pageSize ?? 20,\n cursor: from,\n }),\n (got) => {\n const known = new Set(\n from ? store.get().conversations.map((c) => c.id) : [],\n );\n cursor = got.next_cursor;\n store.set({\n conversations: [\n ...(from ? store.get().conversations : []),\n ...got.data.filter((c) => !known.has(c.id)),\n ],\n loading: false,\n failure: null,\n done: cursor === null,\n });\n },\n (error) => {\n if (from !== null && staleCursor(error) && !restarted) {\n restarted = true;\n cursor = null;\n page(null);\n return;\n }\n store.set({ loading: false, failure: reasonOf(error) });\n },\n );\n };\n const refresh = () => {\n cursor = null;\n restarted = false;\n store.set({ started: true });\n page(null);\n };\n const loadMore = () => {\n const s = store.get();\n if (!s.started || s.loading || s.done || cursor === null) return;\n page(cursor);\n };\n return {\n getSnapshot: store.get,\n subscribe: store.subscribe,\n refresh,\n loadMore,\n retry: () => (cursor === null ? refresh() : loadMore()),\n async remove(key, conversationId) {\n try {\n await runtime.deleteConversation(key, conversationId);\n } catch (error) {\n if (!(isRefusal(error) && error.reason === 'not_found')) throw error;\n }\n store.set({\n conversations: store\n .get()\n .conversations.filter((c) => c.id !== conversationId),\n });\n },\n };\n}\n\nexport interface AgentTranscriptState {\n /** The conversation's items, oldest first, as the kits draw them (`historyItems`). */\n readonly items: readonly TimelineItem[];\n readonly loading: boolean;\n /** Why the last page could not be read: `not_found` (deleted, or never theirs), `expired`, … */\n readonly failure: AgentFailureReason | null;\n /** Nothing earlier to read. */\n readonly done: boolean;\n readonly started: boolean;\n}\n\nexport interface AgentTranscript {\n getSnapshot(): AgentTranscriptState;\n subscribe(listener: () => void): () => void;\n /** Reads the newest page anew. */\n load(): void;\n /** The page before the earliest read, prepended. */\n loadEarlier(): void;\n /** Reads again the page that failed: the newest (as `load`), or the earlier one. */\n retry(): void;\n}\n\nexport interface AgentTranscriptOptions {\n readonly runtime: AgentRuntime;\n /** Default: the runtime's. */\n readonly backend?: AgentBackend;\n readonly conversationId: string;\n /** Items per page. Default 30. */\n readonly pageSize?: number;\n}\n\nconst NO_TRANSCRIPT: AgentTranscriptState = {\n items: [],\n loading: false,\n failure: null,\n done: false,\n started: false,\n};\n\n/** A past conversation's transcript, read-only: newest page first, earlier pages prepended. */\nexport function createAgentTranscript(\n options: AgentTranscriptOptions,\n): AgentTranscript {\n const { runtime } = options;\n const backend = options.backend ?? runtime.backend;\n const store = paged(runtime, NO_TRANSCRIPT);\n let cursor: string | null = null;\n let restarted = false;\n const page = (from: string | null) => {\n const read = backend.readConversation?.bind(backend);\n if (!read) {\n store.set({ started: true, failure: 'off', done: true });\n return;\n }\n store.read(\n () =>\n read(options.conversationId, {\n limit: options.pageSize ?? 30,\n cursor: from,\n }),\n (got) => {\n const earlier = historyItems(got.data);\n const held = from ? store.get().items : [];\n const keys = new Set(held.map((i) => i.key));\n cursor = got.next_cursor;\n store.set({\n items: [...earlier.filter((i) => !keys.has(i.key)), ...held],\n loading: false,\n failure: null,\n done: cursor === null,\n });\n },\n (error) => {\n // An earlier page's cursor went stale: read back from the newest page, once.\n if (from !== null && staleCursor(error) && !restarted) {\n restarted = true;\n cursor = null;\n page(null);\n return;\n }\n store.set({ loading: false, failure: reasonOf(error) });\n },\n );\n };\n const load = () => {\n cursor = null;\n restarted = false;\n store.set({ started: true });\n page(null);\n };\n const loadEarlier = () => {\n const s = store.get();\n if (!s.started || s.loading || s.done || cursor === null) return;\n page(cursor);\n };\n return {\n getSnapshot: store.get,\n subscribe: store.subscribe,\n load,\n loadEarlier,\n retry: () => (cursor === null ? load() : loadEarlier()),\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/history/index.ts"],"names":[],"mappings":";;;;;AA0BO,SAAS,WAAA,CACd,YAAA,EACA,OAAA,EACA,MAAA,EACU;AACV,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA,MAAM,YAAA,CAAa,eAAA;AAAA,IACnB,KAAA,EAAO,aAAa,iBAAA,IAAqB,IAAA;AAAA,IACzC,GAAI,MAAA,GAAS,EAAE,MAAA,KAAW;AAAC,GAC7B;AACF;AAOO,SAAS,YAAA,CACd,cACA,G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AAA;AAAA,QACF;AACA,QAAA,KAAA,CAAM,GAAA,CAAI,EAAE,OAAA,EAAS,KAAA,EAAO,SAAS,QAAA,CAAS,KAAK,GAAG,CAAA;AAAA,MACxD;AAAA,KACF;AAAA,EACF,CAAA;AACA,EAAA,MAAM,OAAO,MAAM;AACjB,IAAA,MAAA,GAAS,IAAA;AACT,IAAA,SAAA,GAAY,KAAA;AACZ,IAAA,KAAA,CAAM,GAAA,CAAI,EAAE,OAAA,EAAS,IAAA,EAAM,CAAA;AAC3B,IAAA,IAAA,CAAK,IAAI,CAAA;AAAA,EACX,CAAA;AACA,EAAA,MAAM,cAAc,MAAM;AACxB,IAAA,MAAM,CAAA,GAAI,MAAM,GAAA,EAAI;AACpB,IAAA,IAAI,CAAC,EAAE,OAAA,IAAW,CAAA,CAAE,WAAW,CAAA,CAAE,IAAA,IAAQ,WAAW,IAAA,EAAM;AAC1D,IAAA,IAAA,CAAK,MAAM,CAAA;AAAA,EACb,CAAA;AACA,EAAA,OAAO;AAAA,IACL,aAAa,KAAA,CAAM,GAAA;AAAA,IACnB,WAAW,KAAA,CAAM,SAAA;AAAA,IACjB,IAAA;AAAA,IACA,WAAA;AAAA,IACA,OAAO,MAAO,MAAA,KAAW,IAAA,GAAO,IAAA,KAAS,WAAA;AAAY,GACvD;AACF","file":"index.js","sourcesContent":["// @vinaverse/client/history: a person's past conversations with the agent (C2), through the tenant's backend\n// (`AgentBackend.listConversations`, `readConversation`): the list, newest activity first, a page at a time; a past\n// conversation's transcript, newest page first, earlier pages prepended; delete through the runtime. No React: the\n// kits bind to it. Nothing begun before a sign-out lands after it (the runtime's `epoch`).\nimport {\n type AgentBackend,\n type AgentFailureReason,\n type AgentKey,\n type AgentRuntime,\n isRefusal,\n} from '../agent/runtime.js';\nimport type {\n CardItem,\n MessageItem,\n TimelineItem,\n} from '../conversation/view.js';\nimport type { Schemas } from '../events.js';\n\nexport type ConversationSummary = Schemas['ConversationSummary'];\ntype HistoryItem = Schemas['TimelineItem'];\n\n/**\n * The key a past conversation continues under for this person: its side and business as it was bound when it started\n * (`experience_mode`, `business_scope_id`), the `thread` given (default `chat`). For `runtime.continue(key, id)` and\n * `deleteConversation(key, id)`, so the device forgets it only where that key still continues it.\n */\nexport function continueKey(\n conversation: ConversationSummary,\n subject: string | null,\n thread?: string,\n): AgentKey {\n return {\n subject,\n mode: conversation.experience_mode,\n scope: conversation.business_scope_id ?? null,\n ...(thread ? { thread } : {}),\n };\n}\n\n/**\n * A past conversation's title for its row: the object's title when the conversation is about one (`anchor.title`,\n * vinaverse-v1 v1-draft.37, read once the summary carries it), else the app's own (`own`), else the server's. Never\n * the model's words, and never the person's.\n */\nexport function summaryTitle(\n conversation: ConversationSummary,\n own?: string | null,\n): string {\n const anchor = (conversation as { readonly anchor?: { title?: unknown } })\n .anchor;\n return (\n (typeof anchor?.title === 'string' && anchor.title) ||\n own ||\n conversation.title\n );\n}\n\n/**\n * A past conversation's items as the kits draw them: messages as sent (an `assistant` reply is the agent's, cut-off\n * replies and refusals marked), cards restored (drawn as they were, taking no answer or action), a proposal with the\n * outcome it had. Keyed by the server's ids, so pages merge without doubles.\n */\nexport function historyItems(data: readonly HistoryItem[]): TimelineItem[] {\n return data.map((item, seq): TimelineItem => {\n if (item.type === 'card') {\n const outcome = item.proposal_outcome;\n const card: CardItem = {\n type: 'card',\n key: item.card.id,\n busy: false,\n restored: true,\n card: {\n id: item.card.id,\n kind: item.card.kind,\n card: item.card as unknown as Readonly<Record<string, unknown>>,\n locked: [],\n turnId: item.turn_id,\n outcome: outcome && outcome !== 'pending' ? outcome : null,\n seq,\n },\n };\n return card;\n }\n const message: MessageItem = {\n type: 'message',\n key: `h:${item.id}`,\n role: item.role === 'assistant' ? 'agent' : item.role,\n text: item.text,\n state: 'sent',\n ...(item.interrupted ? { partial: 'interrupted' as const } : {}),\n ...(item.refusal ? { refusal: true as const } : {}),\n at: item.at,\n };\n return message;\n });\n}\n\n/** Why a page could not be read, as the runtime says it. */\nfunction reasonOf(error: unknown): AgentFailureReason {\n if (isRefusal(error)) return error.reason;\n const given = (error as { reason?: unknown } | null)?.reason;\n return typeof given === 'string' ? (given as AgentFailureReason) : 'unknown';\n}\n\n/**\n * The cursor restart (`vinaverse.agent-backend/1`): a page asked with a cursor, refused `unavailable`, not retryable,\n * with the gateway's `invalid_request`, had a cursor the gateway no longer takes (stale, or of other filters). Asked\n * again from the first page, once. Any other refusal is said as it is.\n */\nfunction staleCursor(error: unknown): boolean {\n const e = error as {\n outcome?: unknown;\n gatewayCode?: unknown;\n retryable?: unknown;\n } | null;\n return (\n e?.outcome === 'unavailable' &&\n e.gatewayCode === 'invalid_request' &&\n e.retryable === false\n );\n}\n\n/** A paged store: one page read at a time, dropped when it lands after a newer read or a sign-out. */\nfunction paged<S extends { readonly loading: boolean }>(\n runtime: AgentRuntime,\n empty: S,\n) {\n let state = empty;\n let epoch = runtime.epoch();\n let asked = 0;\n const listeners = new Set<() => void>();\n const set = (next: Partial<S>) => {\n state = { ...state, ...next };\n for (const l of [...listeners]) l();\n };\n // A sign-out empties what the last person had, as soon as anyone listens again.\n const fresh = () => {\n if (runtime.epoch() === epoch) return;\n epoch = runtime.epoch();\n asked++;\n state = empty;\n };\n let off: (() => void) | null = null;\n return {\n get: () => {\n fresh();\n return state;\n },\n set,\n subscribe(listener: () => void) {\n listeners.add(listener);\n off ??= runtime.subscribe(() => {\n const before = state;\n fresh();\n if (state !== before) for (const l of [...listeners]) l();\n });\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n off?.();\n off = null;\n }\n };\n },\n /** Reads a page; `apply` lands only for the newest read, under the same person. */\n read<T>(\n call: () => Promise<T>,\n apply: (value: T) => void,\n failed: (error: unknown) => void,\n ) {\n fresh();\n const mine = ++asked;\n const under = epoch;\n set({ loading: true } as Partial<S>);\n call().then(\n (value) => {\n if (mine === asked && under === runtime.epoch()) apply(value);\n },\n (error: unknown) => {\n if (mine === asked && under === runtime.epoch()) failed(error);\n },\n );\n },\n };\n}\n\nexport interface AgentHistoryState {\n readonly conversations: readonly ConversationSummary[];\n /** A page is being read. */\n readonly loading: boolean;\n /** Why the last page could not be read; null when it was. `off` when the backend serves no history. */\n readonly failure: AgentFailureReason | null;\n /** Every page is read. */\n readonly done: boolean;\n /** The first page was asked for. */\n readonly started: boolean;\n}\n\nexport interface AgentHistory {\n getSnapshot(): AgentHistoryState;\n subscribe(listener: () => void): () => void;\n /** Reads the first page anew: the first time, after a failure, or to see what changed. */\n refresh(): void;\n /** The next older page, when there is one and none is being read. */\n loadMore(): void;\n /** Reads again the page that failed: the first (as `refresh`), or the next. */\n retry(): void;\n /**\n * Deletes a past conversation (the runtime's `deleteConversation` for the key that holds it, so the device forgets\n * it only where the key still continues it) and drops its row. One already gone (`not_found`) is dropped too.\n */\n remove(key: AgentKey, conversationId: string): Promise<void>;\n}\n\nexport interface AgentHistoryOptions {\n readonly runtime: AgentRuntime;\n /** Default: the runtime's. */\n readonly backend?: AgentBackend;\n /** The app's side: `customer` or `business`. */\n readonly mode: string;\n /** The business acted for, with `business`. */\n readonly scope?: string | null;\n /** The agent, for a backend that serves more than one. */\n readonly agent?: string | null;\n /** Conversations per page (1–50). Default 20. */\n readonly pageSize?: number;\n}\n\nconst NO_HISTORY: AgentHistoryState = {\n conversations: [],\n loading: false,\n failure: null,\n done: false,\n started: false,\n};\n\n/** A person's past conversations, newest activity first. */\nexport function createAgentHistory(options: AgentHistoryOptions): AgentHistory {\n const { runtime } = options;\n const backend = options.backend ?? runtime.backend;\n const store = paged(runtime, NO_HISTORY);\n let cursor: string | null = null;\n let restarted = false;\n const page = (from: string | null) => {\n const list = backend.listConversations?.bind(backend);\n if (!list) {\n store.set({ started: true, failure: 'off', done: true });\n return;\n }\n store.read(\n () =>\n list({\n mode: options.mode,\n scope: options.scope ?? null,\n agent: options.agent ?? null,\n limit: options.pageSize ?? 20,\n cursor: from,\n }),\n (got) => {\n const known = new Set(\n from ? store.get().conversations.map((c) => c.id) : [],\n );\n cursor = got.next_cursor;\n store.set({\n conversations: [\n ...(from ? store.get().conversations : []),\n ...got.data.filter((c) => !known.has(c.id)),\n ],\n loading: false,\n failure: null,\n done: cursor === null,\n });\n },\n (error) => {\n if (from !== null && staleCursor(error) && !restarted) {\n restarted = true;\n cursor = null;\n page(null);\n return;\n }\n store.set({ loading: false, failure: reasonOf(error) });\n },\n );\n };\n const refresh = () => {\n cursor = null;\n restarted = false;\n store.set({ started: true });\n page(null);\n };\n const loadMore = () => {\n const s = store.get();\n if (!s.started || s.loading || s.done || cursor === null) return;\n page(cursor);\n };\n return {\n getSnapshot: store.get,\n subscribe: store.subscribe,\n refresh,\n loadMore,\n retry: () => (cursor === null ? refresh() : loadMore()),\n async remove(key, conversationId) {\n try {\n await runtime.deleteConversation(key, conversationId);\n } catch (error) {\n if (!(isRefusal(error) && error.reason === 'not_found')) throw error;\n }\n store.set({\n conversations: store\n .get()\n .conversations.filter((c) => c.id !== conversationId),\n });\n },\n };\n}\n\nexport interface AgentTranscriptState {\n /** The conversation's items, oldest first, as the kits draw them (`historyItems`). */\n readonly items: readonly TimelineItem[];\n readonly loading: boolean;\n /** Why the last page could not be read: `not_found` (deleted, or never theirs), `expired`, … */\n readonly failure: AgentFailureReason | null;\n /** Nothing earlier to read. */\n readonly done: boolean;\n readonly started: boolean;\n}\n\nexport interface AgentTranscript {\n getSnapshot(): AgentTranscriptState;\n subscribe(listener: () => void): () => void;\n /** Reads the newest page anew. */\n load(): void;\n /** The page before the earliest read, prepended. */\n loadEarlier(): void;\n /** Reads again the page that failed: the newest (as `load`), or the earlier one. */\n retry(): void;\n}\n\nexport interface AgentTranscriptOptions {\n readonly runtime: AgentRuntime;\n /** Default: the runtime's. */\n readonly backend?: AgentBackend;\n readonly conversationId: string;\n /** Items per page. Default 30. */\n readonly pageSize?: number;\n}\n\nconst NO_TRANSCRIPT: AgentTranscriptState = {\n items: [],\n loading: false,\n failure: null,\n done: false,\n started: false,\n};\n\n/** A past conversation's transcript, read-only: newest page first, earlier pages prepended. */\nexport function createAgentTranscript(\n options: AgentTranscriptOptions,\n): AgentTranscript {\n const { runtime } = options;\n const backend = options.backend ?? runtime.backend;\n const store = paged(runtime, NO_TRANSCRIPT);\n let cursor: string | null = null;\n let restarted = false;\n const page = (from: string | null) => {\n const read = backend.readConversation?.bind(backend);\n if (!read) {\n store.set({ started: true, failure: 'off', done: true });\n return;\n }\n store.read(\n () =>\n read(options.conversationId, {\n limit: options.pageSize ?? 30,\n cursor: from,\n }),\n (got) => {\n const earlier = historyItems(got.data);\n const held = from ? store.get().items : [];\n const keys = new Set(held.map((i) => i.key));\n cursor = got.next_cursor;\n store.set({\n items: [...earlier.filter((i) => !keys.has(i.key)), ...held],\n loading: false,\n failure: null,\n done: cursor === null,\n });\n },\n (error) => {\n // An earlier page's cursor went stale: read back from the newest page, once.\n if (from !== null && staleCursor(error) && !restarted) {\n restarted = true;\n cursor = null;\n page(null);\n return;\n }\n store.set({ loading: false, failure: reasonOf(error) });\n },\n );\n };\n const load = () => {\n cursor = null;\n restarted = false;\n store.set({ started: true });\n page(null);\n };\n const loadEarlier = () => {\n const s = store.get();\n if (!s.started || s.loading || s.done || cursor === null) return;\n page(cursor);\n };\n return {\n getSnapshot: store.get,\n subscribe: store.subscribe,\n load,\n loadEarlier,\n retry: () => (cursor === null ? load() : loadEarlier()),\n };\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -18,6 +18,11 @@ var RECONNECTABLE = /* @__PURE__ */ new Set(["server_restart"]);
|
|
|
18
18
|
var NEW_SESSION = /* @__PURE__ */ new Set(["idle", "expired", "limit_reached"]);
|
|
19
19
|
var str = (v) => typeof v === "string" ? v : void 0;
|
|
20
20
|
var ConversationEngine = class {
|
|
21
|
+
/** `now`: the clock a message is stamped by when the server gives no time. */
|
|
22
|
+
constructor(now = Date.now) {
|
|
23
|
+
this.now = now;
|
|
24
|
+
}
|
|
25
|
+
now;
|
|
21
26
|
sessionId = null;
|
|
22
27
|
conversationId = null;
|
|
23
28
|
connection = null;
|
|
@@ -206,7 +211,14 @@ var ConversationEngine = class {
|
|
|
206
211
|
case "message.staff": {
|
|
207
212
|
const id = str(event.message_id);
|
|
208
213
|
if (id !== void 0)
|
|
209
|
-
this.say(
|
|
214
|
+
this.say(
|
|
215
|
+
"staff",
|
|
216
|
+
str(event.text) ?? "",
|
|
217
|
+
`s:${id}`,
|
|
218
|
+
{},
|
|
219
|
+
void 0,
|
|
220
|
+
str(event.at)
|
|
221
|
+
);
|
|
210
222
|
return;
|
|
211
223
|
}
|
|
212
224
|
case "input.received": {
|
|
@@ -288,7 +300,9 @@ var ConversationEngine = class {
|
|
|
288
300
|
role,
|
|
289
301
|
str(item.text) ?? "",
|
|
290
302
|
transcriptKey(role, str(item.id), str(item.at)),
|
|
291
|
-
item.interrupted === true ? { partial: "interrupted" } : {}
|
|
303
|
+
item.interrupted === true ? { partial: "interrupted" } : {},
|
|
304
|
+
void 0,
|
|
305
|
+
str(item.at)
|
|
292
306
|
);
|
|
293
307
|
}
|
|
294
308
|
}
|
|
@@ -333,7 +347,8 @@ var ConversationEngine = class {
|
|
|
333
347
|
text,
|
|
334
348
|
key,
|
|
335
349
|
partial ? { partial } : {},
|
|
336
|
-
at
|
|
350
|
+
at,
|
|
351
|
+
str(item.at)
|
|
337
352
|
);
|
|
338
353
|
else
|
|
339
354
|
this.messages = this.messages.map((m) => {
|
|
@@ -461,9 +476,16 @@ var ConversationEngine = class {
|
|
|
461
476
|
this.unqueue(id);
|
|
462
477
|
}
|
|
463
478
|
/** Adds a message once: a key already held (a frame seen twice) changes nothing. */
|
|
464
|
-
say(role, text, key, extra = {}, seq) {
|
|
479
|
+
say(role, text, key, extra = {}, seq, at) {
|
|
465
480
|
if (this.messages.some((m) => m.key === key)) return;
|
|
466
|
-
this.messages.push({
|
|
481
|
+
this.messages.push({
|
|
482
|
+
role,
|
|
483
|
+
text,
|
|
484
|
+
key,
|
|
485
|
+
seq: seq ?? this.seq++,
|
|
486
|
+
...extra,
|
|
487
|
+
at: at || new Date(this.now()).toISOString()
|
|
488
|
+
});
|
|
467
489
|
}
|
|
468
490
|
/** The turn's streamed text, kept as the agent's message when the turn ends without its `message.completed`. */
|
|
469
491
|
cutOff(partial) {
|
|
@@ -629,7 +651,8 @@ var CARD_KINDS = {
|
|
|
629
651
|
question: true,
|
|
630
652
|
proposal: true,
|
|
631
653
|
next_step: true,
|
|
632
|
-
links: true
|
|
654
|
+
links: true,
|
|
655
|
+
anchor: true
|
|
633
656
|
};
|
|
634
657
|
function isKnownCard(card) {
|
|
635
658
|
return Object.hasOwn(CARD_KINDS, card.kind);
|