@lostgradient/cinder 0.8.0 → 0.9.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 +9 -0
- package/README.md +2 -2
- package/components.json +59 -15
- package/dist/components/_internal/command-list-context.d.ts +3 -3
- package/dist/components/_internal/create-command-list-state.svelte.d.ts +11 -11
- package/dist/components/access-gate/index.js +201 -126
- package/dist/components/approval-card/approval-card-actions.svelte.d.ts +16 -0
- package/dist/components/approval-card/approval-card-state.d.ts +65 -0
- package/dist/components/approval-card/approval-card.css +269 -92
- package/dist/components/approval-card/approval-card.schema.js +10 -31
- package/dist/components/approval-card/approval-card.svelte.d.ts +3 -3
- package/dist/components/approval-card/approval-card.types.d.ts +14 -28
- package/dist/components/approval-card/index.d.ts +1 -1
- package/dist/components/approval-card/index.js +4268 -2884
- package/dist/components/card/card.css +5 -0
- package/dist/components/card/card.schema.js +2 -2
- package/dist/components/card/card.types.d.ts +2 -2
- package/dist/components/card/index.js +2 -1
- package/dist/components/chat/chat.schema.js +44 -2
- package/dist/components/chat/chat.svelte.d.ts +4 -2
- package/dist/components/chat/chat.types.d.ts +34 -3
- package/dist/components/chat/container/chat.svelte.d.ts +4 -2
- package/dist/components/chat/index.d.ts +3 -3
- package/dist/components/chat/index.js +2485 -6279
- package/dist/components/chat/input/chat-attachment-serialization.d.ts +9 -0
- package/dist/components/chat/input/chat-input.svelte.d.ts +22 -1
- package/dist/components/chat/input/index.d.ts +1 -0
- package/dist/components/chat-composer-popover/chat-composer-popover-filter.d.ts +11 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.css +7 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.css.d.ts +1 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.schema.d.ts +3 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.schema.js +150 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.svelte.d.ts +46 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.types.d.ts +77 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.variables.d.ts +2 -0
- package/dist/components/chat-composer-popover/chat-composer-popover.variables.js +61 -0
- package/dist/components/chat-composer-popover/index.d.ts +6 -0
- package/dist/components/chat-composer-popover/index.js +3285 -0
- package/dist/components/chat-conversation-header/index.js +1627 -5600
- package/dist/components/chat-conversation-list/index.js +15 -3988
- package/dist/components/code-block/code-block.schema.js +5 -1
- package/dist/components/code-block/code-block.types.d.ts +6 -0
- package/dist/components/code-block/index.js +4 -4
- package/dist/components/command-item/index.js +7 -6
- package/dist/components/command-menu/command-menu.schema.js +5 -1
- package/dist/components/command-menu/command-menu.types.d.ts +2 -0
- package/dist/components/command-menu/index.js +48 -19
- package/dist/components/command-palette/index.js +40 -14
- package/dist/components/event-stream-viewer/index.js +44 -44
- package/dist/components/experimental/json-viewer/index.js +44 -44
- package/dist/components/json-schema-editor/index.js +5 -2
- package/dist/components/json-viewer/index.js +44 -44
- package/dist/components/json-viewer/json-viewer.css +10 -6
- package/dist/components/payload-inspector/index.d.ts +1 -1
- package/dist/components/payload-inspector/index.js +306 -1422
- package/dist/components/payload-inspector/payload-inspector.css +15 -75
- package/dist/components/payload-inspector/payload-inspector.schema.js +5 -41
- package/dist/components/payload-inspector/payload-inspector.svelte.d.ts +5 -5
- package/dist/components/payload-inspector/payload-inspector.types.d.ts +10 -56
- package/dist/components/tab/index.js +5 -2
- package/dist/components/tab/tab.schema.js +7 -3
- package/dist/components/tab/tab.svelte.d.ts +2 -1
- package/dist/components/tab/tab.types.d.ts +8 -2
- package/dist/components/tabs/index.js +5 -2
- package/dist/components/team-section/index.js +2 -1
- package/dist/components/testimonial-section/index.js +2 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +23711 -27432
- package/dist/server/components/access-gate/index.js +2 -1
- package/dist/server/components/accordion/index.js +2 -2
- package/dist/server/components/alert-dialog/index.js +2 -2
- package/dist/server/components/approval-card/index.js +15 -19
- package/dist/server/components/autocomplete/index.js +7 -7
- package/dist/server/components/avatar-group/index.js +5 -5
- package/dist/server/components/button-group/index.js +3 -5
- package/dist/server/components/card/index.js +2 -2
- package/dist/server/components/chat/index.js +14 -11
- package/dist/server/components/chat-composer-popover/index.js +26 -0
- package/dist/server/components/chat-conversation-header/index.js +9 -10
- package/dist/server/components/chat-conversation-list/index.js +4 -5
- package/dist/server/components/checkbox/index.js +3 -3
- package/dist/server/components/choice-grid/index.js +2 -2
- package/dist/server/components/code-block/index.js +2 -2
- package/dist/server/components/color-field/index.js +3 -3
- package/dist/server/components/combobox/index.js +6 -6
- package/dist/server/components/command-item/index.js +5 -3
- package/dist/server/components/command-menu/index.js +6 -4
- package/dist/server/components/command-palette/index.js +2 -2
- package/dist/server/components/confirm-dialog/index.js +2 -2
- package/dist/server/components/context-menu/index.js +5 -5
- package/dist/server/components/data-table/index.js +5 -5
- package/dist/server/components/date-picker/index.js +6 -6
- package/dist/server/components/date-range-field/index.js +7 -7
- package/dist/server/components/description-list/index.js +3 -5
- package/dist/server/components/diff-viewer/index.js +7 -7
- package/dist/server/components/drawer/index.js +3 -3
- package/dist/server/components/dropdown/index.js +4 -4
- package/dist/server/components/event-stream-viewer/index.js +2 -2
- package/dist/server/components/experimental/json-viewer/index.js +2 -2
- package/dist/server/components/faceted-filter-bar/index.js +1 -1
- package/dist/server/components/hover-card/index.js +1 -1
- package/dist/server/components/input/index.js +3 -3
- package/dist/server/components/json-schema-editor/index.js +22 -22
- package/dist/server/components/json-viewer/index.js +2 -2
- package/dist/server/components/markdown-editor/index.js +13 -13
- package/dist/server/components/modal/index.js +1 -1
- package/dist/server/components/multi-select/index.js +1 -1
- package/dist/server/components/newsletter-section/index.js +3 -3
- package/dist/server/components/payload-inspector/index.js +4 -16
- package/dist/server/components/popover/index.js +5 -5
- package/dist/server/components/review-editor/index.js +25 -25
- package/dist/server/components/run-step-timeline/index.js +2 -2
- package/dist/server/components/schema-form/index.js +4 -4
- package/dist/server/components/secret-value-field/index.js +3 -5
- package/dist/server/components/segment/index.js +3 -3
- package/dist/server/components/segmented-control/index.js +4 -4
- package/dist/server/components/selection-popover/index.js +4 -4
- package/dist/server/components/sheet/index.js +2 -2
- package/dist/server/components/sidebar/index.js +3 -3
- package/dist/server/components/tab/index.js +2 -2
- package/dist/server/components/table/index.js +3 -3
- package/dist/server/components/tabs/index.js +4 -4
- package/dist/server/components/tag-input/index.js +3 -3
- package/dist/server/components/team-section/index.js +8 -8
- package/dist/server/components/testimonial-section/index.js +2 -2
- package/dist/server/components/toggle/index.js +2 -2
- package/dist/server/components/tooltip/index.js +1 -1
- package/dist/server/index.js +159 -148
- package/dist/server/{index.server-cbtg8rq5.js → index.server-0atyznp7.js} +5 -5
- package/dist/server/{index.server-20y4mdhm.js → index.server-0chhpkg7.js} +40 -14
- package/dist/server/{index.server-ecm1vs3g.js → index.server-1nq7z1qw.js} +1 -1
- package/dist/server/{index.server-jync915v.js → index.server-2dga5yzr.js} +25 -25
- package/dist/server/index.server-2hqrv7ca.js +9 -0
- package/dist/server/{index.server-69epaape.js → index.server-2qj6s2mh.js} +1 -1
- package/dist/server/{index.server-pt8b65mt.js → index.server-38aywdk1.js} +1 -1
- package/dist/server/{index.server-rermas0k.js → index.server-47v2fhp4.js} +4 -4
- package/dist/server/{index.server-9j5sym59.js → index.server-4x91gzy1.js} +2 -2
- package/dist/server/{index.server-q48q15g7.js → index.server-4y2jtvv1.js} +4 -2
- package/dist/server/index.server-67qgdypc.js +9 -0
- package/dist/server/{index.server-9jete4xx.js → index.server-6sh11ap6.js} +4 -4
- package/dist/server/{index.server-4ztq565m.js → index.server-85p4ydph.js} +1 -1
- package/dist/server/{index.server-4nk7gy0q.js → index.server-894t97kv.js} +1 -1
- package/dist/server/{index.server-1vscnped.js → index.server-8dt36sdm.js} +2 -1
- package/dist/server/{index.server-bfsbp9dz.js → index.server-8xvc73zh.js} +4 -4
- package/dist/server/{index.server-cvnw3hw9.js → index.server-9kz37z89.js} +1 -1
- package/dist/server/{index.server-kc1k02tk.js → index.server-9ykk1kbg.js} +1 -1
- package/dist/server/{index.server-g789yep6.js → index.server-aqethy0w.js} +2 -2
- package/dist/server/{index.server-wc9aggfz.js → index.server-bmxbsk4y.js} +7 -7
- package/dist/server/{index.server-j3ddzcaa.js → index.server-c6pe4kaf.js} +6 -6
- package/dist/server/{index.server-q89dz8h1.js → index.server-d3pqthtp.js} +4 -3
- package/dist/server/{index.server-qfa2rxzx.js → index.server-dg83ke3k.js} +3 -5
- package/dist/server/{index.server-jk4rx4aa.js → index.server-dmmvwy6p.js} +1 -1
- package/dist/server/{index.server-hcfnae5y.js → index.server-dwwqssey.js} +4 -4
- package/dist/server/{index.server-e8x1hgw6.js → index.server-dz1r9xw5.js} +4 -4
- package/dist/server/{index.server-wm7mk5kw.js → index.server-ez5hp0ya.js} +1 -1
- package/dist/server/{index.server-ejyf7e48.js → index.server-fy068yne.js} +1 -1
- package/dist/server/{index.server-wc8dbm5e.js → index.server-g95bk8rc.js} +4 -2
- package/dist/server/index.server-gkazv439.js +576 -0
- package/dist/server/{index.server-dp9j2kkr.js → index.server-h0cvwvhy.js} +1 -1
- package/dist/server/{index.server-pacrqvyz.js → index.server-h90pjrez.js} +1 -1
- package/dist/server/{index.server-p8d04em5.js → index.server-hcsd5gdb.js} +6 -2
- package/dist/server/{index.server-n39drzes.js → index.server-hpeavvnq.js} +1 -1
- package/dist/server/{index.server-1p3patnj.js → index.server-hx91ng7z.js} +1 -1
- package/dist/server/{index.server-jtj3r53y.js → index.server-jgb8yh5k.js} +1 -1
- package/dist/server/{index.server-w1g5q9j9.js → index.server-jtgw9d23.js} +4 -2
- package/dist/server/{index.server-mehav5vs.js → index.server-jxg42jpz.js} +4 -4
- package/dist/server/{index.server-hd8gpehd.js → index.server-jxseyx33.js} +1 -1
- package/dist/server/{index.server-kg2sepsr.js → index.server-krqvnt6p.js} +14 -15
- package/dist/server/{index.server-0e1m3agz.js → index.server-ks49bprh.js} +1 -1
- package/dist/server/{index.server-f0hma3jp.js → index.server-kt0ncrqs.js} +1 -1
- package/dist/server/index.server-m0ezemav.js +170 -0
- package/dist/server/index.server-m191rm44.js +79 -0
- package/dist/server/{index.server-f23vtaw4.js → index.server-marjz8ps.js} +6 -6
- package/dist/server/index.server-mqt1ngqw.js +250 -0
- package/dist/server/{index.server-s9khh4ry.js → index.server-mzzb7dnm.js} +3 -5
- package/dist/server/{index.server-z721a5gp.js → index.server-n82rcv0m.js} +4 -4
- package/dist/server/{index.server-rhhmdxtz.js → index.server-np3c7kx0.js} +4 -4
- package/dist/server/{index.server-hpcs7tmh.js → index.server-p56xxef1.js} +1 -1
- package/dist/server/{index.server-w83kmcrx.js → index.server-pag862vz.js} +10 -10
- package/dist/server/{index.server-gxf0d1p5.js → index.server-q7w4jw89.js} +2 -2
- package/dist/server/{index.server-v4zabrsx.js → index.server-qh9qzd5t.js} +163 -25
- package/dist/server/{index.server-pxg74pyp.js → index.server-qk1h0n02.js} +4 -4
- package/dist/server/{index.server-sh2rxejg.js → index.server-qke3ekjt.js} +1 -1
- package/dist/server/{index.server-73x2d3j3.js → index.server-rbpvcrr3.js} +1 -1
- package/dist/server/{index.server-8cxs4kya.js → index.server-rga8sc7j.js} +41 -44
- package/dist/server/{index.server-e3fnv1g6.js → index.server-sedjxt36.js} +5 -5
- package/dist/server/{index.server-pen9pxt6.js → index.server-sf3nk3j2.js} +2 -2
- package/dist/server/{index.server-7fp0epev.js → index.server-veastatv.js} +4 -4
- package/dist/server/{index.server-jeqg2d22.js → index.server-xeqtpv61.js} +1 -1
- package/dist/server/{index.server-9s4na701.js → index.server-xjnge1yw.js} +4 -4
- package/dist/server/index.server-xtjjmsyq.js +315 -0
- package/dist/server/{index.server-hhxjaeen.js → index.server-xz99aed4.js} +4 -4
- package/dist/server/{index.server-vne4rzfa.js → index.server-y4n8vc08.js} +1 -1
- package/dist/server/{index.server-5d01cn4f.js → index.server-y83kqayt.js} +4 -4
- package/dist/server/{index.server-1nh09ck7.js → index.server-yhzydqxe.js} +5 -5
- package/dist/server/{index.server-atjc1qcq.js → index.server-yz4k36cw.js} +1 -1
- package/dist/server/{index.server-w6k62afr.js → index.server-ze1ymmbw.js} +2 -2
- package/dist/server/{index.server-914v805w.js → index.server-zgd1g8mk.js} +1 -1
- package/dist/server/{index.server-aeexcxdp.js → index.server-zh3r8vfd.js} +1 -1
- package/dist/server/{index.server-10jb2jzm.js → index.server-zsjf8enc.js} +4 -4
- package/package.json +40 -6
- package/src/components/_internal/command-list-context.ts +3 -3
- package/src/components/_internal/create-command-list-state.svelte.ts +39 -13
- package/src/components/approval-card/approval-card-actions.svelte +168 -0
- package/src/components/approval-card/approval-card-state.ts +152 -1
- package/src/components/approval-card/approval-card.css +269 -92
- package/src/components/approval-card/approval-card.examples.json +3 -3
- package/src/components/approval-card/approval-card.schema.json +9 -30
- package/src/components/approval-card/approval-card.schema.ts +13 -31
- package/src/components/approval-card/approval-card.svelte +176 -470
- package/src/components/approval-card/approval-card.types.ts +15 -28
- package/src/components/approval-card/index.ts +1 -0
- package/src/components/card/card.css +5 -0
- package/src/components/card/card.schema.json +1 -1
- package/src/components/card/card.schema.ts +1 -1
- package/src/components/card/card.svelte +1 -0
- package/src/components/card/card.types.ts +2 -2
- package/src/components/chat/chat.schema.json +36 -1
- package/src/components/chat/chat.schema.ts +44 -1
- package/src/components/chat/chat.svelte +12 -0
- package/src/components/chat/chat.types.ts +35 -3
- package/src/components/chat/container/chat.svelte +73 -3
- package/src/components/chat/index.ts +15 -3
- package/src/components/chat/input/chat-attachment-serialization.ts +63 -0
- package/src/components/chat/input/chat-input.svelte +22 -5
- package/src/components/chat/input/index.ts +5 -0
- package/src/components/chat-composer-popover/chat-composer-popover-filter.ts +68 -0
- package/src/components/chat-composer-popover/chat-composer-popover.css +7 -0
- package/src/components/chat-composer-popover/chat-composer-popover.examples.json +13 -0
- package/src/components/chat-composer-popover/chat-composer-popover.schema.json +90 -0
- package/src/components/chat-composer-popover/chat-composer-popover.schema.ts +95 -0
- package/src/components/chat-composer-popover/chat-composer-popover.svelte +234 -0
- package/src/components/chat-composer-popover/chat-composer-popover.types.ts +88 -0
- package/src/components/chat-composer-popover/chat-composer-popover.variables.json +1 -0
- package/src/components/chat-composer-popover/chat-composer-popover.variables.ts +3 -0
- package/src/components/chat-composer-popover/index.ts +19 -0
- package/src/components/code-block/code-block.schema.json +4 -0
- package/src/components/code-block/code-block.schema.ts +5 -0
- package/src/components/code-block/code-block.svelte +3 -3
- package/src/components/code-block/code-block.types.ts +6 -0
- package/src/components/command-item/command-item.svelte +7 -6
- package/src/components/command-menu/command-menu.schema.json +4 -0
- package/src/components/command-menu/command-menu.schema.ts +4 -0
- package/src/components/command-menu/command-menu.svelte +16 -5
- package/src/components/command-menu/command-menu.types.ts +2 -0
- package/src/components/json-viewer/_json-viewer-node.svelte +15 -1
- package/src/components/json-viewer/json-viewer.css +10 -6
- package/src/components/payload-inspector/index.ts +0 -2
- package/src/components/payload-inspector/payload-inspector.css +15 -75
- package/src/components/payload-inspector/payload-inspector.examples.json +5 -5
- package/src/components/payload-inspector/payload-inspector.schema.json +3 -31
- package/src/components/payload-inspector/payload-inspector.schema.ts +3 -36
- package/src/components/payload-inspector/payload-inspector.svelte +46 -239
- package/src/components/payload-inspector/payload-inspector.types.ts +10 -58
- package/src/components/tab/tab.schema.json +6 -2
- package/src/components/tab/tab.schema.ts +8 -2
- package/src/components/tab/tab.svelte +9 -5
- package/src/components/tab/tab.types.ts +8 -2
- package/src/index.ts +18 -5
- package/src/styles/components.css +1 -0
- package/dist/server/index-49we2xfs.js +0 -344
- package/dist/server/index-8fhvxk30.js +0 -265
- package/dist/server/index-x2ysg8ds.js +0 -388
- package/dist/server/index.server-20j0jh30.js +0 -9
- package/dist/server/index.server-dc35kh7d.js +0 -1835
- package/dist/server/index.server-kd4b0342.js +0 -607
- package/dist/server/index.server-n5cdmsz4.js +0 -30
- package/dist/server/index.server-rjap4k7y.js +0 -1389
- package/dist/server/index.server-vt6sn9y4.js +0 -447
- package/dist/server/index.server-w63n618v.js +0 -9
- package/dist/server/index.server-y2pa1k46.js +0 -9
|
@@ -51,3999 +51,26 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
51
51
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
if (item.type === "text")
|
|
57
|
-
return {
|
|
58
|
-
type: "text",
|
|
59
|
-
text: item.text,
|
|
60
|
-
...item.citations !== undefined ? { citations: structuredClone(item.citations) } : {}
|
|
61
|
-
};
|
|
62
|
-
if (item.type === "thinking")
|
|
63
|
-
return {
|
|
64
|
-
type: "thinking",
|
|
65
|
-
thinking: item.thinking,
|
|
66
|
-
signature: item.signature
|
|
67
|
-
};
|
|
68
|
-
if (item.type === "redacted_thinking")
|
|
69
|
-
return {
|
|
70
|
-
type: "redacted_thinking",
|
|
71
|
-
data: item.data
|
|
72
|
-
};
|
|
73
|
-
if (item.type === "server_tool_use")
|
|
74
|
-
return {
|
|
75
|
-
type: "server_tool_use",
|
|
76
|
-
id: item.id,
|
|
77
|
-
name: item.name,
|
|
78
|
-
input: structuredClone(item.input)
|
|
79
|
-
};
|
|
80
|
-
if (item.type === "web_search_tool_result")
|
|
81
|
-
return {
|
|
82
|
-
type: "web_search_tool_result",
|
|
83
|
-
tool_use_id: item.tool_use_id,
|
|
84
|
-
content: structuredClone(item.content)
|
|
85
|
-
};
|
|
86
|
-
if (item.type === "code_execution_tool_result" || item.type === "bash_code_execution_tool_result" || item.type === "text_editor_code_execution_tool_result" || item.type === "web_fetch_tool_result")
|
|
87
|
-
return {
|
|
88
|
-
type: item.type,
|
|
89
|
-
tool_use_id: item.tool_use_id,
|
|
90
|
-
content: structuredClone(item.content)
|
|
91
|
-
};
|
|
92
|
-
if (item.type === "container_upload")
|
|
93
|
-
return {
|
|
94
|
-
type: "container_upload",
|
|
95
|
-
file_id: item.file_id
|
|
96
|
-
};
|
|
97
|
-
const image = item;
|
|
98
|
-
return {
|
|
99
|
-
type: "image",
|
|
100
|
-
url: image.url,
|
|
101
|
-
...image.mimeType !== undefined ? { mimeType: image.mimeType } : {},
|
|
102
|
-
...image.text !== undefined ? { text: image.text } : {}
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
function copyContent(content) {
|
|
106
|
-
if (typeof content === "string")
|
|
107
|
-
return content;
|
|
108
|
-
return content.map(copyMultiModalContent);
|
|
109
|
-
}
|
|
110
|
-
function toReadonly(value) {
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
|
-
function createMessage(props) {
|
|
114
|
-
const content = typeof props.content === "string" ? props.content : toReadonly(structuredClone([...props.content]));
|
|
115
|
-
const message = {
|
|
116
|
-
id: props.id,
|
|
117
|
-
role: props.role,
|
|
118
|
-
content,
|
|
119
|
-
position: props.position,
|
|
120
|
-
createdAt: props.createdAt,
|
|
121
|
-
metadata: toReadonly({ ...props.metadata }),
|
|
122
|
-
hidden: props.hidden,
|
|
123
|
-
toolCall: props.toolCall ? toReadonly(structuredClone(props.toolCall)) : undefined,
|
|
124
|
-
toolResult: props.toolResult ? toReadonly(structuredClone(props.toolResult)) : undefined,
|
|
125
|
-
tokenUsage: props.tokenUsage ? toReadonly({ ...props.tokenUsage }) : undefined
|
|
126
|
-
};
|
|
127
|
-
if (isAssistantMessage(props))
|
|
128
|
-
return toReadonly({
|
|
129
|
-
...message,
|
|
130
|
-
role: "assistant",
|
|
131
|
-
goalCompleted: props.goalCompleted
|
|
132
|
-
});
|
|
133
|
-
return toReadonly(message);
|
|
134
|
-
}
|
|
135
|
-
function messageParts(message) {
|
|
136
|
-
if (typeof message.content === "string")
|
|
137
|
-
return message.content ? [{
|
|
138
|
-
type: "text",
|
|
139
|
-
text: message.content
|
|
140
|
-
}] : [];
|
|
141
|
-
return message.content;
|
|
142
|
-
}
|
|
143
|
-
function messageHasImages(message) {
|
|
144
|
-
return messageParts(message).some((p) => p.type === "image");
|
|
145
|
-
}
|
|
146
|
-
function isAssistantMessage(message) {
|
|
147
|
-
return message.role === "assistant";
|
|
148
|
-
}
|
|
149
|
-
var init_message_BEjqY7Jv = () => {};
|
|
150
|
-
|
|
151
|
-
// ../../node_modules/conversationalist/dist/schemas.js
|
|
152
|
-
import { z } from "zod";
|
|
153
|
-
var isPlainObject = (value) => {
|
|
154
|
-
if (!value || typeof value !== "object")
|
|
155
|
-
return false;
|
|
156
|
-
const prototype = Reflect.getPrototypeOf(value);
|
|
157
|
-
return prototype === Object.prototype || prototype === null;
|
|
158
|
-
}, jsonValueSchema, multiModalContentSchema, messageRoleSchema, toolCallSchema, toolCallInputSchema, toolErrorCategorySchema, toolErrorSchema, toolActionSchema, toolResultSchema, tokenUsageSchema, messageInputSchema, messageSchema, conversationStatusSchema, conversationShape, conversationSchema;
|
|
159
|
-
var init_schemas = __esm(() => {
|
|
160
|
-
jsonValueSchema = z.lazy(() => {
|
|
161
|
-
const jsonObjectSchema = z.preprocess((value, ctx) => {
|
|
162
|
-
if (!isPlainObject(value)) {
|
|
163
|
-
ctx.addIssue({
|
|
164
|
-
code: z.ZodIssueCode.custom,
|
|
165
|
-
message: "expected a plain object"
|
|
166
|
-
});
|
|
167
|
-
return z.NEVER;
|
|
168
|
-
}
|
|
169
|
-
return value;
|
|
170
|
-
}, z.record(z.string(), jsonValueSchema));
|
|
171
|
-
return z.union([
|
|
172
|
-
z.string(),
|
|
173
|
-
z.number().refine((value) => Number.isFinite(value), { message: "expected a finite number" }),
|
|
174
|
-
z.boolean(),
|
|
175
|
-
z.null(),
|
|
176
|
-
z.array(jsonValueSchema),
|
|
177
|
-
jsonObjectSchema
|
|
178
|
-
]);
|
|
179
|
-
});
|
|
180
|
-
multiModalContentSchema = z.discriminatedUnion("type", [
|
|
181
|
-
z.object({
|
|
182
|
-
type: z.literal("text"),
|
|
183
|
-
text: z.string(),
|
|
184
|
-
citations: jsonValueSchema.optional()
|
|
185
|
-
}),
|
|
186
|
-
z.object({
|
|
187
|
-
type: z.literal("image"),
|
|
188
|
-
url: z.string().url(),
|
|
189
|
-
mimeType: z.string().optional(),
|
|
190
|
-
text: z.string().optional()
|
|
191
|
-
}),
|
|
192
|
-
z.object({
|
|
193
|
-
type: z.literal("thinking"),
|
|
194
|
-
thinking: z.string(),
|
|
195
|
-
signature: z.string()
|
|
196
|
-
}),
|
|
197
|
-
z.object({
|
|
198
|
-
type: z.literal("redacted_thinking"),
|
|
199
|
-
data: z.string()
|
|
200
|
-
}),
|
|
201
|
-
z.object({
|
|
202
|
-
type: z.literal("server_tool_use"),
|
|
203
|
-
id: z.string(),
|
|
204
|
-
name: z.string(),
|
|
205
|
-
input: jsonValueSchema
|
|
206
|
-
}),
|
|
207
|
-
z.object({
|
|
208
|
-
type: z.literal("web_search_tool_result"),
|
|
209
|
-
tool_use_id: z.string(),
|
|
210
|
-
content: jsonValueSchema
|
|
211
|
-
}),
|
|
212
|
-
z.object({
|
|
213
|
-
type: z.enum([
|
|
214
|
-
"code_execution_tool_result",
|
|
215
|
-
"bash_code_execution_tool_result",
|
|
216
|
-
"text_editor_code_execution_tool_result",
|
|
217
|
-
"web_fetch_tool_result"
|
|
218
|
-
]),
|
|
219
|
-
tool_use_id: z.string(),
|
|
220
|
-
content: jsonValueSchema
|
|
221
|
-
}),
|
|
222
|
-
z.object({
|
|
223
|
-
type: z.literal("container_upload"),
|
|
224
|
-
file_id: z.string()
|
|
225
|
-
})
|
|
226
|
-
]).transform((value) => {
|
|
227
|
-
if (value.type === "image")
|
|
228
|
-
return {
|
|
229
|
-
type: "image",
|
|
230
|
-
url: value.url,
|
|
231
|
-
...value.mimeType !== undefined ? { mimeType: value.mimeType } : {},
|
|
232
|
-
...value.text !== undefined ? { text: value.text } : {}
|
|
233
|
-
};
|
|
234
|
-
if (value.type === "text")
|
|
235
|
-
return {
|
|
236
|
-
type: "text",
|
|
237
|
-
text: value.text,
|
|
238
|
-
...value.citations !== undefined ? { citations: value.citations } : {}
|
|
239
|
-
};
|
|
240
|
-
return value;
|
|
241
|
-
});
|
|
242
|
-
messageRoleSchema = z.enum([
|
|
243
|
-
"user",
|
|
244
|
-
"assistant",
|
|
245
|
-
"system",
|
|
246
|
-
"developer",
|
|
247
|
-
"tool-call",
|
|
248
|
-
"tool-result",
|
|
249
|
-
"snapshot"
|
|
250
|
-
]);
|
|
251
|
-
toolCallSchema = z.object({
|
|
252
|
-
id: z.string(),
|
|
253
|
-
name: z.string(),
|
|
254
|
-
arguments: jsonValueSchema
|
|
255
|
-
}).strict();
|
|
256
|
-
toolCallInputSchema = z.object({
|
|
257
|
-
id: z.string().optional(),
|
|
258
|
-
name: z.string(),
|
|
259
|
-
arguments: jsonValueSchema.optional()
|
|
260
|
-
}).strict();
|
|
261
|
-
toolErrorCategorySchema = z.enum([
|
|
262
|
-
"validation",
|
|
263
|
-
"permission",
|
|
264
|
-
"not_found",
|
|
265
|
-
"conflict",
|
|
266
|
-
"transient",
|
|
267
|
-
"timeout",
|
|
268
|
-
"cancelled",
|
|
269
|
-
"internal"
|
|
270
|
-
]);
|
|
271
|
-
toolErrorSchema = z.object({
|
|
272
|
-
code: z.string(),
|
|
273
|
-
category: toolErrorCategorySchema,
|
|
274
|
-
retryable: z.boolean(),
|
|
275
|
-
message: z.string(),
|
|
276
|
-
details: jsonValueSchema.optional()
|
|
277
|
-
}).strict();
|
|
278
|
-
toolActionSchema = z.object({
|
|
279
|
-
type: z.enum(["approval", "input"]),
|
|
280
|
-
message: z.string().optional(),
|
|
281
|
-
schema: jsonValueSchema.optional()
|
|
282
|
-
}).strict();
|
|
283
|
-
toolResultSchema = z.object({
|
|
284
|
-
callId: z.string(),
|
|
285
|
-
outcome: z.enum([
|
|
286
|
-
"success",
|
|
287
|
-
"error",
|
|
288
|
-
"action_required"
|
|
289
|
-
]),
|
|
290
|
-
content: jsonValueSchema,
|
|
291
|
-
error: toolErrorSchema.optional(),
|
|
292
|
-
action: toolActionSchema.optional(),
|
|
293
|
-
inputDigest: z.string().optional(),
|
|
294
|
-
outputDigest: z.string().optional()
|
|
295
|
-
}).strict();
|
|
296
|
-
tokenUsageSchema = z.object({
|
|
297
|
-
prompt: z.number().int().min(0),
|
|
298
|
-
completion: z.number().int().min(0),
|
|
299
|
-
total: z.number().int().min(0)
|
|
300
|
-
});
|
|
301
|
-
messageInputSchema = z.object({
|
|
302
|
-
role: messageRoleSchema,
|
|
303
|
-
content: z.union([z.string(), z.array(multiModalContentSchema)]),
|
|
304
|
-
metadata: z.record(z.string(), jsonValueSchema).optional(),
|
|
305
|
-
hidden: z.boolean().optional(),
|
|
306
|
-
toolCall: toolCallSchema.optional(),
|
|
307
|
-
toolResult: toolResultSchema.optional(),
|
|
308
|
-
tokenUsage: tokenUsageSchema.optional(),
|
|
309
|
-
goalCompleted: z.boolean().optional()
|
|
310
|
-
}).strict();
|
|
311
|
-
messageSchema = z.object({
|
|
312
|
-
id: z.string(),
|
|
313
|
-
role: messageRoleSchema,
|
|
314
|
-
content: z.union([z.string(), z.array(multiModalContentSchema)]),
|
|
315
|
-
position: z.number().int().min(0),
|
|
316
|
-
createdAt: z.string(),
|
|
317
|
-
metadata: z.record(z.string(), jsonValueSchema),
|
|
318
|
-
hidden: z.boolean(),
|
|
319
|
-
toolCall: toolCallSchema.optional(),
|
|
320
|
-
toolResult: toolResultSchema.optional(),
|
|
321
|
-
tokenUsage: tokenUsageSchema.optional(),
|
|
322
|
-
goalCompleted: z.boolean().optional()
|
|
323
|
-
}).strict();
|
|
324
|
-
conversationStatusSchema = z.enum([
|
|
325
|
-
"active",
|
|
326
|
-
"archived",
|
|
327
|
-
"deleted"
|
|
328
|
-
]);
|
|
329
|
-
conversationShape = {
|
|
330
|
-
schemaVersion: z.number().int().min(1),
|
|
331
|
-
id: z.string(),
|
|
332
|
-
title: z.string().optional(),
|
|
333
|
-
status: conversationStatusSchema,
|
|
334
|
-
metadata: z.record(z.string(), jsonValueSchema),
|
|
335
|
-
ids: z.array(z.string()),
|
|
336
|
-
messages: z.record(z.string(), messageSchema),
|
|
337
|
-
createdAt: z.string(),
|
|
338
|
-
updatedAt: z.string()
|
|
339
|
-
};
|
|
340
|
-
conversationSchema = z.object(conversationShape).strict();
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
// ../../node_modules/conversationalist/dist/transient-Bk0RaCFf.js
|
|
344
|
-
function getOrderedMessages(conversation) {
|
|
345
|
-
const ordered = [];
|
|
346
|
-
for (const id of conversation.ids) {
|
|
347
|
-
const message = conversation.messages[id];
|
|
348
|
-
if (message)
|
|
349
|
-
ordered.push(message);
|
|
350
|
-
}
|
|
351
|
-
return ordered;
|
|
352
|
-
}
|
|
353
|
-
function toIdRecord(items) {
|
|
354
|
-
const record = {};
|
|
355
|
-
for (const item of items)
|
|
356
|
-
record[item.id] = item;
|
|
357
|
-
return record;
|
|
358
|
-
}
|
|
359
|
-
function createInvalidPositionError(expected, actual) {
|
|
360
|
-
return new ConversationalistError("error:invalid-position", `invalid position: expected ${expected}, got ${actual}`, { context: {
|
|
361
|
-
expected,
|
|
362
|
-
actual
|
|
363
|
-
} });
|
|
364
|
-
}
|
|
365
|
-
function createInvalidToolReferenceError(callId) {
|
|
366
|
-
return new ConversationalistError("error:invalid-tool-reference", `tool result references non-existent tool-call: ${callId}`, { context: { callId } });
|
|
367
|
-
}
|
|
368
|
-
function createSerializationError(message, cause) {
|
|
369
|
-
return new ConversationalistError("error:serialization", message, { cause });
|
|
370
|
-
}
|
|
371
|
-
function createValidationError(message, context, cause) {
|
|
372
|
-
return new ConversationalistError("error:validation", message, {
|
|
373
|
-
context,
|
|
374
|
-
cause
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
function createIntegrityError(message, context) {
|
|
378
|
-
return new ConversationalistError("error:integrity", message, { context });
|
|
379
|
-
}
|
|
380
|
-
function validateConversationHistoryIntegrity(conversation) {
|
|
381
|
-
const issues = [];
|
|
382
|
-
const seenIds = /* @__PURE__ */ new Set;
|
|
383
|
-
conversation.ids.forEach((id, index) => {
|
|
384
|
-
if (seenIds.has(id))
|
|
385
|
-
issues.push({
|
|
386
|
-
code: "integrity:duplicate-message-id",
|
|
387
|
-
message: `duplicate message id in ids: ${id}`,
|
|
388
|
-
data: {
|
|
389
|
-
id,
|
|
390
|
-
position: index
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
else
|
|
394
|
-
seenIds.add(id);
|
|
395
|
-
if (!conversation.messages[id])
|
|
396
|
-
issues.push({
|
|
397
|
-
code: "integrity:missing-message",
|
|
398
|
-
message: `missing message for id ${id}`,
|
|
399
|
-
data: {
|
|
400
|
-
id,
|
|
401
|
-
position: index
|
|
402
|
-
}
|
|
403
|
-
});
|
|
404
|
-
});
|
|
405
|
-
for (const id of Object.keys(conversation.messages))
|
|
406
|
-
if (!seenIds.has(id))
|
|
407
|
-
issues.push({
|
|
408
|
-
code: "integrity:unlisted-message",
|
|
409
|
-
message: `message ${id} is not listed in ids`,
|
|
410
|
-
data: { id }
|
|
411
|
-
});
|
|
412
|
-
const toolCalls = /* @__PURE__ */ new Map;
|
|
413
|
-
conversation.ids.forEach((id, index) => {
|
|
414
|
-
const message = conversation.messages[id];
|
|
415
|
-
if (!message)
|
|
416
|
-
return;
|
|
417
|
-
if (message.role === "tool-call" && message.toolCall)
|
|
418
|
-
if (toolCalls.has(message.toolCall.id))
|
|
419
|
-
issues.push({
|
|
420
|
-
code: "integrity:duplicate-tool-call",
|
|
421
|
-
message: `duplicate toolCall.id ${message.toolCall.id}`,
|
|
422
|
-
data: {
|
|
423
|
-
toolCallId: message.toolCall.id,
|
|
424
|
-
messageId: message.id
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
else
|
|
428
|
-
toolCalls.set(message.toolCall.id, {
|
|
429
|
-
position: index,
|
|
430
|
-
messageId: message.id
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
conversation.ids.forEach((id, index) => {
|
|
434
|
-
const message = conversation.messages[id];
|
|
435
|
-
if (!message)
|
|
436
|
-
return;
|
|
437
|
-
if (message.role === "tool-result" && message.toolResult) {
|
|
438
|
-
const toolCall = toolCalls.get(message.toolResult.callId);
|
|
439
|
-
if (!toolCall)
|
|
440
|
-
issues.push({
|
|
441
|
-
code: "integrity:orphan-tool-result",
|
|
442
|
-
message: `tool-result references missing tool-call ${message.toolResult.callId}`,
|
|
443
|
-
data: {
|
|
444
|
-
callId: message.toolResult.callId,
|
|
445
|
-
messageId: message.id
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
else if (toolCall.position >= index)
|
|
449
|
-
issues.push({
|
|
450
|
-
code: "integrity:tool-result-before-call",
|
|
451
|
-
message: `tool-result ${message.toolResult.callId} occurs before tool-call`,
|
|
452
|
-
data: {
|
|
453
|
-
callId: message.toolResult.callId,
|
|
454
|
-
messageId: message.id,
|
|
455
|
-
toolCallMessageId: toolCall.messageId
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
return issues;
|
|
461
|
-
}
|
|
462
|
-
function assertConversationHistoryIntegrity(conversation) {
|
|
463
|
-
const issues = validateConversationHistoryIntegrity(conversation);
|
|
464
|
-
if (issues.length === 0)
|
|
465
|
-
return;
|
|
466
|
-
throw createIntegrityError("conversation integrity check failed", { issues });
|
|
467
|
-
}
|
|
468
|
-
function assertConversationSafe(conversation) {
|
|
469
|
-
const parsed = conversationSchema.safeParse(conversation);
|
|
470
|
-
if (!parsed.success)
|
|
471
|
-
throw createValidationError("conversation failed schema validation", { issues: parsed.error.issues });
|
|
472
|
-
assertConversationHistoryIntegrity(conversation);
|
|
473
|
-
}
|
|
474
|
-
function ensureConversationSafe(conversation) {
|
|
475
|
-
assertConversationSafe(conversation);
|
|
476
|
-
return conversation;
|
|
477
|
-
}
|
|
478
|
-
var ConversationalistError;
|
|
479
|
-
var init_transient_Bk0RaCFf = __esm(() => {
|
|
480
|
-
init_message_BEjqY7Jv();
|
|
481
|
-
init_schemas();
|
|
482
|
-
ConversationalistError = class ConversationalistError2 extends Error {
|
|
483
|
-
code;
|
|
484
|
-
context;
|
|
485
|
-
cause;
|
|
486
|
-
constructor(code, message, options) {
|
|
487
|
-
super(message);
|
|
488
|
-
this.name = "ConversationalistError";
|
|
489
|
-
this.code = code;
|
|
490
|
-
this.context = options?.context;
|
|
491
|
-
this.cause = options?.cause;
|
|
492
|
-
if (Error.captureStackTrace)
|
|
493
|
-
Error.captureStackTrace(this, ConversationalistError2);
|
|
494
|
-
}
|
|
495
|
-
toDetailedString() {
|
|
496
|
-
const parts = [`[${this.code}] ${this.message}`];
|
|
497
|
-
if (this.context && Object.keys(this.context).length > 0)
|
|
498
|
-
parts.push(`Context: ${JSON.stringify(this.context, null, 2)}`);
|
|
499
|
-
if (this.cause)
|
|
500
|
-
parts.push(`Caused by: ${this.cause.message}`);
|
|
501
|
-
return parts.join(`
|
|
502
|
-
`);
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
// ../../node_modules/conversationalist/dist/append-l_g0hmrj.js
|
|
508
|
-
function normalizeContent(content) {
|
|
509
|
-
if (content === undefined)
|
|
510
|
-
return;
|
|
511
|
-
if (typeof content === "string")
|
|
512
|
-
return content;
|
|
513
|
-
return Array.isArray(content) ? content : [content];
|
|
514
|
-
}
|
|
515
|
-
function materializeToolCall(toolCall, options = {}) {
|
|
516
|
-
return {
|
|
517
|
-
id: toolCall.id ?? options.generateId?.() ?? crypto.randomUUID(),
|
|
518
|
-
name: toolCall.name,
|
|
519
|
-
arguments: normalizeJSONValue(toolCall.arguments ?? {})
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
function materializeToolCalls(toolCalls, options = {}) {
|
|
523
|
-
return toolCalls.map((toolCall) => materializeToolCall(toolCall, options));
|
|
524
|
-
}
|
|
525
|
-
function materializeToolResult(toolResult) {
|
|
526
|
-
if (hasStreamingPayload(toolResult))
|
|
527
|
-
throw new Error("materializeToolResult does not support streaming tool results. Use materializeToolResultAsync or materializeToolResultsAsync.");
|
|
528
|
-
return stripRuntimeToolResultFields(toolResult, normalizeJSONValue(toolResult.content));
|
|
529
|
-
}
|
|
530
|
-
function materializeToolResults(toolResults) {
|
|
531
|
-
return toolResults.map((toolResult) => materializeToolResult(toolResult));
|
|
532
|
-
}
|
|
533
|
-
async function materializeToolResultAsync(toolResult) {
|
|
534
|
-
const streamingPayload = getStreamingPayload(toolResult);
|
|
535
|
-
if (!streamingPayload)
|
|
536
|
-
return stripRuntimeToolResultFields(toolResult, normalizeJSONValue(toolResult.content));
|
|
537
|
-
return stripRuntimeToolResultFields(toolResult, normalizeJSONValue(await collectAsyncIterable(streamingPayload)));
|
|
538
|
-
}
|
|
539
|
-
async function materializeToolResultsAsync(toolResults) {
|
|
540
|
-
return Promise.all(toolResults.map((toolResult) => materializeToolResultAsync(toolResult)));
|
|
541
|
-
}
|
|
542
|
-
function stripRuntimeToolResultFields(toolResult, content) {
|
|
543
|
-
return {
|
|
544
|
-
callId: toolResult.callId,
|
|
545
|
-
outcome: toolResult.outcome,
|
|
546
|
-
content,
|
|
547
|
-
...toolResult.error ? { error: {
|
|
548
|
-
code: toolResult.error.code,
|
|
549
|
-
category: toolResult.error.category,
|
|
550
|
-
retryable: toolResult.error.retryable,
|
|
551
|
-
message: toolResult.error.message,
|
|
552
|
-
...toolResult.error.details !== undefined ? { details: normalizeJSONValue(toolResult.error.details) } : {}
|
|
553
|
-
} } : {},
|
|
554
|
-
...toolResult.action ? { action: normalizeToolAction(toolResult.action) } : {},
|
|
555
|
-
...toolResult.inputDigest ? { inputDigest: toolResult.inputDigest } : {},
|
|
556
|
-
...toolResult.outputDigest ? { outputDigest: toolResult.outputDigest } : {}
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
function normalizeToolAction(action) {
|
|
560
|
-
return {
|
|
561
|
-
type: action.type,
|
|
562
|
-
...action.message ? { message: action.message } : {},
|
|
563
|
-
...action.schema !== undefined ? { schema: normalizeJSONValue(action.schema) } : {}
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
function hasStreamingPayload(toolResult) {
|
|
567
|
-
return getStreamingPayload(toolResult) !== undefined;
|
|
568
|
-
}
|
|
569
|
-
function getStreamingPayload(toolResult) {
|
|
570
|
-
if (toolResult.stream)
|
|
571
|
-
return toolResult.stream;
|
|
572
|
-
if (isAsyncIterable(toolResult.result))
|
|
573
|
-
return toolResult.result;
|
|
574
|
-
}
|
|
575
|
-
function isAsyncIterable(value) {
|
|
576
|
-
if (!value || typeof value !== "object" && typeof value !== "function")
|
|
577
|
-
return false;
|
|
578
|
-
return Symbol.asyncIterator in value;
|
|
579
|
-
}
|
|
580
|
-
function normalizeJSONValue(value) {
|
|
581
|
-
if (value === undefined)
|
|
582
|
-
return null;
|
|
583
|
-
try {
|
|
584
|
-
assertJSONValue(value, "tool materialization");
|
|
585
|
-
return value;
|
|
586
|
-
} catch {
|
|
587
|
-
try {
|
|
588
|
-
const serialized = JSON.stringify(value);
|
|
589
|
-
return serialized === undefined ? stringifyNonJSON(value) : JSON.parse(serialized);
|
|
590
|
-
} catch {
|
|
591
|
-
return stringifyNonJSON(value);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
function stringifyNonJSON(value) {
|
|
596
|
-
return String(value);
|
|
597
|
-
}
|
|
598
|
-
function assertJSONValue(value, path) {
|
|
599
|
-
const stack = /* @__PURE__ */ new WeakSet;
|
|
600
|
-
const walk = (current, currentPath) => {
|
|
601
|
-
if (current === null)
|
|
602
|
-
return;
|
|
603
|
-
const type = typeof current;
|
|
604
|
-
if (type === "string" || type === "boolean")
|
|
605
|
-
return;
|
|
606
|
-
if (type === "number") {
|
|
607
|
-
if (Number.isFinite(current))
|
|
608
|
-
return;
|
|
609
|
-
throw new TypeError(`Non-finite number at ${currentPath}`);
|
|
610
|
-
}
|
|
611
|
-
if (type === "undefined" || type === "bigint" || type === "function" || type === "symbol")
|
|
612
|
-
throw new TypeError(`Invalid JSON value at ${currentPath}`);
|
|
613
|
-
if (Array.isArray(current)) {
|
|
614
|
-
if (stack.has(current))
|
|
615
|
-
throw new TypeError(`Circular reference detected at ${currentPath}`);
|
|
616
|
-
stack.add(current);
|
|
617
|
-
for (let index = 0;index < current.length; index += 1)
|
|
618
|
-
walk(current[index], `${currentPath}[${index}]`);
|
|
619
|
-
stack.delete(current);
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
if (type === "object") {
|
|
623
|
-
if (!isPlainObject2(current))
|
|
624
|
-
throw new TypeError(`Non-plain object is not valid JSON at ${currentPath}`);
|
|
625
|
-
const record = current;
|
|
626
|
-
if (stack.has(record))
|
|
627
|
-
throw new TypeError(`Circular reference detected at ${currentPath}`);
|
|
628
|
-
stack.add(record);
|
|
629
|
-
for (const key of Object.keys(record))
|
|
630
|
-
walk(record[key], `${currentPath}.${key}`);
|
|
631
|
-
stack.delete(record);
|
|
632
|
-
}
|
|
633
|
-
};
|
|
634
|
-
walk(value, path);
|
|
635
|
-
}
|
|
636
|
-
function isPlainObject2(value) {
|
|
637
|
-
if (!value || typeof value !== "object")
|
|
638
|
-
return false;
|
|
639
|
-
const prototype = Reflect.getPrototypeOf(value);
|
|
640
|
-
return prototype === Object.prototype || prototype === null;
|
|
641
|
-
}
|
|
642
|
-
async function collectAsyncIterable(stream) {
|
|
643
|
-
const chunks = [];
|
|
644
|
-
for await (const chunk of stream)
|
|
645
|
-
chunks.push(chunk);
|
|
646
|
-
return chunks;
|
|
647
|
-
}
|
|
648
|
-
function pairToolCallsWithResults(messages) {
|
|
649
|
-
const isMessageArray = (value) => Array.isArray(value);
|
|
650
|
-
const ordered = isMessageArray(messages) ? messages : Object.values(messages).sort((a, b) => a.position - b.position);
|
|
651
|
-
const pairs = [];
|
|
652
|
-
const resultsMap = /* @__PURE__ */ new Map;
|
|
653
|
-
for (const msg of ordered)
|
|
654
|
-
if (msg.toolResult)
|
|
655
|
-
resultsMap.set(msg.toolResult.callId, msg.toolResult);
|
|
656
|
-
for (const msg of ordered)
|
|
657
|
-
if (msg.toolCall)
|
|
658
|
-
pairs.push({
|
|
659
|
-
call: msg.toolCall,
|
|
660
|
-
result: resultsMap.get(msg.toolCall.id)
|
|
661
|
-
});
|
|
662
|
-
return pairs;
|
|
663
|
-
}
|
|
664
|
-
function appendToolCall(conversation, toolCall, options, environment) {
|
|
665
|
-
const resolvedEnvironment = resolveConversationEnvironment(isConversationEnvironmentParameter(options) ? options : environment);
|
|
666
|
-
const resolvedOptions = isConversationEnvironmentParameter(options) ? undefined : options;
|
|
667
|
-
return appendMessages(conversation, createToolCallMessageInput(materializeToolCall(toolCall, { generateId: resolvedEnvironment.randomId }), resolvedOptions), resolvedEnvironment);
|
|
668
|
-
}
|
|
669
|
-
function appendToolCalls(conversation, toolCalls, environment) {
|
|
670
|
-
if (toolCalls.length === 0)
|
|
671
|
-
return conversation;
|
|
672
|
-
const resolvedEnvironment = resolveConversationEnvironment(environment);
|
|
673
|
-
return appendMessages(conversation, ...materializeToolCalls(toolCalls, { generateId: resolvedEnvironment.randomId }).map((toolCall) => createToolCallMessageInput(toolCall, undefined)), resolvedEnvironment);
|
|
674
|
-
}
|
|
675
|
-
function appendToolResult(conversation, toolResult, options, environment) {
|
|
676
|
-
const resolvedOptions = isConversationEnvironmentParameter(options) ? undefined : options;
|
|
677
|
-
return appendMessages(conversation, createToolResultMessageInput(materializeToolResult(toolResult), resolvedOptions), isConversationEnvironmentParameter(options) ? options : environment);
|
|
678
|
-
}
|
|
679
|
-
function appendToolResults(conversation, toolResults, environment) {
|
|
680
|
-
if (toolResults.length === 0)
|
|
681
|
-
return conversation;
|
|
682
|
-
return appendMessages(conversation, ...materializeToolResults(toolResults).map((toolResult) => createToolResultMessageInput(toolResult, undefined)), environment);
|
|
683
|
-
}
|
|
684
|
-
async function appendToolResultAsync(conversation, toolResult, options, environment) {
|
|
685
|
-
const resolvedOptions = isConversationEnvironmentParameter(options) ? undefined : options;
|
|
686
|
-
return appendMessages(conversation, createToolResultMessageInput(await materializeToolResultAsync(toolResult), resolvedOptions), isConversationEnvironmentParameter(options) ? options : environment);
|
|
687
|
-
}
|
|
688
|
-
async function appendToolResultsAsync(conversation, toolResults, environment) {
|
|
689
|
-
if (toolResults.length === 0)
|
|
690
|
-
return conversation;
|
|
691
|
-
return appendMessages(conversation, ...(await materializeToolResultsAsync(toolResults)).map((toolResult) => createToolResultMessageInput(toolResult, undefined)), environment);
|
|
692
|
-
}
|
|
693
|
-
function getPendingToolCalls(conversation) {
|
|
694
|
-
const orderedMessages = getOrderedMessages(conversation);
|
|
695
|
-
const completedCallIdentifiers = /* @__PURE__ */ new Set;
|
|
696
|
-
for (const message of orderedMessages)
|
|
697
|
-
if (message.role === "tool-result" && message.toolResult)
|
|
698
|
-
completedCallIdentifiers.add(message.toolResult.callId);
|
|
699
|
-
const pendingToolCalls = [];
|
|
700
|
-
for (const message of orderedMessages)
|
|
701
|
-
if (message.role === "tool-call" && message.toolCall) {
|
|
702
|
-
if (!completedCallIdentifiers.has(message.toolCall.id))
|
|
703
|
-
pendingToolCalls.push(message.toolCall);
|
|
704
|
-
}
|
|
705
|
-
return pendingToolCalls;
|
|
706
|
-
}
|
|
707
|
-
function getToolInteractions(conversation) {
|
|
708
|
-
return pairToolCallsWithResults(getOrderedMessages(conversation));
|
|
709
|
-
}
|
|
710
|
-
function createToolCallMessageInput(toolCall, options) {
|
|
711
|
-
return {
|
|
712
|
-
role: "tool-call",
|
|
713
|
-
content: options?.content ?? "",
|
|
714
|
-
metadata: options?.metadata,
|
|
715
|
-
hidden: options?.hidden,
|
|
716
|
-
toolCall,
|
|
717
|
-
tokenUsage: options?.tokenUsage
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
function createToolResultMessageInput(toolResult, options) {
|
|
721
|
-
return {
|
|
722
|
-
role: "tool-result",
|
|
723
|
-
content: options?.content ?? "",
|
|
724
|
-
metadata: options?.metadata,
|
|
725
|
-
hidden: options?.hidden,
|
|
726
|
-
toolResult,
|
|
727
|
-
tokenUsage: options?.tokenUsage
|
|
728
|
-
};
|
|
729
|
-
}
|
|
730
|
-
function toSessionInfo(conversation) {
|
|
731
|
-
return {
|
|
732
|
-
id: conversation.id,
|
|
733
|
-
...conversation.title !== undefined ? { title: conversation.title } : {},
|
|
734
|
-
tags: conversation.metadata["_tags"] ?? [],
|
|
735
|
-
createdAt: conversation.createdAt,
|
|
736
|
-
updatedAt: conversation.updatedAt,
|
|
737
|
-
messageCount: conversation.ids.length
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
function partCharLength(part) {
|
|
741
|
-
switch (part.type) {
|
|
742
|
-
case "text":
|
|
743
|
-
return part.text.length + (part.citations !== undefined ? JSON.stringify(part.citations).length : 0);
|
|
744
|
-
case "image":
|
|
745
|
-
return (part.text ?? "").length + (part.url?.length ?? 0);
|
|
746
|
-
case "thinking":
|
|
747
|
-
return part.thinking.length + part.signature.length;
|
|
748
|
-
case "redacted_thinking":
|
|
749
|
-
return part.data.length;
|
|
750
|
-
case "server_tool_use":
|
|
751
|
-
return part.name.length + JSON.stringify(part.input).length;
|
|
752
|
-
case "web_search_tool_result":
|
|
753
|
-
return JSON.stringify(part.content).length;
|
|
754
|
-
case "code_execution_tool_result":
|
|
755
|
-
case "bash_code_execution_tool_result":
|
|
756
|
-
case "text_editor_code_execution_tool_result":
|
|
757
|
-
case "web_fetch_tool_result":
|
|
758
|
-
return JSON.stringify(part.content).length;
|
|
759
|
-
case "container_upload":
|
|
760
|
-
return part.file_id.length;
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
function simpleTokenEstimator(message) {
|
|
764
|
-
if (typeof message.content === "string")
|
|
765
|
-
return Math.ceil(message.content.length / 4);
|
|
766
|
-
const total = messageParts(message).reduce((sum, part) => sum + partCharLength(part), 0);
|
|
767
|
-
return Math.ceil(total / 4);
|
|
768
|
-
}
|
|
769
|
-
function resolveConversationEnvironment(environment) {
|
|
770
|
-
return {
|
|
771
|
-
now: environment?.now ?? defaultConversationEnvironment.now,
|
|
772
|
-
randomId: environment?.randomId ?? defaultConversationEnvironment.randomId,
|
|
773
|
-
estimateTokens: environment?.estimateTokens ?? defaultConversationEnvironment.estimateTokens,
|
|
774
|
-
plugins: [...environment?.plugins ?? defaultConversationEnvironment.plugins],
|
|
775
|
-
...environment?.persistence ? { persistence: environment.persistence } : {},
|
|
776
|
-
...environment?.persistenceDebounceMilliseconds !== undefined ? { persistenceDebounceMilliseconds: environment.persistenceDebounceMilliseconds } : {},
|
|
777
|
-
...environment?.setTimeoutFunction ? { setTimeoutFunction: environment.setTimeoutFunction } : {},
|
|
778
|
-
...environment?.clearTimeoutFunction ? { clearTimeoutFunction: environment.clearTimeoutFunction } : {},
|
|
779
|
-
...environment?.maxHistoryDepth !== undefined ? { maxHistoryDepth: environment.maxHistoryDepth } : {}
|
|
780
|
-
};
|
|
781
|
-
}
|
|
782
|
-
function isConversationEnvironmentParameter(value) {
|
|
783
|
-
if (!value || typeof value !== "object" || value === null)
|
|
784
|
-
return false;
|
|
785
|
-
if ("role" in value)
|
|
786
|
-
return false;
|
|
787
|
-
const candidate = value;
|
|
788
|
-
return typeof candidate["now"] === "function" || typeof candidate["randomId"] === "function" || typeof candidate["estimateTokens"] === "function" || Array.isArray(candidate["plugins"]) && candidate["plugins"].length > 0 || typeof candidate["persistence"] === "object" && candidate["persistence"] !== null;
|
|
789
|
-
}
|
|
790
|
-
function partitionAppendArgs(args) {
|
|
791
|
-
const filtered = args.filter((arg) => arg !== undefined);
|
|
792
|
-
if (filtered.length === 0)
|
|
793
|
-
return { inputs: [] };
|
|
794
|
-
const last = filtered[filtered.length - 1];
|
|
795
|
-
if (isConversationEnvironmentParameter(last))
|
|
796
|
-
return {
|
|
797
|
-
inputs: filtered.slice(0, -1),
|
|
798
|
-
environment: last
|
|
799
|
-
};
|
|
800
|
-
return { inputs: filtered };
|
|
801
|
-
}
|
|
802
|
-
function appendMessages(conversation, ...args) {
|
|
803
|
-
return appendMessagesInternal(conversation, args, true);
|
|
804
|
-
}
|
|
805
|
-
function appendUserMessage(conversation, content, metadata, environment) {
|
|
806
|
-
const resolvedEnvironment = isConversationEnvironmentParameter(metadata) ? metadata : environment;
|
|
807
|
-
return appendMessages(conversation, {
|
|
808
|
-
role: "user",
|
|
809
|
-
content,
|
|
810
|
-
metadata: isConversationEnvironmentParameter(metadata) ? undefined : metadata
|
|
811
|
-
}, resolvedEnvironment);
|
|
812
|
-
}
|
|
813
|
-
function appendAssistantMessage(conversation, content, metadata, environment) {
|
|
814
|
-
const resolvedEnvironment = isConversationEnvironmentParameter(metadata) ? metadata : environment;
|
|
815
|
-
return appendMessages(conversation, {
|
|
816
|
-
role: "assistant",
|
|
817
|
-
content,
|
|
818
|
-
metadata: isConversationEnvironmentParameter(metadata) ? undefined : metadata
|
|
819
|
-
}, resolvedEnvironment);
|
|
820
|
-
}
|
|
821
|
-
function appendSystemMessage(conversation, content, metadata, environment) {
|
|
822
|
-
const resolvedEnvironment = isConversationEnvironmentParameter(metadata) ? metadata : environment;
|
|
823
|
-
return appendMessages(conversation, {
|
|
824
|
-
role: "system",
|
|
825
|
-
content,
|
|
826
|
-
metadata: isConversationEnvironmentParameter(metadata) ? undefined : metadata
|
|
827
|
-
}, resolvedEnvironment);
|
|
828
|
-
}
|
|
829
|
-
var defaultConversationEnvironment, buildToolUseIndex = (messages) => messages.reduce((index, message) => {
|
|
830
|
-
if (message.role === "tool-call" && message.toolCall)
|
|
831
|
-
index.set(message.toolCall.id, { name: message.toolCall.name });
|
|
832
|
-
return index;
|
|
833
|
-
}, /* @__PURE__ */ new Map), registerToolUse = (index, toolCall) => {
|
|
834
|
-
const next = new Map(index);
|
|
835
|
-
next.set(toolCall.id, { name: toolCall.name });
|
|
836
|
-
return next;
|
|
837
|
-
}, assertToolReference = (index, callId) => {
|
|
838
|
-
if (!index.has(callId))
|
|
839
|
-
throw createInvalidToolReferenceError(callId);
|
|
840
|
-
}, appendMessagesInternal = (conversation, args, validate) => {
|
|
841
|
-
const { inputs, environment } = partitionAppendArgs(args);
|
|
842
|
-
const resolvedEnvironment = resolveConversationEnvironment(environment);
|
|
843
|
-
const now = resolvedEnvironment.now();
|
|
844
|
-
const startPosition = conversation.ids.length;
|
|
845
|
-
const initialToolUses = validate ? buildToolUseIndex(getOrderedMessages(conversation)) : /* @__PURE__ */ new Map;
|
|
846
|
-
const { messages } = inputs.reduce((state, input, index) => {
|
|
847
|
-
const processedInput = resolvedEnvironment.plugins.reduce((acc, plugin) => plugin(acc), input);
|
|
848
|
-
if (validate && processedInput.role === "tool-result" && processedInput.toolResult)
|
|
849
|
-
assertToolReference(state.toolUses, processedInput.toolResult.callId);
|
|
850
|
-
const normalizedContent = normalizeContent(processedInput.content);
|
|
851
|
-
const baseMessage = {
|
|
852
|
-
id: resolvedEnvironment.randomId(),
|
|
853
|
-
role: processedInput.role,
|
|
854
|
-
content: normalizedContent,
|
|
855
|
-
position: startPosition + index,
|
|
856
|
-
createdAt: now,
|
|
857
|
-
metadata: { ...processedInput.metadata ?? {} },
|
|
858
|
-
hidden: processedInput.hidden ?? false,
|
|
859
|
-
toolCall: processedInput.toolCall,
|
|
860
|
-
toolResult: processedInput.toolResult,
|
|
861
|
-
tokenUsage: processedInput.tokenUsage
|
|
862
|
-
};
|
|
863
|
-
let message;
|
|
864
|
-
if (processedInput.role === "assistant")
|
|
865
|
-
message = createMessage({
|
|
866
|
-
...baseMessage,
|
|
867
|
-
role: "assistant",
|
|
868
|
-
goalCompleted: processedInput.goalCompleted
|
|
869
|
-
});
|
|
870
|
-
else
|
|
871
|
-
message = createMessage(baseMessage);
|
|
872
|
-
let toolUses = state.toolUses;
|
|
873
|
-
if (processedInput.role === "tool-call" && processedInput.toolCall) {
|
|
874
|
-
if (validate && state.toolUses.has(processedInput.toolCall.id))
|
|
875
|
-
throw createIntegrityError("duplicate toolCall.id in conversation", {
|
|
876
|
-
toolCallId: processedInput.toolCall.id,
|
|
877
|
-
messageId: baseMessage.id
|
|
878
|
-
});
|
|
879
|
-
toolUses = validate ? registerToolUse(state.toolUses, processedInput.toolCall) : state.toolUses;
|
|
880
|
-
}
|
|
881
|
-
return {
|
|
882
|
-
toolUses,
|
|
883
|
-
messages: [...state.messages, message]
|
|
884
|
-
};
|
|
885
|
-
}, {
|
|
886
|
-
toolUses: initialToolUses,
|
|
887
|
-
messages: []
|
|
888
|
-
});
|
|
889
|
-
const messageIds = messages.map((message) => message.id);
|
|
890
|
-
const readonly = toReadonly({
|
|
891
|
-
...conversation,
|
|
892
|
-
ids: [...conversation.ids, ...messageIds],
|
|
893
|
-
messages: {
|
|
894
|
-
...conversation.messages,
|
|
895
|
-
...toIdRecord(messages)
|
|
896
|
-
},
|
|
897
|
-
updatedAt: now
|
|
898
|
-
});
|
|
899
|
-
return validate ? ensureConversationSafe(readonly) : readonly;
|
|
900
|
-
};
|
|
901
|
-
var init_append_l_g0hmrj = __esm(() => {
|
|
902
|
-
init_message_BEjqY7Jv();
|
|
903
|
-
init_transient_Bk0RaCFf();
|
|
904
|
-
defaultConversationEnvironment = {
|
|
905
|
-
now: () => (/* @__PURE__ */ new Date()).toISOString(),
|
|
906
|
-
randomId: () => crypto.randomUUID(),
|
|
907
|
-
estimateTokens: simpleTokenEstimator,
|
|
908
|
-
plugins: []
|
|
909
|
-
};
|
|
910
|
-
});
|
|
911
|
-
|
|
912
|
-
// ../../node_modules/conversationalist/dist/types-C6g2sHGB.js
|
|
913
|
-
var init_types_C6g2sHGB = () => {};
|
|
914
|
-
|
|
915
|
-
// ../../node_modules/conversationalist/dist/tool-results-DKQs9ZGX.js
|
|
916
|
-
function redactToolResult(toolResult, placeholder) {
|
|
917
|
-
return {
|
|
918
|
-
...toolResult,
|
|
919
|
-
content: placeholder
|
|
920
|
-
};
|
|
921
|
-
}
|
|
922
|
-
var init_tool_results_DKQs9ZGX = () => {};
|
|
923
|
-
|
|
924
|
-
// ../../node_modules/conversationalist/dist/conversation-7sbYchzO.js
|
|
925
|
-
function createConversationHistory(options, environment) {
|
|
926
|
-
const resolvedEnvironment = resolveConversationEnvironment(environment);
|
|
927
|
-
const now = resolvedEnvironment.now();
|
|
928
|
-
return ensureConversationSafe(toReadonly({
|
|
929
|
-
schemaVersion: 4,
|
|
930
|
-
id: options?.id ?? resolvedEnvironment.randomId(),
|
|
931
|
-
title: options?.title,
|
|
932
|
-
status: options?.status ?? "active",
|
|
933
|
-
metadata: { ...options?.metadata ?? {} },
|
|
934
|
-
ids: [],
|
|
935
|
-
messages: {},
|
|
936
|
-
createdAt: now,
|
|
937
|
-
updatedAt: now
|
|
938
|
-
}));
|
|
939
|
-
}
|
|
940
|
-
function getMessages(conversation, options) {
|
|
941
|
-
const includeHidden = options?.includeHidden ?? false;
|
|
942
|
-
const ordered = getOrderedMessages(conversation);
|
|
943
|
-
return includeHidden ? ordered : ordered.filter((m) => !m.hidden);
|
|
944
|
-
}
|
|
945
|
-
function getMessageAtPosition(conversation, position) {
|
|
946
|
-
const id = conversation.ids[position];
|
|
947
|
-
return id ? conversation.messages[id] : undefined;
|
|
948
|
-
}
|
|
949
|
-
function getMessageIds(conversation) {
|
|
950
|
-
return [...conversation.ids];
|
|
951
|
-
}
|
|
952
|
-
function getMessageById(conversation, id) {
|
|
953
|
-
return conversation.messages[id];
|
|
954
|
-
}
|
|
955
|
-
function searchConversationMessages(conversation, predicate) {
|
|
956
|
-
return getOrderedMessages(conversation).filter(predicate);
|
|
957
|
-
}
|
|
958
|
-
function getStatistics(conversation) {
|
|
959
|
-
const ordered = getOrderedMessages(conversation);
|
|
960
|
-
const stats = ordered.reduce((acc, message) => {
|
|
961
|
-
return {
|
|
962
|
-
byRole: {
|
|
963
|
-
...acc.byRole,
|
|
964
|
-
[message.role]: (acc.byRole[message.role] ?? 0) + 1
|
|
965
|
-
},
|
|
966
|
-
hidden: acc.hidden + (message.hidden ? 1 : 0),
|
|
967
|
-
withImages: acc.withImages + (messageHasImages(message) ? 1 : 0)
|
|
968
|
-
};
|
|
969
|
-
}, {
|
|
970
|
-
byRole: {},
|
|
971
|
-
hidden: 0,
|
|
972
|
-
withImages: 0
|
|
973
|
-
});
|
|
974
|
-
return {
|
|
975
|
-
total: ordered.length,
|
|
976
|
-
...stats
|
|
977
|
-
};
|
|
978
|
-
}
|
|
979
|
-
function hasSystemMessage(conversation) {
|
|
980
|
-
return getOrderedMessages(conversation).some((m) => m.role === "system");
|
|
981
|
-
}
|
|
982
|
-
function getFirstSystemMessage(conversation) {
|
|
983
|
-
return getOrderedMessages(conversation).find((m) => m.role === "system");
|
|
984
|
-
}
|
|
985
|
-
function getSystemMessages(conversation) {
|
|
986
|
-
return getOrderedMessages(conversation).filter((m) => m.role === "system");
|
|
987
|
-
}
|
|
988
|
-
function prependSystemMessage(conversation, content, metadata, environment) {
|
|
989
|
-
const resolvedEnvironment = resolveConversationEnvironment(isConversationEnvironmentParameter(metadata) ? metadata : environment);
|
|
990
|
-
const resolvedMetadata = isConversationEnvironmentParameter(metadata) ? undefined : metadata;
|
|
991
|
-
const now = resolvedEnvironment.now();
|
|
992
|
-
const newMessage = createMessage({
|
|
993
|
-
id: resolvedEnvironment.randomId(),
|
|
994
|
-
role: "system",
|
|
995
|
-
content,
|
|
996
|
-
position: 0,
|
|
997
|
-
createdAt: now,
|
|
998
|
-
metadata: { ...resolvedMetadata ?? {} },
|
|
999
|
-
hidden: false,
|
|
1000
|
-
toolCall: undefined,
|
|
1001
|
-
toolResult: undefined,
|
|
1002
|
-
tokenUsage: undefined
|
|
1003
|
-
});
|
|
1004
|
-
const ordered = getOrderedMessages(conversation);
|
|
1005
|
-
const renumberedMessages = ordered.map((message) => createMessage({
|
|
1006
|
-
id: message.id,
|
|
1007
|
-
role: message.role,
|
|
1008
|
-
content: copyContent(message.content),
|
|
1009
|
-
position: message.position + 1,
|
|
1010
|
-
createdAt: message.createdAt,
|
|
1011
|
-
metadata: { ...message.metadata },
|
|
1012
|
-
hidden: message.hidden,
|
|
1013
|
-
toolCall: message.toolCall,
|
|
1014
|
-
toolResult: message.toolResult,
|
|
1015
|
-
tokenUsage: message.tokenUsage
|
|
1016
|
-
}));
|
|
1017
|
-
return ensureConversationSafe(toReadonly({
|
|
1018
|
-
...conversation,
|
|
1019
|
-
ids: [newMessage.id, ...ordered.map((message) => message.id)],
|
|
1020
|
-
messages: toIdRecord([newMessage, ...renumberedMessages]),
|
|
1021
|
-
updatedAt: now
|
|
1022
|
-
}));
|
|
1023
|
-
}
|
|
1024
|
-
function replaceSystemMessage(conversation, content, metadata, environment) {
|
|
1025
|
-
const resolvedEnvironment = resolveConversationEnvironment(isConversationEnvironmentParameter(metadata) ? metadata : environment);
|
|
1026
|
-
const resolvedMetadata = isConversationEnvironmentParameter(metadata) ? undefined : metadata;
|
|
1027
|
-
const now = resolvedEnvironment.now();
|
|
1028
|
-
const ordered = getOrderedMessages(conversation);
|
|
1029
|
-
const firstSystemIndex = ordered.findIndex((m) => m.role === "system");
|
|
1030
|
-
if (firstSystemIndex === -1)
|
|
1031
|
-
return prependSystemMessage(conversation, content, resolvedMetadata, resolvedEnvironment);
|
|
1032
|
-
const original = ordered[firstSystemIndex];
|
|
1033
|
-
const replaced = createMessage({
|
|
1034
|
-
id: original.id,
|
|
1035
|
-
role: "system",
|
|
1036
|
-
content,
|
|
1037
|
-
position: original.position,
|
|
1038
|
-
createdAt: original.createdAt,
|
|
1039
|
-
metadata: { ...resolvedMetadata ?? original.metadata },
|
|
1040
|
-
hidden: original.hidden,
|
|
1041
|
-
toolCall: undefined,
|
|
1042
|
-
toolResult: undefined,
|
|
1043
|
-
tokenUsage: undefined
|
|
1044
|
-
});
|
|
1045
|
-
return ensureConversationSafe(toReadonly({
|
|
1046
|
-
...conversation,
|
|
1047
|
-
ids: [...conversation.ids],
|
|
1048
|
-
messages: {
|
|
1049
|
-
...conversation.messages,
|
|
1050
|
-
[replaced.id]: replaced
|
|
1051
|
-
},
|
|
1052
|
-
updatedAt: now
|
|
1053
|
-
}));
|
|
1054
|
-
}
|
|
1055
|
-
function collapseSystemMessages(conversation, environment) {
|
|
1056
|
-
const ordered = getOrderedMessages(conversation);
|
|
1057
|
-
const systemMessages = ordered.filter((m) => m.role === "system");
|
|
1058
|
-
if (systemMessages.length <= 1)
|
|
1059
|
-
return ensureConversationSafe(conversation);
|
|
1060
|
-
const now = resolveConversationEnvironment(environment).now();
|
|
1061
|
-
const { parts } = systemMessages.reduce((state, message) => {
|
|
1062
|
-
const contentStr = typeof message.content === "string" ? message.content : message.content.map((part) => part.type === "text" ? part.text : "").join("");
|
|
1063
|
-
if (!contentStr || state.seen.has(contentStr))
|
|
1064
|
-
return state;
|
|
1065
|
-
const seen = new Set(state.seen);
|
|
1066
|
-
seen.add(contentStr);
|
|
1067
|
-
return {
|
|
1068
|
-
seen,
|
|
1069
|
-
parts: [...state.parts, contentStr]
|
|
1070
|
-
};
|
|
1071
|
-
}, {
|
|
1072
|
-
seen: /* @__PURE__ */ new Set,
|
|
1073
|
-
parts: []
|
|
1074
|
-
});
|
|
1075
|
-
const collapsedContent = parts.join(`
|
|
1076
|
-
`);
|
|
1077
|
-
const firstSystemMsg = systemMessages[0];
|
|
1078
|
-
const collapsed = createMessage({
|
|
1079
|
-
id: firstSystemMsg.id,
|
|
1080
|
-
role: "system",
|
|
1081
|
-
content: collapsedContent,
|
|
1082
|
-
position: firstSystemMsg.position,
|
|
1083
|
-
createdAt: firstSystemMsg.createdAt,
|
|
1084
|
-
metadata: { ...firstSystemMsg.metadata },
|
|
1085
|
-
hidden: firstSystemMsg.hidden,
|
|
1086
|
-
toolCall: undefined,
|
|
1087
|
-
toolResult: undefined,
|
|
1088
|
-
tokenUsage: undefined
|
|
1089
|
-
});
|
|
1090
|
-
const systemIdsToRemove = systemMessages.slice(1).reduce((ids, message) => {
|
|
1091
|
-
const nextIds = new Set(ids);
|
|
1092
|
-
nextIds.add(message.id);
|
|
1093
|
-
return nextIds;
|
|
1094
|
-
}, /* @__PURE__ */ new Set);
|
|
1095
|
-
const renumbered = ordered.filter((m) => !systemIdsToRemove.has(m.id)).map((m) => m.id === firstSystemMsg.id ? collapsed : m).map((message, index) => {
|
|
1096
|
-
if (message.position === index)
|
|
1097
|
-
return message;
|
|
1098
|
-
return createMessage({
|
|
1099
|
-
id: message.id,
|
|
1100
|
-
role: message.role,
|
|
1101
|
-
content: copyContent(message.content),
|
|
1102
|
-
position: index,
|
|
1103
|
-
createdAt: message.createdAt,
|
|
1104
|
-
metadata: { ...message.metadata },
|
|
1105
|
-
hidden: message.hidden,
|
|
1106
|
-
toolCall: message.toolCall,
|
|
1107
|
-
toolResult: message.toolResult,
|
|
1108
|
-
tokenUsage: message.tokenUsage
|
|
1109
|
-
});
|
|
1110
|
-
});
|
|
1111
|
-
return ensureConversationSafe(toReadonly({
|
|
1112
|
-
...conversation,
|
|
1113
|
-
ids: renumbered.map((message) => message.id),
|
|
1114
|
-
messages: toIdRecord(renumbered),
|
|
1115
|
-
updatedAt: now
|
|
1116
|
-
}));
|
|
1117
|
-
}
|
|
1118
|
-
function redactMessageAtPosition(conversation, position, placeholderOrOptions, environment) {
|
|
1119
|
-
let placeholder = "[REDACTED]";
|
|
1120
|
-
let options = {};
|
|
1121
|
-
let env = environment;
|
|
1122
|
-
if (typeof placeholderOrOptions === "string")
|
|
1123
|
-
placeholder = placeholderOrOptions;
|
|
1124
|
-
else if (placeholderOrOptions) {
|
|
1125
|
-
if (!environment && isConversationEnvironmentParameter(placeholderOrOptions))
|
|
1126
|
-
env = placeholderOrOptions;
|
|
1127
|
-
else if (isRedactMessageOptions(placeholderOrOptions)) {
|
|
1128
|
-
options = placeholderOrOptions;
|
|
1129
|
-
if (options.placeholder)
|
|
1130
|
-
placeholder = options.placeholder;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
const redactToolArguments = options.redactToolArguments ?? true;
|
|
1134
|
-
const redactToolResults = options.redactToolResults ?? true;
|
|
1135
|
-
const clearToolMetadata = options.clearToolMetadata ?? false;
|
|
1136
|
-
if (position < 0 || position >= conversation.ids.length)
|
|
1137
|
-
throw createInvalidPositionError(conversation.ids.length - 1, position);
|
|
1138
|
-
const id = conversation.ids[position];
|
|
1139
|
-
const original = id ? conversation.messages[id] : undefined;
|
|
1140
|
-
if (!original)
|
|
1141
|
-
throw createInvalidPositionError(conversation.ids.length - 1, position);
|
|
1142
|
-
let toolCall = original.toolCall ? { ...original.toolCall } : undefined;
|
|
1143
|
-
let toolResult = original.toolResult ? { ...original.toolResult } : undefined;
|
|
1144
|
-
if (clearToolMetadata) {
|
|
1145
|
-
toolCall = undefined;
|
|
1146
|
-
toolResult = undefined;
|
|
1147
|
-
} else {
|
|
1148
|
-
if (original.role === "tool-call" && toolCall)
|
|
1149
|
-
toolCall = {
|
|
1150
|
-
...toolCall,
|
|
1151
|
-
arguments: redactToolArguments ? placeholder : toolCall.arguments
|
|
1152
|
-
};
|
|
1153
|
-
if (original.role === "tool-result" && toolResult)
|
|
1154
|
-
toolResult = redactToolResults ? redactToolResult(toolResult, placeholder) : { ...toolResult };
|
|
1155
|
-
}
|
|
1156
|
-
const redacted = createMessage({
|
|
1157
|
-
id: original.id,
|
|
1158
|
-
role: original.role,
|
|
1159
|
-
content: placeholder,
|
|
1160
|
-
position: original.position,
|
|
1161
|
-
createdAt: original.createdAt,
|
|
1162
|
-
metadata: { ...original.metadata },
|
|
1163
|
-
hidden: original.hidden,
|
|
1164
|
-
toolCall,
|
|
1165
|
-
toolResult,
|
|
1166
|
-
tokenUsage: original.tokenUsage ? { ...original.tokenUsage } : undefined
|
|
1167
|
-
});
|
|
1168
|
-
const now = resolveConversationEnvironment(env).now();
|
|
1169
|
-
return ensureConversationSafe(toReadonly({
|
|
1170
|
-
...conversation,
|
|
1171
|
-
ids: [...conversation.ids],
|
|
1172
|
-
messages: {
|
|
1173
|
-
...conversation.messages,
|
|
1174
|
-
[redacted.id]: redacted
|
|
1175
|
-
},
|
|
1176
|
-
updatedAt: now
|
|
1177
|
-
}));
|
|
1178
|
-
}
|
|
1179
|
-
function normalizeLegacyToolCall(toolCall) {
|
|
1180
|
-
if (!toolCall || typeof toolCall !== "object")
|
|
1181
|
-
return toolCall;
|
|
1182
|
-
const record = { ...toolCall };
|
|
1183
|
-
if (!("arguments" in record) && "args" in record)
|
|
1184
|
-
record["arguments"] = record["args"];
|
|
1185
|
-
delete record["args"];
|
|
1186
|
-
return record;
|
|
1187
|
-
}
|
|
1188
|
-
function normalizeLegacyToolResult(toolResult) {
|
|
1189
|
-
if (!toolResult || typeof toolResult !== "object")
|
|
1190
|
-
return toolResult;
|
|
1191
|
-
const record = { ...toolResult };
|
|
1192
|
-
if (!("content" in record) && "result" in record)
|
|
1193
|
-
record["content"] = record["result"];
|
|
1194
|
-
delete record["result"];
|
|
1195
|
-
return record;
|
|
1196
|
-
}
|
|
1197
|
-
function normalizeLegacyConversationData(json) {
|
|
1198
|
-
if (!json || typeof json !== "object")
|
|
1199
|
-
return json;
|
|
1200
|
-
const conversation = { ...json };
|
|
1201
|
-
const messagesValue = conversation["messages"];
|
|
1202
|
-
if (!messagesValue || typeof messagesValue !== "object")
|
|
1203
|
-
return conversation;
|
|
1204
|
-
const normalizedMessages = {};
|
|
1205
|
-
for (const [messageId, messageValue] of Object.entries(messagesValue)) {
|
|
1206
|
-
if (!messageValue || typeof messageValue !== "object") {
|
|
1207
|
-
normalizedMessages[messageId] = messageValue;
|
|
1208
|
-
continue;
|
|
1209
|
-
}
|
|
1210
|
-
const message = { ...messageValue };
|
|
1211
|
-
if (message["role"] === "tool-use")
|
|
1212
|
-
message["role"] = "tool-call";
|
|
1213
|
-
if ("toolCall" in message)
|
|
1214
|
-
message["toolCall"] = normalizeLegacyToolCall(message["toolCall"]);
|
|
1215
|
-
if ("toolResult" in message)
|
|
1216
|
-
message["toolResult"] = normalizeLegacyToolResult(message["toolResult"]);
|
|
1217
|
-
normalizedMessages[messageId] = message;
|
|
1218
|
-
}
|
|
1219
|
-
conversation["messages"] = normalizedMessages;
|
|
1220
|
-
return conversation;
|
|
1221
|
-
}
|
|
1222
|
-
function normalizeToolResult(toolResult) {
|
|
1223
|
-
if (!toolResult)
|
|
1224
|
-
return;
|
|
1225
|
-
return {
|
|
1226
|
-
callId: toolResult.callId,
|
|
1227
|
-
outcome: toolResult.outcome,
|
|
1228
|
-
content: toolResult.content,
|
|
1229
|
-
...toolResult.error ? { error: { ...toolResult.error } } : {},
|
|
1230
|
-
...toolResult.action ? { action: { ...toolResult.action } } : {},
|
|
1231
|
-
...toolResult.inputDigest ? { inputDigest: toolResult.inputDigest } : {},
|
|
1232
|
-
...toolResult.outputDigest ? { outputDigest: toolResult.outputDigest } : {}
|
|
1233
|
-
};
|
|
1234
|
-
}
|
|
1235
|
-
function normalizeMessage(message) {
|
|
1236
|
-
const base = {
|
|
1237
|
-
id: message.id,
|
|
1238
|
-
role: message.role,
|
|
1239
|
-
content: message.content,
|
|
1240
|
-
position: message.position,
|
|
1241
|
-
createdAt: message.createdAt,
|
|
1242
|
-
metadata: message.metadata,
|
|
1243
|
-
hidden: message.hidden,
|
|
1244
|
-
toolCall: message.toolCall ? { ...message.toolCall } : undefined,
|
|
1245
|
-
toolResult: normalizeToolResult(message.toolResult),
|
|
1246
|
-
tokenUsage: message.tokenUsage ? { ...message.tokenUsage } : undefined
|
|
1247
|
-
};
|
|
1248
|
-
if (isAssistantMessage(message))
|
|
1249
|
-
return {
|
|
1250
|
-
...base,
|
|
1251
|
-
role: "assistant",
|
|
1252
|
-
goalCompleted: message.goalCompleted
|
|
1253
|
-
};
|
|
1254
|
-
return base;
|
|
1255
|
-
}
|
|
1256
|
-
function deserializeConversationHistory(json) {
|
|
1257
|
-
const parsed = conversationSchema.safeParse(normalizeLegacyConversationData(json));
|
|
1258
|
-
if (!parsed.success)
|
|
1259
|
-
throw createSerializationError("failed to deserialize conversation: invalid data");
|
|
1260
|
-
const data = parsed.data;
|
|
1261
|
-
try {
|
|
1262
|
-
const messageIds = new Set(Object.keys(data.messages));
|
|
1263
|
-
const orderedMessages = data.ids.map((id, index) => {
|
|
1264
|
-
const message = data.messages[id];
|
|
1265
|
-
if (!message)
|
|
1266
|
-
throw createSerializationError(`missing message for id ${id}`);
|
|
1267
|
-
if (message.position !== index)
|
|
1268
|
-
throw createInvalidPositionError(index, message.position);
|
|
1269
|
-
messageIds.delete(id);
|
|
1270
|
-
return normalizeMessage(message);
|
|
1271
|
-
});
|
|
1272
|
-
if (messageIds.size > 0)
|
|
1273
|
-
throw createSerializationError(`messages not listed in ids: ${[...messageIds].join(", ")}`);
|
|
1274
|
-
orderedMessages.reduce((state, message) => {
|
|
1275
|
-
if (message.role === "tool-call" && message.toolCall)
|
|
1276
|
-
return { toolUses: registerToolUse(state.toolUses, message.toolCall) };
|
|
1277
|
-
if (message.role === "tool-result" && message.toolResult)
|
|
1278
|
-
assertToolReference(state.toolUses, message.toolResult.callId);
|
|
1279
|
-
return state;
|
|
1280
|
-
}, { toolUses: /* @__PURE__ */ new Map });
|
|
1281
|
-
const messageInstances = orderedMessages.map((message) => createMessage(message));
|
|
1282
|
-
const readonly = toReadonly({
|
|
1283
|
-
schemaVersion: data.schemaVersion,
|
|
1284
|
-
id: data.id,
|
|
1285
|
-
title: data.title,
|
|
1286
|
-
status: data.status,
|
|
1287
|
-
metadata: { ...data.metadata },
|
|
1288
|
-
ids: orderedMessages.map((message) => message.id),
|
|
1289
|
-
messages: toIdRecord(messageInstances),
|
|
1290
|
-
createdAt: data.createdAt,
|
|
1291
|
-
updatedAt: data.updatedAt
|
|
1292
|
-
});
|
|
1293
|
-
assertConversationHistoryIntegrity(readonly);
|
|
1294
|
-
return readonly;
|
|
1295
|
-
} catch (error) {
|
|
1296
|
-
throw createSerializationError(`failed to deserialize conversation: ${error instanceof Error ? error.message : String(error)}`, error);
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
function toChatMessages(conversation) {
|
|
1300
|
-
assertConversationSafe(conversation);
|
|
1301
|
-
const roleMap = {
|
|
1302
|
-
user: "user",
|
|
1303
|
-
assistant: "assistant",
|
|
1304
|
-
system: "system",
|
|
1305
|
-
developer: "system",
|
|
1306
|
-
"tool-call": "assistant",
|
|
1307
|
-
"tool-result": "user",
|
|
1308
|
-
snapshot: "system"
|
|
1309
|
-
};
|
|
1310
|
-
const result = [];
|
|
1311
|
-
for (const message of getOrderedMessages(conversation)) {
|
|
1312
|
-
if (message.hidden)
|
|
1313
|
-
continue;
|
|
1314
|
-
const externalRole = roleMap[message.role];
|
|
1315
|
-
result.push({
|
|
1316
|
-
role: externalRole,
|
|
1317
|
-
content: message.content
|
|
1318
|
-
});
|
|
1319
|
-
}
|
|
1320
|
-
return result;
|
|
1321
|
-
}
|
|
1322
|
-
var isRedactMessageOptions = (value) => {
|
|
1323
|
-
if (!value || typeof value !== "object")
|
|
1324
|
-
return false;
|
|
1325
|
-
const candidate = value;
|
|
1326
|
-
return "placeholder" in candidate || "redactToolArguments" in candidate || "redactToolResults" in candidate || "clearToolMetadata" in candidate;
|
|
1327
|
-
};
|
|
1328
|
-
var init_conversation_7sbYchzO = __esm(() => {
|
|
1329
|
-
init_append_l_g0hmrj();
|
|
1330
|
-
init_message_BEjqY7Jv();
|
|
1331
|
-
init_transient_Bk0RaCFf();
|
|
1332
|
-
init_schemas();
|
|
1333
|
-
init_types_C6g2sHGB();
|
|
1334
|
-
init_tool_results_DKQs9ZGX();
|
|
1335
|
-
});
|
|
1336
|
-
|
|
1337
|
-
// ../../node_modules/conversationalist/dist/streaming.js
|
|
1338
|
-
function isStreamingMessage(message) {
|
|
1339
|
-
return message.metadata[STREAMING_KEY] === true;
|
|
1340
|
-
}
|
|
1341
|
-
function getStreamingMessage(conversation) {
|
|
1342
|
-
return getOrderedMessages(conversation).find(isStreamingMessage);
|
|
1343
|
-
}
|
|
1344
|
-
function appendStreamingMessage(conversation, role, metadata, environment) {
|
|
1345
|
-
const resolvedEnvironment = resolveConversationEnvironment(isConversationEnvironmentParameter(metadata) ? metadata : environment);
|
|
1346
|
-
const resolvedMetadata = isConversationEnvironmentParameter(metadata) ? undefined : metadata;
|
|
1347
|
-
const now = resolvedEnvironment.now();
|
|
1348
|
-
const messageId = resolvedEnvironment.randomId();
|
|
1349
|
-
const newMessage = createMessage({
|
|
1350
|
-
id: messageId,
|
|
1351
|
-
role,
|
|
1352
|
-
content: "",
|
|
1353
|
-
position: conversation.ids.length,
|
|
1354
|
-
createdAt: now,
|
|
1355
|
-
metadata: {
|
|
1356
|
-
...resolvedMetadata ?? {},
|
|
1357
|
-
[STREAMING_KEY]: true
|
|
1358
|
-
},
|
|
1359
|
-
hidden: false,
|
|
1360
|
-
toolCall: undefined,
|
|
1361
|
-
toolResult: undefined,
|
|
1362
|
-
tokenUsage: undefined
|
|
1363
|
-
});
|
|
1364
|
-
return {
|
|
1365
|
-
conversation: ensureConversationSafe(toReadonly({
|
|
1366
|
-
...conversation,
|
|
1367
|
-
ids: [...conversation.ids, messageId],
|
|
1368
|
-
messages: {
|
|
1369
|
-
...conversation.messages,
|
|
1370
|
-
[messageId]: newMessage
|
|
1371
|
-
},
|
|
1372
|
-
updatedAt: now
|
|
1373
|
-
})),
|
|
1374
|
-
messageId
|
|
1375
|
-
};
|
|
1376
|
-
}
|
|
1377
|
-
function updateStreamingMessage(conversation, messageId, content, environment) {
|
|
1378
|
-
const now = resolveConversationEnvironment(environment).now();
|
|
1379
|
-
const original = conversation.messages[messageId];
|
|
1380
|
-
if (!original)
|
|
1381
|
-
return ensureConversationSafe(conversation);
|
|
1382
|
-
const overrides = { content: typeof content === "string" ? content : [...content] };
|
|
1383
|
-
if (original.tokenUsage)
|
|
1384
|
-
overrides.tokenUsage = { ...original.tokenUsage };
|
|
1385
|
-
const updated = cloneMessage(original, overrides);
|
|
1386
|
-
return ensureConversationSafe(toReadonly({
|
|
1387
|
-
...conversation,
|
|
1388
|
-
ids: [...conversation.ids],
|
|
1389
|
-
messages: {
|
|
1390
|
-
...conversation.messages,
|
|
1391
|
-
[updated.id]: updated
|
|
1392
|
-
},
|
|
1393
|
-
updatedAt: now
|
|
1394
|
-
}));
|
|
1395
|
-
}
|
|
1396
|
-
function finalizeStreamingMessage(conversation, messageId, options, environment) {
|
|
1397
|
-
const resolvedEnvironment = resolveConversationEnvironment(isConversationEnvironmentParameter(options) ? options : environment);
|
|
1398
|
-
const resolvedOptions = isConversationEnvironmentParameter(options) ? undefined : options;
|
|
1399
|
-
const now = resolvedEnvironment.now();
|
|
1400
|
-
const original = conversation.messages[messageId];
|
|
1401
|
-
if (!original)
|
|
1402
|
-
return ensureConversationSafe(conversation);
|
|
1403
|
-
const { [STREAMING_KEY]: _, ...restMetadata } = original.metadata;
|
|
1404
|
-
const finalizeOverrides = { metadata: {
|
|
1405
|
-
...restMetadata,
|
|
1406
|
-
...resolvedOptions?.metadata ?? {}
|
|
1407
|
-
} };
|
|
1408
|
-
if (resolvedOptions?.tokenUsage)
|
|
1409
|
-
finalizeOverrides.tokenUsage = { ...resolvedOptions.tokenUsage };
|
|
1410
|
-
const updated = cloneMessage(original, finalizeOverrides);
|
|
1411
|
-
return ensureConversationSafe(toReadonly({
|
|
1412
|
-
...conversation,
|
|
1413
|
-
ids: [...conversation.ids],
|
|
1414
|
-
messages: {
|
|
1415
|
-
...conversation.messages,
|
|
1416
|
-
[updated.id]: updated
|
|
1417
|
-
},
|
|
1418
|
-
updatedAt: now
|
|
1419
|
-
}));
|
|
1420
|
-
}
|
|
1421
|
-
function cancelStreamingMessage(conversation, messageId, environment) {
|
|
1422
|
-
const now = resolveConversationEnvironment(environment).now();
|
|
1423
|
-
if (!conversation.messages[messageId])
|
|
1424
|
-
return ensureConversationSafe(conversation);
|
|
1425
|
-
const messages = getOrderedMessages(conversation).filter((m) => m.id !== messageId).map((message, index) => message.position === index ? message : (() => {
|
|
1426
|
-
const overrides = { position: index };
|
|
1427
|
-
if (message.tokenUsage)
|
|
1428
|
-
overrides.tokenUsage = { ...message.tokenUsage };
|
|
1429
|
-
return cloneMessage(message, overrides);
|
|
1430
|
-
})());
|
|
1431
|
-
return ensureConversationSafe(toReadonly({
|
|
1432
|
-
...conversation,
|
|
1433
|
-
ids: messages.map((message) => message.id),
|
|
1434
|
-
messages: toIdRecord(messages),
|
|
1435
|
-
updatedAt: now
|
|
1436
|
-
}));
|
|
1437
|
-
}
|
|
1438
|
-
var STREAMING_KEY = "__streaming", cloneMessage = (original, overrides = {}) => {
|
|
1439
|
-
const baseMessage = {
|
|
1440
|
-
id: original.id,
|
|
1441
|
-
role: original.role,
|
|
1442
|
-
content: overrides.content ?? (typeof original.content === "string" ? original.content : [...original.content]),
|
|
1443
|
-
position: overrides.position ?? original.position,
|
|
1444
|
-
createdAt: original.createdAt,
|
|
1445
|
-
metadata: overrides.metadata ?? { ...original.metadata },
|
|
1446
|
-
hidden: original.hidden,
|
|
1447
|
-
toolCall: original.toolCall ? { ...original.toolCall } : undefined,
|
|
1448
|
-
toolResult: original.toolResult ? { ...original.toolResult } : undefined,
|
|
1449
|
-
tokenUsage: overrides.tokenUsage
|
|
1450
|
-
};
|
|
1451
|
-
if (isAssistantMessage(original))
|
|
1452
|
-
return createMessage({
|
|
1453
|
-
...baseMessage,
|
|
1454
|
-
role: "assistant",
|
|
1455
|
-
goalCompleted: original.goalCompleted
|
|
1456
|
-
});
|
|
1457
|
-
return createMessage(baseMessage);
|
|
1458
|
-
};
|
|
1459
|
-
var init_streaming = __esm(() => {
|
|
1460
|
-
init_append_l_g0hmrj();
|
|
1461
|
-
init_message_BEjqY7Jv();
|
|
1462
|
-
init_transient_Bk0RaCFf();
|
|
1463
|
-
});
|
|
1464
|
-
|
|
1465
|
-
// ../../node_modules/conversationalist/dist/shared-BylU-V4n.js
|
|
1466
|
-
function toJSONValue(value) {
|
|
1467
|
-
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean")
|
|
1468
|
-
return value;
|
|
1469
|
-
if (Array.isArray(value))
|
|
1470
|
-
return value.map((item) => toJSONValue(item));
|
|
1471
|
-
if (value && typeof value === "object") {
|
|
1472
|
-
const record = {};
|
|
1473
|
-
for (const [key, entry] of Object.entries(value))
|
|
1474
|
-
record[key] = toJSONValue(entry);
|
|
1475
|
-
return record;
|
|
1476
|
-
}
|
|
1477
|
-
return String(value);
|
|
1478
|
-
}
|
|
1479
|
-
function parseJSONValue(value) {
|
|
1480
|
-
try {
|
|
1481
|
-
return JSON.parse(value);
|
|
1482
|
-
} catch {
|
|
1483
|
-
return;
|
|
1484
|
-
}
|
|
1485
|
-
}
|
|
1486
|
-
function isCanonicalToolResultPayload(value) {
|
|
1487
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
1488
|
-
return false;
|
|
1489
|
-
return "outcome" in value && (value["outcome"] === "success" || value["outcome"] === "error" || value["outcome"] === "action_required") && "content" in value;
|
|
1490
|
-
}
|
|
1491
|
-
var init_shared_BylU_V4n = () => {};
|
|
1492
|
-
|
|
1493
|
-
// ../../node_modules/conversationalist/dist/adapters/openai/index.js
|
|
1494
|
-
var exports_openai = {};
|
|
1495
|
-
__export(exports_openai, {
|
|
1496
|
-
toOpenAIMessagesGrouped: () => toOpenAIMessagesGrouped,
|
|
1497
|
-
toOpenAIMessages: () => toOpenAIMessages,
|
|
1498
|
-
openAIConversationAdapter: () => openAIConversationAdapter,
|
|
1499
|
-
fromOpenAIMessages: () => fromOpenAIMessages,
|
|
1500
|
-
appendOpenAIMessages: () => appendOpenAIMessages
|
|
1501
|
-
});
|
|
1502
|
-
function toOpenAIContent(content, options = {}) {
|
|
1503
|
-
if (typeof content === "string")
|
|
1504
|
-
return content;
|
|
1505
|
-
const allowImages = options.allowImages ?? true;
|
|
1506
|
-
const parts = [];
|
|
1507
|
-
for (const part of content)
|
|
1508
|
-
if (part.type === "text")
|
|
1509
|
-
parts.push({
|
|
1510
|
-
type: "text",
|
|
1511
|
-
text: part.text ?? ""
|
|
1512
|
-
});
|
|
1513
|
-
else if (part.type === "image" && allowImages)
|
|
1514
|
-
parts.push({
|
|
1515
|
-
type: "image_url",
|
|
1516
|
-
image_url: { url: part.url ?? "" }
|
|
1517
|
-
});
|
|
1518
|
-
if (parts.length === 0)
|
|
1519
|
-
return "";
|
|
1520
|
-
if (parts.length === 1 && parts[0]?.type === "text")
|
|
1521
|
-
return parts[0].text;
|
|
1522
|
-
return allowImages ? parts : parts;
|
|
1523
|
-
}
|
|
1524
|
-
function toOpenAITextContent(content) {
|
|
1525
|
-
return toOpenAIContent(content, { allowImages: false });
|
|
1526
|
-
}
|
|
1527
|
-
function toOpenAIToolCall(toolCall) {
|
|
1528
|
-
return {
|
|
1529
|
-
id: toolCall.id,
|
|
1530
|
-
type: "function",
|
|
1531
|
-
function: {
|
|
1532
|
-
name: toolCall.name,
|
|
1533
|
-
arguments: typeof toolCall.arguments === "string" ? toolCall.arguments : JSON.stringify(toolCall.arguments)
|
|
1534
|
-
}
|
|
1535
|
-
};
|
|
1536
|
-
}
|
|
1537
|
-
function convertMessage(message) {
|
|
1538
|
-
if (message.hidden)
|
|
1539
|
-
return null;
|
|
1540
|
-
if (isStreamingMessage(message))
|
|
1541
|
-
return null;
|
|
1542
|
-
switch (message.role) {
|
|
1543
|
-
case "system":
|
|
1544
|
-
case "developer":
|
|
1545
|
-
return {
|
|
1546
|
-
role: "system",
|
|
1547
|
-
content: toOpenAITextContent(message.content)
|
|
1548
|
-
};
|
|
1549
|
-
case "user":
|
|
1550
|
-
return {
|
|
1551
|
-
role: "user",
|
|
1552
|
-
content: toOpenAIContent(message.content)
|
|
1553
|
-
};
|
|
1554
|
-
case "assistant":
|
|
1555
|
-
return {
|
|
1556
|
-
role: "assistant",
|
|
1557
|
-
content: toOpenAITextContent(message.content)
|
|
1558
|
-
};
|
|
1559
|
-
case "tool-call":
|
|
1560
|
-
if (!message.toolCall)
|
|
1561
|
-
return null;
|
|
1562
|
-
return {
|
|
1563
|
-
role: "assistant",
|
|
1564
|
-
content: null,
|
|
1565
|
-
tool_calls: [toOpenAIToolCall(message.toolCall)]
|
|
1566
|
-
};
|
|
1567
|
-
case "tool-result":
|
|
1568
|
-
if (!message.toolResult)
|
|
1569
|
-
return null;
|
|
1570
|
-
return {
|
|
1571
|
-
role: "tool",
|
|
1572
|
-
content: stringifyToolResult(message.toolResult),
|
|
1573
|
-
tool_call_id: message.toolResult.callId
|
|
1574
|
-
};
|
|
1575
|
-
case "snapshot":
|
|
1576
|
-
return null;
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
function stringifyToolResult(result) {
|
|
1580
|
-
const payload = result.outcome === "success" ? result.content : {
|
|
1581
|
-
outcome: result.outcome,
|
|
1582
|
-
content: result.content,
|
|
1583
|
-
...result.error ? { error: result.error } : {},
|
|
1584
|
-
...result.action ? { action: result.action } : {}
|
|
1585
|
-
};
|
|
1586
|
-
if (typeof payload === "string")
|
|
1587
|
-
return payload;
|
|
1588
|
-
return JSON.stringify(payload);
|
|
1589
|
-
}
|
|
1590
|
-
function toConversationContent(content) {
|
|
1591
|
-
if (content === null)
|
|
1592
|
-
return;
|
|
1593
|
-
if (typeof content === "string")
|
|
1594
|
-
return content;
|
|
1595
|
-
const parts = content.map((part) => part.type === "text" ? {
|
|
1596
|
-
type: "text",
|
|
1597
|
-
text: part.text
|
|
1598
|
-
} : {
|
|
1599
|
-
type: "image",
|
|
1600
|
-
url: part.image_url.url
|
|
1601
|
-
});
|
|
1602
|
-
if (parts.length === 0)
|
|
1603
|
-
return "";
|
|
1604
|
-
if (parts.length === 1 && parts[0]?.type === "text")
|
|
1605
|
-
return parts[0].text ?? "";
|
|
1606
|
-
return parts;
|
|
1607
|
-
}
|
|
1608
|
-
function parseToolArguments(value) {
|
|
1609
|
-
return parseJSONValue(value) ?? value;
|
|
1610
|
-
}
|
|
1611
|
-
function parseToolResult(callId, content) {
|
|
1612
|
-
const serialized = typeof content === "string" ? content : content.map((part) => part.text).join(`
|
|
1613
|
-
|
|
1614
|
-
`);
|
|
1615
|
-
const parsed = parseJSONValue(serialized);
|
|
1616
|
-
if (parsed !== undefined && isCanonicalToolResultPayload(parsed))
|
|
1617
|
-
return {
|
|
1618
|
-
callId,
|
|
1619
|
-
outcome: parsed.outcome,
|
|
1620
|
-
content: parsed.content,
|
|
1621
|
-
...parsed.error ? { error: parsed.error } : {},
|
|
1622
|
-
...parsed.action ? { action: parsed.action } : {},
|
|
1623
|
-
...typeof parsed.inputDigest === "string" ? { inputDigest: parsed.inputDigest } : {},
|
|
1624
|
-
...typeof parsed.outputDigest === "string" ? { outputDigest: parsed.outputDigest } : {}
|
|
1625
|
-
};
|
|
1626
|
-
return {
|
|
1627
|
-
callId,
|
|
1628
|
-
outcome: "success",
|
|
1629
|
-
content: parsed ?? serialized
|
|
1630
|
-
};
|
|
1631
|
-
}
|
|
1632
|
-
function toMessageInputs(messages) {
|
|
1633
|
-
const inputs = [];
|
|
1634
|
-
for (const message of messages)
|
|
1635
|
-
switch (message.role) {
|
|
1636
|
-
case "system":
|
|
1637
|
-
inputs.push({
|
|
1638
|
-
role: "system",
|
|
1639
|
-
content: toConversationContent(message.content) ?? ""
|
|
1640
|
-
});
|
|
1641
|
-
break;
|
|
1642
|
-
case "user":
|
|
1643
|
-
inputs.push({
|
|
1644
|
-
role: "user",
|
|
1645
|
-
content: toConversationContent(message.content) ?? ""
|
|
1646
|
-
});
|
|
1647
|
-
break;
|
|
1648
|
-
case "assistant": {
|
|
1649
|
-
const assistantContent = toConversationContent(message.content);
|
|
1650
|
-
if (assistantContent !== undefined && (typeof assistantContent === "string" ? assistantContent.length > 0 : assistantContent.length > 0))
|
|
1651
|
-
inputs.push({
|
|
1652
|
-
role: "assistant",
|
|
1653
|
-
content: assistantContent
|
|
1654
|
-
});
|
|
1655
|
-
for (const toolCall of message.tool_calls ?? [])
|
|
1656
|
-
inputs.push({
|
|
1657
|
-
role: "tool-call",
|
|
1658
|
-
content: "",
|
|
1659
|
-
toolCall: {
|
|
1660
|
-
id: toolCall.id,
|
|
1661
|
-
name: toolCall.function.name,
|
|
1662
|
-
arguments: parseToolArguments(toolCall.function.arguments)
|
|
1663
|
-
}
|
|
1664
|
-
});
|
|
1665
|
-
break;
|
|
1666
|
-
}
|
|
1667
|
-
case "tool":
|
|
1668
|
-
inputs.push({
|
|
1669
|
-
role: "tool-result",
|
|
1670
|
-
content: "",
|
|
1671
|
-
toolResult: parseToolResult(message.tool_call_id, message.content)
|
|
1672
|
-
});
|
|
1673
|
-
break;
|
|
1674
|
-
}
|
|
1675
|
-
return inputs;
|
|
1676
|
-
}
|
|
1677
|
-
function toOpenAIMessages(conversation) {
|
|
1678
|
-
assertConversationSafe(conversation);
|
|
1679
|
-
const messages = [];
|
|
1680
|
-
for (const message of getOrderedMessages(conversation)) {
|
|
1681
|
-
const converted = convertMessage(message);
|
|
1682
|
-
if (converted)
|
|
1683
|
-
messages.push(converted);
|
|
1684
|
-
}
|
|
1685
|
-
return messages;
|
|
1686
|
-
}
|
|
1687
|
-
function toOpenAIMessagesGrouped(conversation) {
|
|
1688
|
-
assertConversationSafe(conversation);
|
|
1689
|
-
const messages = [];
|
|
1690
|
-
let pendingToolCalls = [];
|
|
1691
|
-
for (const message of getOrderedMessages(conversation)) {
|
|
1692
|
-
if (message.hidden)
|
|
1693
|
-
continue;
|
|
1694
|
-
if (isStreamingMessage(message))
|
|
1695
|
-
continue;
|
|
1696
|
-
if (message.role === "tool-call" && message.toolCall) {
|
|
1697
|
-
pendingToolCalls.push(toOpenAIToolCall(message.toolCall));
|
|
1698
|
-
continue;
|
|
1699
|
-
}
|
|
1700
|
-
if (pendingToolCalls.length > 0) {
|
|
1701
|
-
messages.push({
|
|
1702
|
-
role: "assistant",
|
|
1703
|
-
content: null,
|
|
1704
|
-
tool_calls: pendingToolCalls
|
|
1705
|
-
});
|
|
1706
|
-
pendingToolCalls = [];
|
|
1707
|
-
}
|
|
1708
|
-
const converted = convertMessage(message);
|
|
1709
|
-
if (converted && message.role !== "tool-call")
|
|
1710
|
-
messages.push(converted);
|
|
1711
|
-
}
|
|
1712
|
-
if (pendingToolCalls.length > 0)
|
|
1713
|
-
messages.push({
|
|
1714
|
-
role: "assistant",
|
|
1715
|
-
content: null,
|
|
1716
|
-
tool_calls: pendingToolCalls
|
|
1717
|
-
});
|
|
1718
|
-
return messages;
|
|
1719
|
-
}
|
|
1720
|
-
function fromOpenAIMessages(messages) {
|
|
1721
|
-
let conversation = createConversationHistory();
|
|
1722
|
-
const inputs = toMessageInputs(messages);
|
|
1723
|
-
if (inputs.length > 0)
|
|
1724
|
-
conversation = appendMessages(conversation, ...inputs);
|
|
1725
|
-
return conversation;
|
|
1726
|
-
}
|
|
1727
|
-
function appendOpenAIMessages(conversation, messages) {
|
|
1728
|
-
const inputs = toMessageInputs(messages);
|
|
1729
|
-
if (inputs.length === 0)
|
|
1730
|
-
return conversation;
|
|
1731
|
-
return appendMessages(conversation, ...inputs);
|
|
1732
|
-
}
|
|
1733
|
-
var openAIConversationAdapter;
|
|
1734
|
-
var init_openai = __esm(() => {
|
|
1735
|
-
init_append_l_g0hmrj();
|
|
1736
|
-
init_transient_Bk0RaCFf();
|
|
1737
|
-
init_conversation_7sbYchzO();
|
|
1738
|
-
init_streaming();
|
|
1739
|
-
init_shared_BylU_V4n();
|
|
1740
|
-
openAIConversationAdapter = {
|
|
1741
|
-
export(conversation, options = {}) {
|
|
1742
|
-
return options.groupToolCalls === false ? toOpenAIMessages(conversation) : toOpenAIMessagesGrouped(conversation);
|
|
1743
|
-
},
|
|
1744
|
-
import(messages) {
|
|
1745
|
-
return fromOpenAIMessages(messages);
|
|
1746
|
-
},
|
|
1747
|
-
append(conversation, messages) {
|
|
1748
|
-
return appendOpenAIMessages(conversation, messages);
|
|
1749
|
-
}
|
|
1750
|
-
};
|
|
1751
|
-
});
|
|
1752
|
-
|
|
1753
|
-
// ../../node_modules/conversationalist/dist/adapters/anthropic/index.js
|
|
1754
|
-
var exports_anthropic = {};
|
|
1755
|
-
__export(exports_anthropic, {
|
|
1756
|
-
toAnthropicMessages: () => toAnthropicMessages,
|
|
1757
|
-
fromAnthropicMessages: () => fromAnthropicMessages,
|
|
1758
|
-
appendAnthropicMessages: () => appendAnthropicMessages,
|
|
1759
|
-
anthropicConversationAdapter: () => anthropicConversationAdapter
|
|
1760
|
-
});
|
|
1761
|
-
function toAnthropicContent(content) {
|
|
1762
|
-
if (typeof content === "string")
|
|
1763
|
-
return content;
|
|
1764
|
-
const blocks = [];
|
|
1765
|
-
for (const part of content)
|
|
1766
|
-
switch (part.type) {
|
|
1767
|
-
case "text":
|
|
1768
|
-
blocks.push({
|
|
1769
|
-
type: "text",
|
|
1770
|
-
text: part.text ?? "",
|
|
1771
|
-
...part.citations !== undefined ? { citations: part.citations } : {}
|
|
1772
|
-
});
|
|
1773
|
-
break;
|
|
1774
|
-
case "thinking":
|
|
1775
|
-
blocks.push({
|
|
1776
|
-
type: "thinking",
|
|
1777
|
-
thinking: part.thinking,
|
|
1778
|
-
signature: part.signature
|
|
1779
|
-
});
|
|
1780
|
-
break;
|
|
1781
|
-
case "redacted_thinking":
|
|
1782
|
-
blocks.push({
|
|
1783
|
-
type: "redacted_thinking",
|
|
1784
|
-
data: part.data
|
|
1785
|
-
});
|
|
1786
|
-
break;
|
|
1787
|
-
case "server_tool_use":
|
|
1788
|
-
blocks.push({
|
|
1789
|
-
type: "server_tool_use",
|
|
1790
|
-
id: part.id,
|
|
1791
|
-
name: part.name,
|
|
1792
|
-
input: part.input
|
|
1793
|
-
});
|
|
1794
|
-
break;
|
|
1795
|
-
case "web_search_tool_result":
|
|
1796
|
-
blocks.push({
|
|
1797
|
-
type: "web_search_tool_result",
|
|
1798
|
-
tool_use_id: part.tool_use_id,
|
|
1799
|
-
content: part.content
|
|
1800
|
-
});
|
|
1801
|
-
break;
|
|
1802
|
-
case "code_execution_tool_result":
|
|
1803
|
-
case "bash_code_execution_tool_result":
|
|
1804
|
-
case "text_editor_code_execution_tool_result":
|
|
1805
|
-
case "web_fetch_tool_result":
|
|
1806
|
-
blocks.push({
|
|
1807
|
-
type: part.type,
|
|
1808
|
-
tool_use_id: part.tool_use_id,
|
|
1809
|
-
content: part.content
|
|
1810
|
-
});
|
|
1811
|
-
break;
|
|
1812
|
-
case "container_upload":
|
|
1813
|
-
blocks.push({
|
|
1814
|
-
type: "container_upload",
|
|
1815
|
-
file_id: part.file_id
|
|
1816
|
-
});
|
|
1817
|
-
break;
|
|
1818
|
-
case "image": {
|
|
1819
|
-
const url = part.url ?? "";
|
|
1820
|
-
const base64Match = url.startsWith("data:") ? url.match(/^data:([^;]+);base64,(.+)$/) : null;
|
|
1821
|
-
if (base64Match && base64Match[1] && base64Match[2])
|
|
1822
|
-
blocks.push({
|
|
1823
|
-
type: "image",
|
|
1824
|
-
source: {
|
|
1825
|
-
type: "base64",
|
|
1826
|
-
media_type: base64Match[1],
|
|
1827
|
-
data: base64Match[2]
|
|
1828
|
-
}
|
|
1829
|
-
});
|
|
1830
|
-
else
|
|
1831
|
-
blocks.push({
|
|
1832
|
-
type: "image",
|
|
1833
|
-
source: {
|
|
1834
|
-
type: "url",
|
|
1835
|
-
url
|
|
1836
|
-
}
|
|
1837
|
-
});
|
|
1838
|
-
break;
|
|
1839
|
-
}
|
|
1840
|
-
}
|
|
1841
|
-
const only = blocks.length === 1 ? blocks[0] : undefined;
|
|
1842
|
-
return only?.type === "text" && only.citations === undefined ? only.text : blocks;
|
|
1843
|
-
}
|
|
1844
|
-
function toToolUseBlock(toolCall) {
|
|
1845
|
-
let input = toolCall.arguments;
|
|
1846
|
-
if (typeof toolCall.arguments === "string")
|
|
1847
|
-
try {
|
|
1848
|
-
input = JSON.parse(toolCall.arguments);
|
|
1849
|
-
} catch {
|
|
1850
|
-
input = toolCall.arguments;
|
|
1851
|
-
}
|
|
1852
|
-
return {
|
|
1853
|
-
type: "tool_use",
|
|
1854
|
-
id: toolCall.id,
|
|
1855
|
-
name: toolCall.name,
|
|
1856
|
-
input
|
|
1857
|
-
};
|
|
1858
|
-
}
|
|
1859
|
-
function toToolResultBlock(toolResult) {
|
|
1860
|
-
const payload = toolResult.outcome === "success" ? toolResult.content : {
|
|
1861
|
-
outcome: toolResult.outcome,
|
|
1862
|
-
content: toolResult.content,
|
|
1863
|
-
...toolResult.error ? { error: toolResult.error } : {},
|
|
1864
|
-
...toolResult.action ? { action: toolResult.action } : {}
|
|
1865
|
-
};
|
|
1866
|
-
const result = {
|
|
1867
|
-
type: "tool_result",
|
|
1868
|
-
tool_use_id: toolResult.callId,
|
|
1869
|
-
content: typeof payload === "string" ? payload : JSON.stringify(payload)
|
|
1870
|
-
};
|
|
1871
|
-
if (toolResult.outcome !== "success")
|
|
1872
|
-
result.is_error = true;
|
|
1873
|
-
return result;
|
|
1874
|
-
}
|
|
1875
|
-
function extractSystemContent(messages) {
|
|
1876
|
-
const systemMessages = messages.filter((m) => (m.role === "system" || m.role === "developer") && !m.hidden && !isStreamingMessage(m));
|
|
1877
|
-
if (systemMessages.length === 0)
|
|
1878
|
-
return;
|
|
1879
|
-
const parts = [];
|
|
1880
|
-
for (const msg of systemMessages)
|
|
1881
|
-
if (typeof msg.content === "string")
|
|
1882
|
-
parts.push(msg.content);
|
|
1883
|
-
else
|
|
1884
|
-
for (const part of msg.content)
|
|
1885
|
-
if (part.type === "text")
|
|
1886
|
-
parts.push(part.text ?? "");
|
|
1887
|
-
return parts.join(`
|
|
1888
|
-
|
|
1889
|
-
`);
|
|
1890
|
-
}
|
|
1891
|
-
function toAnthropicMessages(conversation) {
|
|
1892
|
-
assertConversationSafe(conversation);
|
|
1893
|
-
const ordered = getOrderedMessages(conversation);
|
|
1894
|
-
const system = extractSystemContent(ordered);
|
|
1895
|
-
const messages = [];
|
|
1896
|
-
let currentRole = null;
|
|
1897
|
-
let currentBlocks = [];
|
|
1898
|
-
const flushCurrent = () => {
|
|
1899
|
-
if (currentRole && currentBlocks.length > 0) {
|
|
1900
|
-
const onlyBlock = currentBlocks.length === 1 ? currentBlocks[0] : undefined;
|
|
1901
|
-
const collapsible = onlyBlock?.type === "text" && onlyBlock.citations === undefined;
|
|
1902
|
-
messages.push({
|
|
1903
|
-
role: currentRole,
|
|
1904
|
-
content: collapsible && onlyBlock?.type === "text" ? onlyBlock.text : currentBlocks
|
|
1905
|
-
});
|
|
1906
|
-
currentBlocks = [];
|
|
1907
|
-
}
|
|
1908
|
-
currentRole = null;
|
|
1909
|
-
};
|
|
1910
|
-
for (const message of ordered) {
|
|
1911
|
-
if (message.hidden)
|
|
1912
|
-
continue;
|
|
1913
|
-
if (isStreamingMessage(message))
|
|
1914
|
-
continue;
|
|
1915
|
-
if (message.role === "system" || message.role === "developer")
|
|
1916
|
-
continue;
|
|
1917
|
-
if (message.role === "snapshot")
|
|
1918
|
-
continue;
|
|
1919
|
-
let targetRole;
|
|
1920
|
-
let blocks;
|
|
1921
|
-
if (message.role === "user") {
|
|
1922
|
-
targetRole = "user";
|
|
1923
|
-
const content = toAnthropicContent(message.content);
|
|
1924
|
-
if (typeof content === "string")
|
|
1925
|
-
blocks = [{
|
|
1926
|
-
type: "text",
|
|
1927
|
-
text: content
|
|
1928
|
-
}];
|
|
1929
|
-
else
|
|
1930
|
-
blocks = content;
|
|
1931
|
-
} else if (message.role === "assistant") {
|
|
1932
|
-
targetRole = "assistant";
|
|
1933
|
-
const content = toAnthropicContent(message.content);
|
|
1934
|
-
if (typeof content === "string")
|
|
1935
|
-
blocks = [{
|
|
1936
|
-
type: "text",
|
|
1937
|
-
text: content
|
|
1938
|
-
}];
|
|
1939
|
-
else
|
|
1940
|
-
blocks = content;
|
|
1941
|
-
} else if (message.role === "tool-call" && message.toolCall) {
|
|
1942
|
-
targetRole = "assistant";
|
|
1943
|
-
blocks = [toToolUseBlock(message.toolCall)];
|
|
1944
|
-
} else if (message.role === "tool-result" && message.toolResult) {
|
|
1945
|
-
targetRole = "user";
|
|
1946
|
-
blocks = [toToolResultBlock(message.toolResult)];
|
|
1947
|
-
} else
|
|
1948
|
-
continue;
|
|
1949
|
-
if (currentRole === targetRole)
|
|
1950
|
-
currentBlocks.push(...blocks);
|
|
1951
|
-
else {
|
|
1952
|
-
flushCurrent();
|
|
1953
|
-
currentRole = targetRole;
|
|
1954
|
-
currentBlocks = blocks;
|
|
1955
|
-
}
|
|
1956
|
-
}
|
|
1957
|
-
flushCurrent();
|
|
1958
|
-
const result = { messages };
|
|
1959
|
-
if (system !== undefined)
|
|
1960
|
-
result.system = system;
|
|
1961
|
-
return result;
|
|
1962
|
-
}
|
|
1963
|
-
function parseToolResultContent(callId, content, isError) {
|
|
1964
|
-
const parsed = parseJSONValue(content);
|
|
1965
|
-
if (parsed !== undefined && isCanonicalToolResultPayload(parsed))
|
|
1966
|
-
return {
|
|
1967
|
-
callId,
|
|
1968
|
-
outcome: parsed.outcome,
|
|
1969
|
-
content: parsed.content,
|
|
1970
|
-
...parsed.error ? { error: parsed.error } : {},
|
|
1971
|
-
...parsed.action ? { action: parsed.action } : {},
|
|
1972
|
-
...typeof parsed.inputDigest === "string" ? { inputDigest: parsed.inputDigest } : {},
|
|
1973
|
-
...typeof parsed.outputDigest === "string" ? { outputDigest: parsed.outputDigest } : {}
|
|
1974
|
-
};
|
|
1975
|
-
return {
|
|
1976
|
-
callId,
|
|
1977
|
-
outcome: isError ? "error" : "success",
|
|
1978
|
-
content: parsed ?? content
|
|
1979
|
-
};
|
|
1980
|
-
}
|
|
1981
|
-
function toMessageInputFromBlock(block) {
|
|
1982
|
-
if (block.type === "tool_use")
|
|
1983
|
-
return {
|
|
1984
|
-
role: "tool-call",
|
|
1985
|
-
content: "",
|
|
1986
|
-
toolCall: {
|
|
1987
|
-
id: block.id,
|
|
1988
|
-
name: block.name,
|
|
1989
|
-
arguments: toJSONValue(block.input)
|
|
1990
|
-
}
|
|
1991
|
-
};
|
|
1992
|
-
return {
|
|
1993
|
-
role: "tool-result",
|
|
1994
|
-
content: "",
|
|
1995
|
-
toolResult: parseToolResultContent(block.tool_use_id, block.content, block.is_error)
|
|
1996
|
-
};
|
|
1997
|
-
}
|
|
1998
|
-
function toGroupableContentPart(block) {
|
|
1999
|
-
switch (block.type) {
|
|
2000
|
-
case "text":
|
|
2001
|
-
return {
|
|
2002
|
-
type: "text",
|
|
2003
|
-
text: block.text,
|
|
2004
|
-
...block.citations !== undefined ? { citations: toJSONValue(block.citations) } : {}
|
|
2005
|
-
};
|
|
2006
|
-
case "thinking":
|
|
2007
|
-
return {
|
|
2008
|
-
type: "thinking",
|
|
2009
|
-
thinking: block.thinking,
|
|
2010
|
-
signature: block.signature
|
|
2011
|
-
};
|
|
2012
|
-
case "redacted_thinking":
|
|
2013
|
-
return {
|
|
2014
|
-
type: "redacted_thinking",
|
|
2015
|
-
data: block.data
|
|
2016
|
-
};
|
|
2017
|
-
case "server_tool_use":
|
|
2018
|
-
return {
|
|
2019
|
-
type: "server_tool_use",
|
|
2020
|
-
id: block.id,
|
|
2021
|
-
name: block.name,
|
|
2022
|
-
input: toJSONValue(block.input)
|
|
2023
|
-
};
|
|
2024
|
-
case "web_search_tool_result":
|
|
2025
|
-
return {
|
|
2026
|
-
type: "web_search_tool_result",
|
|
2027
|
-
tool_use_id: block.tool_use_id,
|
|
2028
|
-
content: toJSONValue(block.content)
|
|
2029
|
-
};
|
|
2030
|
-
case "code_execution_tool_result":
|
|
2031
|
-
case "bash_code_execution_tool_result":
|
|
2032
|
-
case "text_editor_code_execution_tool_result":
|
|
2033
|
-
case "web_fetch_tool_result":
|
|
2034
|
-
return {
|
|
2035
|
-
type: block.type,
|
|
2036
|
-
tool_use_id: block.tool_use_id,
|
|
2037
|
-
content: toJSONValue(block.content)
|
|
2038
|
-
};
|
|
2039
|
-
case "container_upload":
|
|
2040
|
-
return {
|
|
2041
|
-
type: "container_upload",
|
|
2042
|
-
file_id: block.file_id
|
|
2043
|
-
};
|
|
2044
|
-
case "image":
|
|
2045
|
-
return block.source.type === "url" ? {
|
|
2046
|
-
type: "image",
|
|
2047
|
-
url: block.source.url
|
|
2048
|
-
} : {
|
|
2049
|
-
type: "image",
|
|
2050
|
-
url: `data:${block.source.media_type};base64,${block.source.data}`,
|
|
2051
|
-
mimeType: block.source.media_type
|
|
2052
|
-
};
|
|
2053
|
-
default:
|
|
2054
|
-
return;
|
|
2055
|
-
}
|
|
2056
|
-
}
|
|
2057
|
-
function toMessageInputs2(payload) {
|
|
2058
|
-
const inputs = [];
|
|
2059
|
-
if (payload.system !== undefined)
|
|
2060
|
-
inputs.push({
|
|
2061
|
-
role: "system",
|
|
2062
|
-
content: payload.system
|
|
2063
|
-
});
|
|
2064
|
-
for (const message of payload.messages) {
|
|
2065
|
-
if (typeof message.content === "string") {
|
|
2066
|
-
inputs.push({
|
|
2067
|
-
role: message.role,
|
|
2068
|
-
content: message.content
|
|
2069
|
-
});
|
|
2070
|
-
continue;
|
|
2071
|
-
}
|
|
2072
|
-
let pendingParts = [];
|
|
2073
|
-
const flushPending = () => {
|
|
2074
|
-
if (pendingParts.length === 0)
|
|
2075
|
-
return;
|
|
2076
|
-
const first = pendingParts[0];
|
|
2077
|
-
if (pendingParts.length === 1 && first?.type === "text" && first.citations === undefined)
|
|
2078
|
-
inputs.push({
|
|
2079
|
-
role: message.role,
|
|
2080
|
-
content: first.text
|
|
2081
|
-
});
|
|
2082
|
-
else
|
|
2083
|
-
inputs.push({
|
|
2084
|
-
role: message.role,
|
|
2085
|
-
content: pendingParts
|
|
2086
|
-
});
|
|
2087
|
-
pendingParts = [];
|
|
2088
|
-
};
|
|
2089
|
-
for (const block of message.content) {
|
|
2090
|
-
const part = toGroupableContentPart(block);
|
|
2091
|
-
if (part !== undefined)
|
|
2092
|
-
pendingParts.push(part);
|
|
2093
|
-
else if (block.type === "tool_use" || block.type === "tool_result") {
|
|
2094
|
-
flushPending();
|
|
2095
|
-
inputs.push(toMessageInputFromBlock(block));
|
|
2096
|
-
}
|
|
2097
|
-
}
|
|
2098
|
-
flushPending();
|
|
2099
|
-
}
|
|
2100
|
-
return inputs;
|
|
2101
|
-
}
|
|
2102
|
-
function fromAnthropicMessages(payload) {
|
|
2103
|
-
let conversation = createConversationHistory();
|
|
2104
|
-
const inputs = toMessageInputs2(payload);
|
|
2105
|
-
if (inputs.length > 0)
|
|
2106
|
-
conversation = appendMessages(conversation, ...inputs);
|
|
2107
|
-
return conversation;
|
|
2108
|
-
}
|
|
2109
|
-
function appendAnthropicMessages(conversation, payload) {
|
|
2110
|
-
const inputs = toMessageInputs2(payload);
|
|
2111
|
-
if (inputs.length === 0)
|
|
2112
|
-
return conversation;
|
|
2113
|
-
return appendMessages(conversation, ...inputs);
|
|
2114
|
-
}
|
|
2115
|
-
var anthropicConversationAdapter;
|
|
2116
|
-
var init_anthropic = __esm(() => {
|
|
2117
|
-
init_append_l_g0hmrj();
|
|
2118
|
-
init_transient_Bk0RaCFf();
|
|
2119
|
-
init_conversation_7sbYchzO();
|
|
2120
|
-
init_streaming();
|
|
2121
|
-
init_shared_BylU_V4n();
|
|
2122
|
-
anthropicConversationAdapter = {
|
|
2123
|
-
export(conversation) {
|
|
2124
|
-
return toAnthropicMessages(conversation);
|
|
2125
|
-
},
|
|
2126
|
-
import(payload) {
|
|
2127
|
-
return fromAnthropicMessages(payload);
|
|
2128
|
-
},
|
|
2129
|
-
append(conversation, payload) {
|
|
2130
|
-
return appendAnthropicMessages(conversation, payload);
|
|
2131
|
-
}
|
|
2132
|
-
};
|
|
2133
|
-
});
|
|
2134
|
-
|
|
2135
|
-
// ../../node_modules/conversationalist/dist/adapters/gemini/index.js
|
|
2136
|
-
var exports_gemini = {};
|
|
2137
|
-
__export(exports_gemini, {
|
|
2138
|
-
toGeminiMessages: () => toGeminiMessages,
|
|
2139
|
-
geminiConversationAdapter: () => geminiConversationAdapter,
|
|
2140
|
-
fromGeminiMessages: () => fromGeminiMessages,
|
|
2141
|
-
appendGeminiMessages: () => appendGeminiMessages
|
|
2142
|
-
});
|
|
2143
|
-
function inferMimeType(url) {
|
|
2144
|
-
const trimmed = url.split("#")[0]?.split("?")[0] ?? "";
|
|
2145
|
-
const dotIndex = trimmed.lastIndexOf(".");
|
|
2146
|
-
if (dotIndex === -1)
|
|
2147
|
-
return;
|
|
2148
|
-
return MIME_TYPE_BY_EXTENSION[trimmed.slice(dotIndex + 1).toLowerCase()];
|
|
2149
|
-
}
|
|
2150
|
-
function resolveMimeType(url, explicit) {
|
|
2151
|
-
return explicit ?? inferMimeType(url) ?? DEFAULT_FILE_MIME_TYPE;
|
|
2152
|
-
}
|
|
2153
|
-
function normalizeGeminiResponse(toolResult) {
|
|
2154
|
-
if (toolResult.outcome === "success") {
|
|
2155
|
-
if (toolResult.content !== null && typeof toolResult.content === "object")
|
|
2156
|
-
return toolResult.content;
|
|
2157
|
-
return { result: toolResult.content };
|
|
2158
|
-
}
|
|
2159
|
-
return {
|
|
2160
|
-
outcome: toolResult.outcome,
|
|
2161
|
-
content: toolResult.content,
|
|
2162
|
-
...toolResult.error ? { error: toolResult.error } : {},
|
|
2163
|
-
...toolResult.action ? { action: toolResult.action } : {}
|
|
2164
|
-
};
|
|
2165
|
-
}
|
|
2166
|
-
function toGeminiParts(content) {
|
|
2167
|
-
if (typeof content === "string")
|
|
2168
|
-
return content ? [{ text: content }] : [];
|
|
2169
|
-
const parts = [];
|
|
2170
|
-
for (const part of content)
|
|
2171
|
-
if (part.type === "text") {
|
|
2172
|
-
if (part.text)
|
|
2173
|
-
parts.push({ text: part.text });
|
|
2174
|
-
} else if (part.type === "image") {
|
|
2175
|
-
const url = part.url ?? "";
|
|
2176
|
-
if (url.startsWith("data:")) {
|
|
2177
|
-
const matches = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
2178
|
-
if (matches)
|
|
2179
|
-
parts.push({ inlineData: {
|
|
2180
|
-
mimeType: matches[1],
|
|
2181
|
-
data: matches[2]
|
|
2182
|
-
} });
|
|
2183
|
-
} else {
|
|
2184
|
-
const fileData = {
|
|
2185
|
-
fileUri: url,
|
|
2186
|
-
mimeType: resolveMimeType(url, part.mimeType)
|
|
2187
|
-
};
|
|
2188
|
-
parts.push({ fileData });
|
|
2189
|
-
}
|
|
2190
|
-
}
|
|
2191
|
-
return parts;
|
|
2192
|
-
}
|
|
2193
|
-
function toFunctionCallPart(toolCall) {
|
|
2194
|
-
let args;
|
|
2195
|
-
if (typeof toolCall.arguments === "string")
|
|
2196
|
-
try {
|
|
2197
|
-
const parsed = JSON.parse(toolCall.arguments);
|
|
2198
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
2199
|
-
args = parsed;
|
|
2200
|
-
else
|
|
2201
|
-
args = { _value: parsed };
|
|
2202
|
-
} catch {
|
|
2203
|
-
args = { _raw: toolCall.arguments };
|
|
2204
|
-
}
|
|
2205
|
-
else if (toolCall.arguments && typeof toolCall.arguments === "object" && !Array.isArray(toolCall.arguments))
|
|
2206
|
-
args = toolCall.arguments;
|
|
2207
|
-
else
|
|
2208
|
-
args = { _value: toolCall.arguments };
|
|
2209
|
-
return { functionCall: {
|
|
2210
|
-
name: toolCall.name,
|
|
2211
|
-
args
|
|
2212
|
-
} };
|
|
2213
|
-
}
|
|
2214
|
-
function toFunctionResponsePart(toolResult, functionName) {
|
|
2215
|
-
return { functionResponse: {
|
|
2216
|
-
name: functionName,
|
|
2217
|
-
response: normalizeGeminiResponse(toolResult)
|
|
2218
|
-
} };
|
|
2219
|
-
}
|
|
2220
|
-
function extractSystemInstruction(messages) {
|
|
2221
|
-
const systemMessages = messages.filter((m) => (m.role === "system" || m.role === "developer") && !m.hidden && !isStreamingMessage(m));
|
|
2222
|
-
if (systemMessages.length === 0)
|
|
2223
|
-
return;
|
|
2224
|
-
const parts = [];
|
|
2225
|
-
for (const msg of systemMessages)
|
|
2226
|
-
parts.push(...toGeminiParts(msg.content));
|
|
2227
|
-
if (parts.length === 0)
|
|
2228
|
-
return;
|
|
2229
|
-
return {
|
|
2230
|
-
role: "user",
|
|
2231
|
-
parts
|
|
2232
|
-
};
|
|
2233
|
-
}
|
|
2234
|
-
function toGeminiMessages(conversation) {
|
|
2235
|
-
assertConversationSafe(conversation);
|
|
2236
|
-
const ordered = getOrderedMessages(conversation);
|
|
2237
|
-
const systemInstruction = extractSystemInstruction(ordered);
|
|
2238
|
-
const toolCallNames = /* @__PURE__ */ new Map;
|
|
2239
|
-
for (const message of ordered)
|
|
2240
|
-
if (message.role === "tool-call" && message.toolCall)
|
|
2241
|
-
toolCallNames.set(message.toolCall.id, message.toolCall.name);
|
|
2242
|
-
const contents = [];
|
|
2243
|
-
let currentRole = null;
|
|
2244
|
-
let currentParts = [];
|
|
2245
|
-
const flushCurrent = () => {
|
|
2246
|
-
if (currentRole && currentParts.length > 0) {
|
|
2247
|
-
contents.push({
|
|
2248
|
-
role: currentRole,
|
|
2249
|
-
parts: currentParts
|
|
2250
|
-
});
|
|
2251
|
-
currentParts = [];
|
|
2252
|
-
}
|
|
2253
|
-
currentRole = null;
|
|
2254
|
-
};
|
|
2255
|
-
for (const message of ordered) {
|
|
2256
|
-
if (message.hidden)
|
|
2257
|
-
continue;
|
|
2258
|
-
if (isStreamingMessage(message))
|
|
2259
|
-
continue;
|
|
2260
|
-
if (message.role === "system" || message.role === "developer")
|
|
2261
|
-
continue;
|
|
2262
|
-
if (message.role === "snapshot")
|
|
2263
|
-
continue;
|
|
2264
|
-
let targetRole;
|
|
2265
|
-
let parts;
|
|
2266
|
-
if (message.role === "user") {
|
|
2267
|
-
targetRole = "user";
|
|
2268
|
-
parts = toGeminiParts(message.content);
|
|
2269
|
-
} else if (message.role === "assistant") {
|
|
2270
|
-
targetRole = "model";
|
|
2271
|
-
parts = toGeminiParts(message.content);
|
|
2272
|
-
} else if (message.role === "tool-call" && message.toolCall) {
|
|
2273
|
-
targetRole = "model";
|
|
2274
|
-
parts = [toFunctionCallPart(message.toolCall)];
|
|
2275
|
-
} else if (message.role === "tool-result" && message.toolResult) {
|
|
2276
|
-
targetRole = "user";
|
|
2277
|
-
const functionName = toolCallNames.get(message.toolResult.callId) ?? "unknown";
|
|
2278
|
-
parts = [toFunctionResponsePart(message.toolResult, functionName)];
|
|
2279
|
-
} else
|
|
2280
|
-
continue;
|
|
2281
|
-
if (parts.length === 0)
|
|
2282
|
-
continue;
|
|
2283
|
-
if (currentRole === targetRole)
|
|
2284
|
-
currentParts.push(...parts);
|
|
2285
|
-
else {
|
|
2286
|
-
flushCurrent();
|
|
2287
|
-
currentRole = targetRole;
|
|
2288
|
-
currentParts = parts;
|
|
2289
|
-
}
|
|
2290
|
-
}
|
|
2291
|
-
flushCurrent();
|
|
2292
|
-
const result = { contents };
|
|
2293
|
-
if (systemInstruction !== undefined)
|
|
2294
|
-
result.systemInstruction = systemInstruction;
|
|
2295
|
-
return result;
|
|
2296
|
-
}
|
|
2297
|
-
function parseFunctionArguments(args) {
|
|
2298
|
-
if (Object.keys(args).length === 1 && Object.hasOwn(args, "_value"))
|
|
2299
|
-
return toJSONValue(args["_value"]);
|
|
2300
|
-
if (Object.keys(args).length === 1 && Object.hasOwn(args, "_raw") && typeof args["_raw"] === "string")
|
|
2301
|
-
return args["_raw"];
|
|
2302
|
-
return toJSONValue(args);
|
|
2303
|
-
}
|
|
2304
|
-
function parseFunctionResponse(callId, response) {
|
|
2305
|
-
const value = toJSONValue(response);
|
|
2306
|
-
if (isCanonicalToolResultPayload(value))
|
|
2307
|
-
return {
|
|
2308
|
-
callId,
|
|
2309
|
-
outcome: value.outcome,
|
|
2310
|
-
content: value.content,
|
|
2311
|
-
...value.error ? { error: value.error } : {},
|
|
2312
|
-
...value.action ? { action: value.action } : {},
|
|
2313
|
-
...typeof value.inputDigest === "string" ? { inputDigest: value.inputDigest } : {},
|
|
2314
|
-
...typeof value.outputDigest === "string" ? { outputDigest: value.outputDigest } : {}
|
|
2315
|
-
};
|
|
2316
|
-
if (value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 1 && Object.hasOwn(value, "result"))
|
|
2317
|
-
return {
|
|
2318
|
-
callId,
|
|
2319
|
-
outcome: "success",
|
|
2320
|
-
content: toJSONValue(value.result)
|
|
2321
|
-
};
|
|
2322
|
-
return {
|
|
2323
|
-
callId,
|
|
2324
|
-
outcome: "success",
|
|
2325
|
-
content: value
|
|
2326
|
-
};
|
|
2327
|
-
}
|
|
2328
|
-
function toContentFromGeminiPart(part) {
|
|
2329
|
-
if ("text" in part)
|
|
2330
|
-
return part.text;
|
|
2331
|
-
if ("inlineData" in part)
|
|
2332
|
-
return [{
|
|
2333
|
-
type: "image",
|
|
2334
|
-
url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`,
|
|
2335
|
-
mimeType: part.inlineData.mimeType
|
|
2336
|
-
}];
|
|
2337
|
-
return [{
|
|
2338
|
-
type: "image",
|
|
2339
|
-
url: part.fileData.fileUri,
|
|
2340
|
-
mimeType: part.fileData.mimeType
|
|
2341
|
-
}];
|
|
2342
|
-
}
|
|
2343
|
-
function toSystemInstructionContent(parts) {
|
|
2344
|
-
const contentParts = [];
|
|
2345
|
-
for (const part of parts)
|
|
2346
|
-
if ("text" in part)
|
|
2347
|
-
contentParts.push({
|
|
2348
|
-
type: "text",
|
|
2349
|
-
text: part.text
|
|
2350
|
-
});
|
|
2351
|
-
else if ("inlineData" in part)
|
|
2352
|
-
contentParts.push({
|
|
2353
|
-
type: "image",
|
|
2354
|
-
url: `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`,
|
|
2355
|
-
mimeType: part.inlineData.mimeType
|
|
2356
|
-
});
|
|
2357
|
-
else if ("fileData" in part)
|
|
2358
|
-
contentParts.push({
|
|
2359
|
-
type: "image",
|
|
2360
|
-
url: part.fileData.fileUri,
|
|
2361
|
-
mimeType: part.fileData.mimeType
|
|
2362
|
-
});
|
|
2363
|
-
if (contentParts.length === 0)
|
|
2364
|
-
return;
|
|
2365
|
-
if (contentParts.length === 1 && contentParts[0]?.type === "text")
|
|
2366
|
-
return contentParts[0].text ?? "";
|
|
2367
|
-
return contentParts;
|
|
2368
|
-
}
|
|
2369
|
-
function toMessageInputs3(payload) {
|
|
2370
|
-
const inputs = [];
|
|
2371
|
-
let syntheticToolCallCount = 0;
|
|
2372
|
-
const pendingToolCalls = /* @__PURE__ */ new Map;
|
|
2373
|
-
const queueToolCall = (name, callId) => {
|
|
2374
|
-
const queued = pendingToolCalls.get(name) ?? [];
|
|
2375
|
-
queued.push(callId);
|
|
2376
|
-
pendingToolCalls.set(name, queued);
|
|
2377
|
-
};
|
|
2378
|
-
const dequeueToolCall = (name) => {
|
|
2379
|
-
const queued = pendingToolCalls.get(name);
|
|
2380
|
-
if (!queued || queued.length === 0)
|
|
2381
|
-
return;
|
|
2382
|
-
const [callId, ...rest] = queued;
|
|
2383
|
-
if (rest.length === 0)
|
|
2384
|
-
pendingToolCalls.delete(name);
|
|
2385
|
-
else
|
|
2386
|
-
pendingToolCalls.set(name, rest);
|
|
2387
|
-
return callId;
|
|
2388
|
-
};
|
|
2389
|
-
const systemContent = payload.systemInstruction ? toSystemInstructionContent(payload.systemInstruction.parts) : undefined;
|
|
2390
|
-
if (systemContent !== undefined)
|
|
2391
|
-
inputs.push({
|
|
2392
|
-
role: "system",
|
|
2393
|
-
content: systemContent
|
|
2394
|
-
});
|
|
2395
|
-
for (const content of payload.contents) {
|
|
2396
|
-
const role = content.role === "model" ? "assistant" : "user";
|
|
2397
|
-
for (const part of content.parts) {
|
|
2398
|
-
if ("functionCall" in part) {
|
|
2399
|
-
const callId = `gemini-call-${++syntheticToolCallCount}`;
|
|
2400
|
-
queueToolCall(part.functionCall.name, callId);
|
|
2401
|
-
inputs.push({
|
|
2402
|
-
role: "tool-call",
|
|
2403
|
-
content: "",
|
|
2404
|
-
toolCall: {
|
|
2405
|
-
id: callId,
|
|
2406
|
-
name: part.functionCall.name,
|
|
2407
|
-
arguments: parseFunctionArguments(part.functionCall.args)
|
|
2408
|
-
}
|
|
2409
|
-
});
|
|
2410
|
-
continue;
|
|
2411
|
-
}
|
|
2412
|
-
if ("functionResponse" in part) {
|
|
2413
|
-
const callId = dequeueToolCall(part.functionResponse.name) ?? `gemini-call-${++syntheticToolCallCount}`;
|
|
2414
|
-
inputs.push({
|
|
2415
|
-
role: "tool-result",
|
|
2416
|
-
content: "",
|
|
2417
|
-
toolResult: parseFunctionResponse(callId, part.functionResponse.response)
|
|
2418
|
-
});
|
|
2419
|
-
continue;
|
|
2420
|
-
}
|
|
2421
|
-
inputs.push({
|
|
2422
|
-
role,
|
|
2423
|
-
content: toContentFromGeminiPart(part)
|
|
2424
|
-
});
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
|
-
return inputs;
|
|
2428
|
-
}
|
|
2429
|
-
function fromGeminiMessages(payload) {
|
|
2430
|
-
let conversation = createConversationHistory();
|
|
2431
|
-
const inputs = toMessageInputs3(payload);
|
|
2432
|
-
if (inputs.length > 0)
|
|
2433
|
-
conversation = appendMessages(conversation, ...inputs);
|
|
2434
|
-
return conversation;
|
|
2435
|
-
}
|
|
2436
|
-
function appendGeminiMessages(conversation, payload) {
|
|
2437
|
-
const inputs = toMessageInputs3(payload);
|
|
2438
|
-
if (inputs.length === 0)
|
|
2439
|
-
return conversation;
|
|
2440
|
-
return appendMessages(conversation, ...inputs);
|
|
2441
|
-
}
|
|
2442
|
-
var DEFAULT_FILE_MIME_TYPE = "application/octet-stream", MIME_TYPE_BY_EXTENSION, geminiConversationAdapter;
|
|
2443
|
-
var init_gemini = __esm(() => {
|
|
2444
|
-
init_append_l_g0hmrj();
|
|
2445
|
-
init_transient_Bk0RaCFf();
|
|
2446
|
-
init_conversation_7sbYchzO();
|
|
2447
|
-
init_streaming();
|
|
2448
|
-
init_shared_BylU_V4n();
|
|
2449
|
-
MIME_TYPE_BY_EXTENSION = {
|
|
2450
|
-
bmp: "image/bmp",
|
|
2451
|
-
gif: "image/gif",
|
|
2452
|
-
heic: "image/heic",
|
|
2453
|
-
heif: "image/heif",
|
|
2454
|
-
jpeg: "image/jpeg",
|
|
2455
|
-
jpg: "image/jpeg",
|
|
2456
|
-
png: "image/png",
|
|
2457
|
-
svg: "image/svg+xml",
|
|
2458
|
-
webp: "image/webp"
|
|
2459
|
-
};
|
|
2460
|
-
geminiConversationAdapter = {
|
|
2461
|
-
export(conversation) {
|
|
2462
|
-
return toGeminiMessages(conversation);
|
|
2463
|
-
},
|
|
2464
|
-
import(payload) {
|
|
2465
|
-
return fromGeminiMessages(payload);
|
|
2466
|
-
},
|
|
2467
|
-
append(conversation, payload) {
|
|
2468
|
-
return appendGeminiMessages(conversation, payload);
|
|
2469
|
-
}
|
|
2470
|
-
};
|
|
2471
|
-
});
|
|
2472
|
-
|
|
2473
|
-
// src/components/chat-conversation-list/index.ts
|
|
2474
|
-
import"@lostgradient/cinder/chat-conversation-list/styles";
|
|
2475
|
-
|
|
2476
|
-
// src/components/chat-conversation-list/chat-conversation-list.svelte
|
|
2477
|
-
import"svelte/internal/disclose-version";
|
|
2478
|
-
import * as $ from "svelte/internal/client";
|
|
2479
|
-
|
|
2480
|
-
// src/utilities/class-names.ts
|
|
2481
|
-
function classNames(...parts) {
|
|
2482
|
-
return parts.filter((part) => typeof part === "string" && part.length > 0).join(" ");
|
|
2483
|
-
}
|
|
2484
|
-
|
|
2485
|
-
// ../../node_modules/conversationalist/dist/index.js
|
|
2486
|
-
init_append_l_g0hmrj();
|
|
2487
|
-
init_transient_Bk0RaCFf();
|
|
2488
|
-
init_schemas();
|
|
2489
|
-
init_conversation_7sbYchzO();
|
|
2490
|
-
init_streaming();
|
|
2491
|
-
|
|
2492
|
-
// ../../node_modules/conversationalist/dist/history-5vdiTmMp.js
|
|
2493
|
-
init_append_l_g0hmrj();
|
|
2494
|
-
init_message_BEjqY7Jv();
|
|
2495
|
-
init_transient_Bk0RaCFf();
|
|
2496
|
-
init_types_C6g2sHGB();
|
|
2497
|
-
init_conversation_7sbYchzO();
|
|
2498
|
-
init_streaming();
|
|
2499
|
-
|
|
2500
|
-
// ../../node_modules/conversationalist/dist/context.js
|
|
2501
|
-
init_append_l_g0hmrj();
|
|
2502
|
-
init_message_BEjqY7Jv();
|
|
2503
|
-
init_transient_Bk0RaCFf();
|
|
2504
|
-
init_streaming();
|
|
2505
|
-
var cloneMessageWithPosition = (message, position, content) => {
|
|
2506
|
-
const baseMessage = {
|
|
2507
|
-
id: message.id,
|
|
2508
|
-
role: message.role,
|
|
2509
|
-
content,
|
|
2510
|
-
position,
|
|
2511
|
-
createdAt: message.createdAt,
|
|
2512
|
-
metadata: { ...message.metadata },
|
|
2513
|
-
hidden: message.hidden,
|
|
2514
|
-
toolCall: message.toolCall ? { ...message.toolCall } : undefined,
|
|
2515
|
-
toolResult: message.toolResult ? { ...message.toolResult } : undefined,
|
|
2516
|
-
tokenUsage: message.tokenUsage ? { ...message.tokenUsage } : undefined
|
|
2517
|
-
};
|
|
2518
|
-
if (isAssistantMessage(message))
|
|
2519
|
-
return createMessage({
|
|
2520
|
-
...baseMessage,
|
|
2521
|
-
role: "assistant",
|
|
2522
|
-
goalCompleted: message.goalCompleted
|
|
2523
|
-
});
|
|
2524
|
-
return createMessage(baseMessage);
|
|
2525
|
-
};
|
|
2526
|
-
var isPromiseLike = (value) => typeof value?.then === "function";
|
|
2527
|
-
var hasConversationTokenEstimator = (value) => Boolean(value && typeof value === "object" && typeof value["estimateConversationTokens"] === "function");
|
|
2528
|
-
var createMessageBlock = (message) => ({
|
|
2529
|
-
messages: [message],
|
|
2530
|
-
minPosition: message.position,
|
|
2531
|
-
maxPosition: message.position,
|
|
2532
|
-
tokenCount: 0
|
|
2533
|
-
});
|
|
2534
|
-
var estimateMessageBlockTokens = (messages, options, environment) => {
|
|
2535
|
-
if (options.estimateConversationTokens)
|
|
2536
|
-
return options.estimateConversationTokens(messages);
|
|
2537
|
-
const estimator = options.estimateTokens ?? environment.estimateTokens;
|
|
2538
|
-
return messages.reduce((total, message) => total + estimator(message), 0);
|
|
2539
|
-
};
|
|
2540
|
-
var buildMessageBlocks = (messages, estimateBlockTokens, preserveToolPairs) => {
|
|
2541
|
-
if (!preserveToolPairs) {
|
|
2542
|
-
const blocks2 = messages.map((message) => createMessageBlock(message));
|
|
2543
|
-
for (const block of blocks2)
|
|
2544
|
-
block.tokenCount = estimateBlockTokens(block.messages);
|
|
2545
|
-
const messageToBlock2 = /* @__PURE__ */ new Map;
|
|
2546
|
-
for (const block of blocks2) {
|
|
2547
|
-
const message = block.messages[0];
|
|
2548
|
-
if (message)
|
|
2549
|
-
messageToBlock2.set(message.id, block);
|
|
2550
|
-
}
|
|
2551
|
-
return {
|
|
2552
|
-
blocks: blocks2,
|
|
2553
|
-
messageToBlock: messageToBlock2
|
|
2554
|
-
};
|
|
2555
|
-
}
|
|
2556
|
-
const blocks = [];
|
|
2557
|
-
const toolUses = /* @__PURE__ */ new Map;
|
|
2558
|
-
for (const message of messages) {
|
|
2559
|
-
if (message.role === "tool-call" && message.toolCall) {
|
|
2560
|
-
const block = createMessageBlock(message);
|
|
2561
|
-
toolUses.set(message.toolCall.id, block);
|
|
2562
|
-
blocks.push(block);
|
|
2563
|
-
continue;
|
|
2564
|
-
}
|
|
2565
|
-
if (message.role === "tool-result" && message.toolResult) {
|
|
2566
|
-
const existing = toolUses.get(message.toolResult.callId);
|
|
2567
|
-
if (existing) {
|
|
2568
|
-
existing.messages.push(message);
|
|
2569
|
-
existing.maxPosition = Math.max(existing.maxPosition, message.position);
|
|
2570
|
-
continue;
|
|
2571
|
-
}
|
|
2572
|
-
const orphanBlock = createMessageBlock(message);
|
|
2573
|
-
orphanBlock.orphanToolResult = true;
|
|
2574
|
-
blocks.push(orphanBlock);
|
|
2575
|
-
continue;
|
|
2576
|
-
}
|
|
2577
|
-
blocks.push(createMessageBlock(message));
|
|
2578
|
-
}
|
|
2579
|
-
const filteredBlocks = blocks.filter((block) => !block.orphanToolResult);
|
|
2580
|
-
for (const block of filteredBlocks)
|
|
2581
|
-
block.tokenCount = estimateBlockTokens(block.messages);
|
|
2582
|
-
const messageToBlock = /* @__PURE__ */ new Map;
|
|
2583
|
-
for (const block of filteredBlocks)
|
|
2584
|
-
for (const message of block.messages)
|
|
2585
|
-
messageToBlock.set(message.id, block);
|
|
2586
|
-
return {
|
|
2587
|
-
blocks: filteredBlocks,
|
|
2588
|
-
messageToBlock
|
|
2589
|
-
};
|
|
2590
|
-
};
|
|
2591
|
-
var buildMessageBlocksAsync = async (messages, estimateBlockTokens, preserveToolPairs) => {
|
|
2592
|
-
const { blocks, messageToBlock } = buildMessageBlocks(messages, () => 0, preserveToolPairs);
|
|
2593
|
-
await Promise.all(blocks.map(async (block) => {
|
|
2594
|
-
block.tokenCount = await estimateBlockTokens(block.messages);
|
|
2595
|
-
}));
|
|
2596
|
-
return {
|
|
2597
|
-
blocks,
|
|
2598
|
-
messageToBlock
|
|
2599
|
-
};
|
|
2600
|
-
};
|
|
2601
|
-
var estimateBlocksAsConversation = (blocks, options, environment) => estimateMessageBlockTokens(collectMessagesFromBlocks(blocks), options, environment);
|
|
2602
|
-
var collectBlocksForMessages = (messages, messageToBlock) => {
|
|
2603
|
-
const blocks = [];
|
|
2604
|
-
const seen = /* @__PURE__ */ new Set;
|
|
2605
|
-
for (const message of messages) {
|
|
2606
|
-
const block = messageToBlock.get(message.id);
|
|
2607
|
-
if (block && !seen.has(block)) {
|
|
2608
|
-
seen.add(block);
|
|
2609
|
-
blocks.push(block);
|
|
2610
|
-
}
|
|
2611
|
-
}
|
|
2612
|
-
return blocks;
|
|
2613
|
-
};
|
|
2614
|
-
var collectMessagesFromBlocks = (blocks) => {
|
|
2615
|
-
const messages = [];
|
|
2616
|
-
const seen = /* @__PURE__ */ new Set;
|
|
2617
|
-
for (const block of blocks)
|
|
2618
|
-
for (const message of block.messages)
|
|
2619
|
-
if (!seen.has(message.id)) {
|
|
2620
|
-
seen.add(message.id);
|
|
2621
|
-
messages.push(message);
|
|
2622
|
-
}
|
|
2623
|
-
messages.sort((a, b) => a.position - b.position);
|
|
2624
|
-
return messages;
|
|
2625
|
-
};
|
|
2626
|
-
var ensureTruncationSafe = (conversation, preserveToolPairs, operation) => {
|
|
2627
|
-
try {
|
|
2628
|
-
return ensureConversationSafe(conversation);
|
|
2629
|
-
} catch (error) {
|
|
2630
|
-
if (!preserveToolPairs && error instanceof ConversationalistError && error.code === "error:integrity")
|
|
2631
|
-
throw createIntegrityError(`${operation} produced invalid tool linkage; use preserveToolPairs: true to keep tool interactions intact`, {
|
|
2632
|
-
preserveToolPairs,
|
|
2633
|
-
issues: error.context?.["issues"]
|
|
2634
|
-
});
|
|
2635
|
-
throw error;
|
|
2636
|
-
}
|
|
2637
|
-
};
|
|
2638
|
-
function estimateConversationTokens(conversation, optionsOrEstimator, environment) {
|
|
2639
|
-
let options = {};
|
|
2640
|
-
let env = environment;
|
|
2641
|
-
if (typeof optionsOrEstimator === "function")
|
|
2642
|
-
options = { estimateTokens: optionsOrEstimator };
|
|
2643
|
-
else if (optionsOrEstimator)
|
|
2644
|
-
if (!environment && !hasConversationTokenEstimator(optionsOrEstimator) && isConversationEnvironmentParameter(optionsOrEstimator))
|
|
2645
|
-
env = optionsOrEstimator;
|
|
2646
|
-
else
|
|
2647
|
-
options = optionsOrEstimator;
|
|
2648
|
-
const resolvedEnvironment = resolveConversationEnvironment(env);
|
|
2649
|
-
return estimateMessageBlockTokens(getOrderedMessages(conversation), options, resolvedEnvironment);
|
|
2650
|
-
}
|
|
2651
|
-
var truncateToTokenLimitFromBlocks = (conversation, maxTokens, currentTokens, orderedMessages, blocks, messageToBlock, options, environment) => {
|
|
2652
|
-
if (currentTokens <= maxTokens)
|
|
2653
|
-
return conversation;
|
|
2654
|
-
const now = environment.now();
|
|
2655
|
-
const preserveSystem = options.preserveSystemMessages ?? true;
|
|
2656
|
-
const preserveLastN = options.preserveLastN ?? 0;
|
|
2657
|
-
const preserveToolPairs = options.preserveToolPairs ?? true;
|
|
2658
|
-
const systemMessages = preserveSystem ? orderedMessages.filter((m) => m.role === "system") : [];
|
|
2659
|
-
const nonSystemMessages = orderedMessages.filter((m) => m.role !== "system");
|
|
2660
|
-
const protectedMessages = preserveLastN > 0 ? nonSystemMessages.slice(-preserveLastN) : [];
|
|
2661
|
-
const streamingMessages = orderedMessages.filter(isStreamingMessage);
|
|
2662
|
-
const systemBlocks = collectBlocksForMessages(systemMessages, messageToBlock);
|
|
2663
|
-
const protectedBlocks = collectBlocksForMessages(protectedMessages, messageToBlock);
|
|
2664
|
-
const streamingBlocks = collectBlocksForMessages(streamingMessages, messageToBlock);
|
|
2665
|
-
const lockedBlocks = new Set([
|
|
2666
|
-
...systemBlocks,
|
|
2667
|
-
...protectedBlocks,
|
|
2668
|
-
...streamingBlocks
|
|
2669
|
-
]);
|
|
2670
|
-
const removableBlocks = blocks.filter((block) => !lockedBlocks.has(block));
|
|
2671
|
-
const systemTokens = systemBlocks.reduce((sum, block) => sum + block.tokenCount, 0);
|
|
2672
|
-
const protectedTokens = protectedBlocks.reduce((sum, block) => sum + block.tokenCount, 0);
|
|
2673
|
-
const streamingTokens = streamingBlocks.reduce((sum, block) => sum + block.tokenCount, 0);
|
|
2674
|
-
const availableTokens = maxTokens - systemTokens - protectedTokens - streamingTokens;
|
|
2675
|
-
let selectedBlocks;
|
|
2676
|
-
if (availableTokens <= 0)
|
|
2677
|
-
selectedBlocks = [
|
|
2678
|
-
...systemBlocks,
|
|
2679
|
-
...protectedBlocks,
|
|
2680
|
-
...streamingBlocks
|
|
2681
|
-
];
|
|
2682
|
-
else {
|
|
2683
|
-
const sortedRemovable = [...removableBlocks].sort((a, b) => a.maxPosition - b.maxPosition);
|
|
2684
|
-
const keptRemovable = [];
|
|
2685
|
-
let usedTokens = 0;
|
|
2686
|
-
for (let i = sortedRemovable.length - 1;i >= 0; i--) {
|
|
2687
|
-
const block = sortedRemovable[i];
|
|
2688
|
-
if (usedTokens + block.tokenCount <= availableTokens) {
|
|
2689
|
-
keptRemovable.unshift(block);
|
|
2690
|
-
usedTokens += block.tokenCount;
|
|
2691
|
-
} else
|
|
2692
|
-
break;
|
|
2693
|
-
}
|
|
2694
|
-
selectedBlocks = [
|
|
2695
|
-
...systemBlocks,
|
|
2696
|
-
...keptRemovable,
|
|
2697
|
-
...streamingBlocks,
|
|
2698
|
-
...protectedBlocks
|
|
2699
|
-
];
|
|
2700
|
-
}
|
|
2701
|
-
const renumbered = collectMessagesFromBlocks(selectedBlocks).map((message, index) => cloneMessageWithPosition(message, index, copyContent(message.content)));
|
|
2702
|
-
return ensureTruncationSafe(toReadonly({
|
|
2703
|
-
...conversation,
|
|
2704
|
-
ids: renumbered.map((message) => message.id),
|
|
2705
|
-
messages: toIdRecord(renumbered),
|
|
2706
|
-
updatedAt: now
|
|
2707
|
-
}), preserveToolPairs, "truncateToTokenLimit");
|
|
2708
|
-
};
|
|
2709
|
-
var truncateToTokenLimitWithConversationEstimator = (conversation, maxTokens, orderedMessages, options, environment) => {
|
|
2710
|
-
const preserveSystem = options.preserveSystemMessages ?? true;
|
|
2711
|
-
const preserveLastN = options.preserveLastN ?? 0;
|
|
2712
|
-
const preserveToolPairs = options.preserveToolPairs ?? true;
|
|
2713
|
-
const { blocks, messageToBlock } = buildMessageBlocks(orderedMessages, () => 0, preserveToolPairs);
|
|
2714
|
-
const countAllMessages = estimateMessageBlockTokens(orderedMessages, options, environment);
|
|
2715
|
-
const selectBlocks = (currentTokens) => {
|
|
2716
|
-
if (currentTokens <= maxTokens)
|
|
2717
|
-
return blocks;
|
|
2718
|
-
const systemMessages = preserveSystem ? orderedMessages.filter((m) => m.role === "system") : [];
|
|
2719
|
-
const nonSystemMessages = orderedMessages.filter((m) => m.role !== "system");
|
|
2720
|
-
const protectedMessages = preserveLastN > 0 ? nonSystemMessages.slice(-preserveLastN) : [];
|
|
2721
|
-
const streamingMessages = orderedMessages.filter(isStreamingMessage);
|
|
2722
|
-
const systemBlocks = collectBlocksForMessages(systemMessages, messageToBlock);
|
|
2723
|
-
const protectedBlocks = collectBlocksForMessages(protectedMessages, messageToBlock);
|
|
2724
|
-
const streamingBlocks = collectBlocksForMessages(streamingMessages, messageToBlock);
|
|
2725
|
-
const lockedBlocks = new Set([
|
|
2726
|
-
...systemBlocks,
|
|
2727
|
-
...protectedBlocks,
|
|
2728
|
-
...streamingBlocks
|
|
2729
|
-
]);
|
|
2730
|
-
const removableBlocks = blocks.filter((block) => !lockedBlocks.has(block));
|
|
2731
|
-
const selectedLockedBlocks = [...lockedBlocks];
|
|
2732
|
-
const sortedRemovable = [...removableBlocks].sort((a, b) => a.maxPosition - b.maxPosition);
|
|
2733
|
-
const lockedTokens = estimateBlocksAsConversation(selectedLockedBlocks, options, environment);
|
|
2734
|
-
const selectFromNewest = (lockedTokenCount) => {
|
|
2735
|
-
if (lockedTokenCount >= maxTokens)
|
|
2736
|
-
return selectedLockedBlocks;
|
|
2737
|
-
const keptRemovable = [];
|
|
2738
|
-
for (let index = sortedRemovable.length - 1;index >= 0; index--) {
|
|
2739
|
-
const candidate = sortedRemovable[index];
|
|
2740
|
-
const candidateTokens = estimateBlocksAsConversation([
|
|
2741
|
-
...selectedLockedBlocks,
|
|
2742
|
-
candidate,
|
|
2743
|
-
...keptRemovable
|
|
2744
|
-
], options, environment);
|
|
2745
|
-
if (isPromiseLike(candidateTokens))
|
|
2746
|
-
return candidateTokens.then(async (resolvedCandidateTokens) => {
|
|
2747
|
-
if (resolvedCandidateTokens > maxTokens)
|
|
2748
|
-
return [...selectedLockedBlocks, ...keptRemovable];
|
|
2749
|
-
keptRemovable.unshift(candidate);
|
|
2750
|
-
for (let asyncIndex = index - 1;asyncIndex >= 0; asyncIndex--) {
|
|
2751
|
-
const asyncCandidate = sortedRemovable[asyncIndex];
|
|
2752
|
-
if (await estimateBlocksAsConversation([
|
|
2753
|
-
...selectedLockedBlocks,
|
|
2754
|
-
asyncCandidate,
|
|
2755
|
-
...keptRemovable
|
|
2756
|
-
], options, environment) > maxTokens)
|
|
2757
|
-
break;
|
|
2758
|
-
keptRemovable.unshift(asyncCandidate);
|
|
2759
|
-
}
|
|
2760
|
-
return [...selectedLockedBlocks, ...keptRemovable];
|
|
2761
|
-
});
|
|
2762
|
-
if (candidateTokens > maxTokens)
|
|
2763
|
-
break;
|
|
2764
|
-
keptRemovable.unshift(candidate);
|
|
2765
|
-
}
|
|
2766
|
-
return [...selectedLockedBlocks, ...keptRemovable];
|
|
2767
|
-
};
|
|
2768
|
-
if (isPromiseLike(lockedTokens))
|
|
2769
|
-
return lockedTokens.then(selectFromNewest);
|
|
2770
|
-
return selectFromNewest(lockedTokens);
|
|
2771
|
-
};
|
|
2772
|
-
const finish = (selectedBlocks2) => {
|
|
2773
|
-
if (selectedBlocks2 === blocks)
|
|
2774
|
-
return conversation;
|
|
2775
|
-
const renumbered = collectMessagesFromBlocks(selectedBlocks2).map((message, index) => cloneMessageWithPosition(message, index, copyContent(message.content)));
|
|
2776
|
-
return ensureTruncationSafe(toReadonly({
|
|
2777
|
-
...conversation,
|
|
2778
|
-
ids: renumbered.map((message) => message.id),
|
|
2779
|
-
messages: toIdRecord(renumbered),
|
|
2780
|
-
updatedAt: environment.now()
|
|
2781
|
-
}), preserveToolPairs, "truncateToTokenLimit");
|
|
2782
|
-
};
|
|
2783
|
-
const finishAsync = async (currentTokens) => {
|
|
2784
|
-
return finish(await selectBlocks(await currentTokens));
|
|
2785
|
-
};
|
|
2786
|
-
if (isPromiseLike(countAllMessages))
|
|
2787
|
-
return finishAsync(countAllMessages);
|
|
2788
|
-
const selectedBlocks = selectBlocks(countAllMessages);
|
|
2789
|
-
return isPromiseLike(selectedBlocks) ? selectedBlocks.then(finish) : finish(selectedBlocks);
|
|
2790
|
-
};
|
|
2791
|
-
function truncateToTokenLimit(conversation, maxTokens, optionsOrEstimator, environment) {
|
|
2792
|
-
assertConversationSafe(conversation);
|
|
2793
|
-
let options = {};
|
|
2794
|
-
let env = environment;
|
|
2795
|
-
if (typeof optionsOrEstimator === "function")
|
|
2796
|
-
options = { estimateTokens: optionsOrEstimator };
|
|
2797
|
-
else if (optionsOrEstimator)
|
|
2798
|
-
if (!environment && isConversationEnvironmentParameter(optionsOrEstimator)) {
|
|
2799
|
-
const candidate = optionsOrEstimator;
|
|
2800
|
-
if (!!(candidate["now"] || candidate["randomId"] || Array.isArray(candidate["plugins"]) && candidate["plugins"].length > 0))
|
|
2801
|
-
env = optionsOrEstimator;
|
|
2802
|
-
else
|
|
2803
|
-
options = optionsOrEstimator;
|
|
2804
|
-
} else
|
|
2805
|
-
options = optionsOrEstimator;
|
|
2806
|
-
const resolvedEnvironment = resolveConversationEnvironment(env);
|
|
2807
|
-
const preserveToolPairs = options.preserveToolPairs ?? true;
|
|
2808
|
-
const orderedMessages = getOrderedMessages(conversation);
|
|
2809
|
-
if (options.estimateConversationTokens)
|
|
2810
|
-
return truncateToTokenLimitWithConversationEstimator(conversation, maxTokens, orderedMessages, options, resolvedEnvironment);
|
|
2811
|
-
const estimateBlockTokens = (messages) => estimateMessageBlockTokens(messages, options, resolvedEnvironment);
|
|
2812
|
-
const currentTokens = estimateBlockTokens(orderedMessages);
|
|
2813
|
-
if (isPromiseLike(currentTokens))
|
|
2814
|
-
return currentTokens.then(async (resolvedCurrentTokens) => {
|
|
2815
|
-
if (resolvedCurrentTokens <= maxTokens)
|
|
2816
|
-
return conversation;
|
|
2817
|
-
const { blocks: blocks2, messageToBlock: messageToBlock2 } = await buildMessageBlocksAsync(orderedMessages, estimateBlockTokens, preserveToolPairs);
|
|
2818
|
-
return truncateToTokenLimitFromBlocks(conversation, maxTokens, resolvedCurrentTokens, orderedMessages, blocks2, messageToBlock2, options, resolvedEnvironment);
|
|
2819
|
-
});
|
|
2820
|
-
if (currentTokens <= maxTokens)
|
|
2821
|
-
return conversation;
|
|
2822
|
-
const { blocks, messageToBlock } = buildMessageBlocks(orderedMessages, (messages) => estimateMessageBlockTokens(messages, options, resolvedEnvironment), preserveToolPairs);
|
|
2823
|
-
return truncateToTokenLimitFromBlocks(conversation, maxTokens, currentTokens, orderedMessages, blocks, messageToBlock, options, resolvedEnvironment);
|
|
2824
|
-
}
|
|
2825
|
-
function getRecentMessages(conversation, count, options) {
|
|
2826
|
-
const includeHidden = options?.includeHidden ?? false;
|
|
2827
|
-
const includeSystem = options?.includeSystem ?? false;
|
|
2828
|
-
const preserveToolPairs = options?.preserveToolPairs ?? true;
|
|
2829
|
-
const filtered = getOrderedMessages(conversation).filter((m) => {
|
|
2830
|
-
if (!includeHidden && m.hidden)
|
|
2831
|
-
return false;
|
|
2832
|
-
if (!includeSystem && m.role === "system")
|
|
2833
|
-
return false;
|
|
2834
|
-
return true;
|
|
2835
|
-
});
|
|
2836
|
-
if (!preserveToolPairs)
|
|
2837
|
-
return filtered.slice(-count);
|
|
2838
|
-
const { messageToBlock } = buildMessageBlocks(filtered, () => 0, preserveToolPairs);
|
|
2839
|
-
return collectMessagesFromBlocks(collectBlocksForMessages(filtered.slice(-count), messageToBlock));
|
|
2840
|
-
}
|
|
2841
|
-
function truncateFromPosition(conversation, position, options, environment) {
|
|
2842
|
-
assertConversationSafe(conversation);
|
|
2843
|
-
const preserveSystem = options?.preserveSystemMessages ?? true;
|
|
2844
|
-
const preserveToolPairs = options?.preserveToolPairs ?? true;
|
|
2845
|
-
const now = resolveConversationEnvironment(environment).now();
|
|
2846
|
-
const ordered = getOrderedMessages(conversation);
|
|
2847
|
-
const { messageToBlock } = buildMessageBlocks(ordered, () => 0, preserveToolPairs);
|
|
2848
|
-
const systemMessages = preserveSystem ? ordered.filter((m) => m.role === "system" && m.position < position) : [];
|
|
2849
|
-
const streamingMessages = ordered.filter((m) => isStreamingMessage(m) && m.position < position);
|
|
2850
|
-
const keptMessages = ordered.filter((m) => m.position >= position);
|
|
2851
|
-
const systemBlocks = collectBlocksForMessages(systemMessages, messageToBlock);
|
|
2852
|
-
const streamingBlocks = collectBlocksForMessages(streamingMessages, messageToBlock);
|
|
2853
|
-
const keptBlocks = collectBlocksForMessages(keptMessages, messageToBlock);
|
|
2854
|
-
const renumbered = collectMessagesFromBlocks([
|
|
2855
|
-
...systemBlocks,
|
|
2856
|
-
...streamingBlocks,
|
|
2857
|
-
...keptBlocks
|
|
2858
|
-
]).map((message, index) => cloneMessageWithPosition(message, index, copyContent(message.content)));
|
|
2859
|
-
return ensureTruncationSafe(toReadonly({
|
|
2860
|
-
...conversation,
|
|
2861
|
-
ids: renumbered.map((message) => message.id),
|
|
2862
|
-
messages: toIdRecord(renumbered),
|
|
2863
|
-
updatedAt: now
|
|
2864
|
-
}), preserveToolPairs, "truncateFromPosition");
|
|
2865
|
-
}
|
|
2866
|
-
|
|
2867
|
-
// ../../node_modules/conversationalist/dist/history-5vdiTmMp.js
|
|
2868
|
-
function calculateChunkSize(totalTokens, averageMessageTokens, contextWindow, options) {
|
|
2869
|
-
const baseRatio = options?.baseChunkRatio ?? 0.4;
|
|
2870
|
-
const minRatio = options?.minimumChunkRatio ?? 0.15;
|
|
2871
|
-
const safety = options?.safetyMargin ?? 1.2;
|
|
2872
|
-
const ratio = averageMessageTokens > contextWindow * 0.1 ? minRatio : baseRatio;
|
|
2873
|
-
return Math.max(1, Math.floor(totalTokens * ratio / safety));
|
|
2874
|
-
}
|
|
2875
|
-
function partitionMessages(conversation, options, _environment) {
|
|
2876
|
-
const preserveRecent = options?.preserveRecentCount ?? 4;
|
|
2877
|
-
const preserveSystem = options?.preserveSystemMessages ?? true;
|
|
2878
|
-
const preserveToolPairs = options?.preserveToolPairs ?? true;
|
|
2879
|
-
const allMessages = getMessages(conversation);
|
|
2880
|
-
const systemMessages = preserveSystem ? allMessages.filter((m) => m.role === "system") : [];
|
|
2881
|
-
const streamingMessages = allMessages.filter(isStreamingMessage);
|
|
2882
|
-
const nonSystem = allMessages.filter((m) => m.role !== "system");
|
|
2883
|
-
if (nonSystem.length <= preserveRecent)
|
|
2884
|
-
return {
|
|
2885
|
-
compactable: [],
|
|
2886
|
-
preserved: [...allMessages]
|
|
2887
|
-
};
|
|
2888
|
-
let recentMessages = nonSystem.slice(-preserveRecent);
|
|
2889
|
-
if (preserveToolPairs && recentMessages.length > 0) {
|
|
2890
|
-
const firstRecent = recentMessages[0];
|
|
2891
|
-
if (firstRecent.role === "tool-result" && firstRecent.toolResult) {
|
|
2892
|
-
const callId = firstRecent.toolResult.callId;
|
|
2893
|
-
const toolCallMsg = nonSystem.find((m) => m.role === "tool-call" && m.toolCall?.id === callId);
|
|
2894
|
-
if (toolCallMsg && !recentMessages.includes(toolCallMsg))
|
|
2895
|
-
recentMessages = [toolCallMsg, ...recentMessages];
|
|
2896
|
-
}
|
|
2897
|
-
}
|
|
2898
|
-
const preservedSet = new Set([
|
|
2899
|
-
...systemMessages.map((m) => m.id),
|
|
2900
|
-
...recentMessages.map((m) => m.id),
|
|
2901
|
-
...streamingMessages.map((m) => m.id)
|
|
2902
|
-
]);
|
|
2903
|
-
return {
|
|
2904
|
-
compactable: allMessages.filter((m) => !preservedSet.has(m.id)),
|
|
2905
|
-
preserved: allMessages.filter((m) => preservedSet.has(m.id))
|
|
2906
|
-
};
|
|
2907
|
-
}
|
|
2908
|
-
function chunkMessages(messages, chunkTokenBudget, estimator) {
|
|
2909
|
-
if (messages.length === 0)
|
|
2910
|
-
return [];
|
|
2911
|
-
const chunks = [];
|
|
2912
|
-
let currentChunk = [];
|
|
2913
|
-
let currentTokens = 0;
|
|
2914
|
-
for (let i = 0;i < messages.length; i++) {
|
|
2915
|
-
const message = messages[i];
|
|
2916
|
-
const tokens = estimator(message);
|
|
2917
|
-
let pairedMessages = [message];
|
|
2918
|
-
let pairedTokens = tokens;
|
|
2919
|
-
if (message.role === "tool-call" && message.toolCall) {
|
|
2920
|
-
const nextIdx = i + 1;
|
|
2921
|
-
if (nextIdx < messages.length) {
|
|
2922
|
-
const next = messages[nextIdx];
|
|
2923
|
-
if (next.role === "tool-result" && next.toolResult?.callId === message.toolCall.id) {
|
|
2924
|
-
pairedMessages = [message, next];
|
|
2925
|
-
pairedTokens = tokens + estimator(next);
|
|
2926
|
-
i++;
|
|
2927
|
-
}
|
|
2928
|
-
}
|
|
2929
|
-
}
|
|
2930
|
-
if (currentTokens + pairedTokens > chunkTokenBudget && currentChunk.length > 0) {
|
|
2931
|
-
chunks.push(currentChunk);
|
|
2932
|
-
currentChunk = [];
|
|
2933
|
-
currentTokens = 0;
|
|
2934
|
-
}
|
|
2935
|
-
currentChunk.push(...pairedMessages);
|
|
2936
|
-
currentTokens += pairedTokens;
|
|
2937
|
-
}
|
|
2938
|
-
if (currentChunk.length > 0)
|
|
2939
|
-
chunks.push(currentChunk);
|
|
2940
|
-
return chunks;
|
|
2941
|
-
}
|
|
2942
|
-
var STRIPPED_PLACEHOLDER = "[tool result]";
|
|
2943
|
-
function stripStructuralToolBlocks(content) {
|
|
2944
|
-
if (typeof content === "string")
|
|
2945
|
-
return content;
|
|
2946
|
-
return content.flatMap((part) => {
|
|
2947
|
-
switch (part.type) {
|
|
2948
|
-
case "server_tool_use":
|
|
2949
|
-
return [{
|
|
2950
|
-
...part,
|
|
2951
|
-
input: STRIPPED_PLACEHOLDER
|
|
2952
|
-
}];
|
|
2953
|
-
case "web_search_tool_result":
|
|
2954
|
-
case "web_fetch_tool_result":
|
|
2955
|
-
case "code_execution_tool_result":
|
|
2956
|
-
case "bash_code_execution_tool_result":
|
|
2957
|
-
case "text_editor_code_execution_tool_result":
|
|
2958
|
-
return [{
|
|
2959
|
-
...part,
|
|
2960
|
-
content: STRIPPED_PLACEHOLDER
|
|
2961
|
-
}];
|
|
2962
|
-
case "text": {
|
|
2963
|
-
if (part.citations === undefined)
|
|
2964
|
-
return [part];
|
|
2965
|
-
const { citations: _citations, ...rest } = part;
|
|
2966
|
-
return [rest];
|
|
2967
|
-
}
|
|
2968
|
-
case "thinking":
|
|
2969
|
-
case "redacted_thinking":
|
|
2970
|
-
return [];
|
|
2971
|
-
default:
|
|
2972
|
-
return [part];
|
|
2973
|
-
}
|
|
2974
|
-
});
|
|
2975
|
-
}
|
|
2976
|
-
function stripToolResultDetails(messages) {
|
|
2977
|
-
return messages.map((message) => {
|
|
2978
|
-
if (message.role === "tool-result" && message.toolResult)
|
|
2979
|
-
return {
|
|
2980
|
-
...message,
|
|
2981
|
-
content: STRIPPED_PLACEHOLDER,
|
|
2982
|
-
toolResult: {
|
|
2983
|
-
...message.toolResult,
|
|
2984
|
-
content: STRIPPED_PLACEHOLDER
|
|
2985
|
-
}
|
|
2986
|
-
};
|
|
2987
|
-
if (typeof message.content !== "string")
|
|
2988
|
-
return {
|
|
2989
|
-
...message,
|
|
2990
|
-
content: stripStructuralToolBlocks(message.content)
|
|
2991
|
-
};
|
|
2992
|
-
return message;
|
|
2993
|
-
});
|
|
2994
|
-
}
|
|
2995
|
-
async function compactConversation(conversation, summarizer, options, environment) {
|
|
2996
|
-
const env = resolveConversationEnvironment(environment);
|
|
2997
|
-
const estimator = env.estimateTokens ?? simpleTokenEstimator;
|
|
2998
|
-
const { compactable, preserved } = partitionMessages(conversation, options, environment);
|
|
2999
|
-
if (compactable.length === 0)
|
|
3000
|
-
return {
|
|
3001
|
-
conversation,
|
|
3002
|
-
result: {
|
|
3003
|
-
compacted: false,
|
|
3004
|
-
chunksProcessed: 0,
|
|
3005
|
-
messagesRemoved: 0,
|
|
3006
|
-
summaryContent: ""
|
|
3007
|
-
}
|
|
3008
|
-
};
|
|
3009
|
-
const totalTokens = compactable.reduce((sum, m) => sum + estimator(m), 0);
|
|
3010
|
-
const chunkBudget = calculateChunkSize(totalTokens, totalTokens / compactable.length, totalTokens * 3, options);
|
|
3011
|
-
const chunks = chunkMessages(stripToolResultDetails(compactable), chunkBudget, estimator);
|
|
3012
|
-
const summaries = [];
|
|
3013
|
-
for (const chunk of chunks) {
|
|
3014
|
-
const summary = await summarizer(chunk, { maxTokens: options?.maxSummaryTokens });
|
|
3015
|
-
summaries.push(summary);
|
|
3016
|
-
}
|
|
3017
|
-
const summaryContent = summaries.length === 1 ? summaries[0] : summaries.join(`
|
|
54
|
+
// src/components/chat-conversation-list/index.ts
|
|
55
|
+
import"@lostgradient/cinder/chat-conversation-list/styles";
|
|
3018
56
|
|
|
3019
|
-
|
|
57
|
+
// src/components/chat-conversation-list/chat-conversation-list.svelte
|
|
58
|
+
import"svelte/internal/disclose-version";
|
|
59
|
+
import * as $ from "svelte/internal/client";
|
|
3020
60
|
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
id: conversation.id,
|
|
3025
|
-
title: conversation.title,
|
|
3026
|
-
status: conversation.status,
|
|
3027
|
-
metadata: { ...conversation.metadata },
|
|
3028
|
-
ids: [],
|
|
3029
|
-
messages: {},
|
|
3030
|
-
createdAt: conversation.createdAt,
|
|
3031
|
-
updatedAt: env.now()
|
|
3032
|
-
};
|
|
3033
|
-
compacted = ensureConversationSafe(toReadonly(compacted));
|
|
3034
|
-
compacted = appendMessages(compacted, {
|
|
3035
|
-
role: "system",
|
|
3036
|
-
content: summaryContent,
|
|
3037
|
-
metadata: { compactionSummary: true }
|
|
3038
|
-
}, env);
|
|
3039
|
-
if (preserved.length > 0) {
|
|
3040
|
-
const preservedInputs = preserved.map((m) => ({
|
|
3041
|
-
role: m.role,
|
|
3042
|
-
content: typeof m.content === "string" ? m.content : [...m.content],
|
|
3043
|
-
metadata: { ...m.metadata },
|
|
3044
|
-
hidden: m.hidden,
|
|
3045
|
-
toolCall: m.toolCall ? { ...m.toolCall } : undefined,
|
|
3046
|
-
toolResult: m.toolResult ? { ...m.toolResult } : undefined,
|
|
3047
|
-
tokenUsage: m.tokenUsage ? { ...m.tokenUsage } : undefined
|
|
3048
|
-
}));
|
|
3049
|
-
compacted = appendMessages(compacted, ...preservedInputs, env);
|
|
3050
|
-
}
|
|
3051
|
-
return {
|
|
3052
|
-
conversation: compacted,
|
|
3053
|
-
result: {
|
|
3054
|
-
compacted: true,
|
|
3055
|
-
chunksProcessed: chunks.length,
|
|
3056
|
-
messagesRemoved: compactable.length,
|
|
3057
|
-
summaryContent
|
|
3058
|
-
}
|
|
3059
|
-
};
|
|
3060
|
-
}
|
|
3061
|
-
var ConversationEvent = class extends Event {
|
|
3062
|
-
action;
|
|
3063
|
-
conversation;
|
|
3064
|
-
previousConversation;
|
|
3065
|
-
messageIds;
|
|
3066
|
-
toolCallIds;
|
|
3067
|
-
constructor(type, detail) {
|
|
3068
|
-
super(type);
|
|
3069
|
-
this.action = detail.action;
|
|
3070
|
-
this.conversation = detail.conversation;
|
|
3071
|
-
this.previousConversation = detail.previousConversation;
|
|
3072
|
-
if (detail.messageIds !== undefined)
|
|
3073
|
-
this.messageIds = detail.messageIds;
|
|
3074
|
-
if (detail.toolCallIds !== undefined)
|
|
3075
|
-
this.toolCallIds = detail.toolCallIds;
|
|
3076
|
-
}
|
|
3077
|
-
};
|
|
3078
|
-
var ConversationChangeEvent = class ConversationChangeEvent2 extends ConversationEvent {
|
|
3079
|
-
static type = "change";
|
|
3080
|
-
constructor(detail) {
|
|
3081
|
-
super(ConversationChangeEvent2.type, detail);
|
|
3082
|
-
}
|
|
3083
|
-
};
|
|
3084
|
-
var ConversationPushEvent = class ConversationPushEvent2 extends ConversationEvent {
|
|
3085
|
-
static type = "push";
|
|
3086
|
-
constructor(detail) {
|
|
3087
|
-
super(ConversationPushEvent2.type, detail);
|
|
3088
|
-
}
|
|
3089
|
-
};
|
|
3090
|
-
var ConversationUndoEvent = class ConversationUndoEvent2 extends ConversationEvent {
|
|
3091
|
-
static type = "undo";
|
|
3092
|
-
constructor(detail) {
|
|
3093
|
-
super(ConversationUndoEvent2.type, detail);
|
|
3094
|
-
}
|
|
3095
|
-
};
|
|
3096
|
-
var ConversationRedoEvent = class ConversationRedoEvent2 extends ConversationEvent {
|
|
3097
|
-
static type = "redo";
|
|
3098
|
-
constructor(detail) {
|
|
3099
|
-
super(ConversationRedoEvent2.type, detail);
|
|
3100
|
-
}
|
|
3101
|
-
};
|
|
3102
|
-
var ConversationSwitchEvent = class ConversationSwitchEvent2 extends ConversationEvent {
|
|
3103
|
-
static type = "switch";
|
|
3104
|
-
constructor(detail) {
|
|
3105
|
-
super(ConversationSwitchEvent2.type, detail);
|
|
3106
|
-
}
|
|
3107
|
-
};
|
|
3108
|
-
var MessagesAppendedEvent = class MessagesAppendedEvent2 extends ConversationEvent {
|
|
3109
|
-
static type = "messages.appended";
|
|
3110
|
-
constructor(detail) {
|
|
3111
|
-
super(MessagesAppendedEvent2.type, detail);
|
|
3112
|
-
}
|
|
3113
|
-
};
|
|
3114
|
-
var MessagesUpdatedEvent = class MessagesUpdatedEvent2 extends ConversationEvent {
|
|
3115
|
-
static type = "messages.updated";
|
|
3116
|
-
constructor(detail) {
|
|
3117
|
-
super(MessagesUpdatedEvent2.type, detail);
|
|
3118
|
-
}
|
|
3119
|
-
};
|
|
3120
|
-
var MessagesRemovedEvent = class MessagesRemovedEvent2 extends ConversationEvent {
|
|
3121
|
-
static type = "messages.removed";
|
|
3122
|
-
constructor(detail) {
|
|
3123
|
-
super(MessagesRemovedEvent2.type, detail);
|
|
3124
|
-
}
|
|
3125
|
-
};
|
|
3126
|
-
var ToolCallsAppendedEvent = class ToolCallsAppendedEvent2 extends ConversationEvent {
|
|
3127
|
-
static type = "tool-calls.appended";
|
|
3128
|
-
constructor(detail) {
|
|
3129
|
-
super(ToolCallsAppendedEvent2.type, detail);
|
|
3130
|
-
}
|
|
3131
|
-
};
|
|
3132
|
-
var ToolResultsAppendedEvent = class ToolResultsAppendedEvent2 extends ConversationEvent {
|
|
3133
|
-
static type = "tool-results.appended";
|
|
3134
|
-
constructor(detail) {
|
|
3135
|
-
super(ToolResultsAppendedEvent2.type, detail);
|
|
3136
|
-
}
|
|
3137
|
-
};
|
|
3138
|
-
var StreamStartedEvent = class StreamStartedEvent2 extends ConversationEvent {
|
|
3139
|
-
static type = "stream.started";
|
|
3140
|
-
constructor(detail) {
|
|
3141
|
-
super(StreamStartedEvent2.type, detail);
|
|
3142
|
-
}
|
|
3143
|
-
};
|
|
3144
|
-
var StreamUpdatedEvent = class StreamUpdatedEvent2 extends ConversationEvent {
|
|
3145
|
-
static type = "stream.updated";
|
|
3146
|
-
constructor(detail) {
|
|
3147
|
-
super(StreamUpdatedEvent2.type, detail);
|
|
3148
|
-
}
|
|
3149
|
-
};
|
|
3150
|
-
var StreamFinalizedEvent = class StreamFinalizedEvent2 extends ConversationEvent {
|
|
3151
|
-
static type = "stream.finalized";
|
|
3152
|
-
constructor(detail) {
|
|
3153
|
-
super(StreamFinalizedEvent2.type, detail);
|
|
3154
|
-
}
|
|
3155
|
-
};
|
|
3156
|
-
var StreamCancelledEvent = class StreamCancelledEvent2 extends ConversationEvent {
|
|
3157
|
-
static type = "stream.cancelled";
|
|
3158
|
-
constructor(detail) {
|
|
3159
|
-
super(StreamCancelledEvent2.type, detail);
|
|
3160
|
-
}
|
|
3161
|
-
};
|
|
3162
|
-
var CompactionStartedEvent = class CompactionStartedEvent2 extends ConversationEvent {
|
|
3163
|
-
static type = "compaction.started";
|
|
3164
|
-
constructor(detail) {
|
|
3165
|
-
super(CompactionStartedEvent2.type, detail);
|
|
3166
|
-
}
|
|
3167
|
-
};
|
|
3168
|
-
var CompactionCompletedEvent = class CompactionCompletedEvent2 extends ConversationEvent {
|
|
3169
|
-
static type = "compaction.completed";
|
|
3170
|
-
constructor(detail) {
|
|
3171
|
-
super(CompactionCompletedEvent2.type, detail);
|
|
3172
|
-
}
|
|
3173
|
-
};
|
|
3174
|
-
var SessionForkedEvent = class SessionForkedEvent2 extends ConversationEvent {
|
|
3175
|
-
static type = "session.forked";
|
|
3176
|
-
constructor(detail) {
|
|
3177
|
-
super(SessionForkedEvent2.type, detail);
|
|
3178
|
-
}
|
|
3179
|
-
};
|
|
3180
|
-
var SessionTaggedEvent = class SessionTaggedEvent2 extends ConversationEvent {
|
|
3181
|
-
static type = "session.tagged";
|
|
3182
|
-
constructor(detail) {
|
|
3183
|
-
super(SessionTaggedEvent2.type, detail);
|
|
3184
|
-
}
|
|
3185
|
-
};
|
|
3186
|
-
var SessionRenamedEvent = class SessionRenamedEvent2 extends ConversationEvent {
|
|
3187
|
-
static type = "session.renamed";
|
|
3188
|
-
constructor(detail) {
|
|
3189
|
-
super(SessionRenamedEvent2.type, detail);
|
|
3190
|
-
}
|
|
3191
|
-
};
|
|
3192
|
-
var PersistenceErrorEvent = class PersistenceErrorEvent2 extends Event {
|
|
3193
|
-
static type = "persistence.error";
|
|
3194
|
-
error;
|
|
3195
|
-
constructor(error) {
|
|
3196
|
-
super(PersistenceErrorEvent2.type);
|
|
3197
|
-
this.error = error;
|
|
3198
|
-
}
|
|
3199
|
-
};
|
|
3200
|
-
var conversationEventConstructors = {
|
|
3201
|
-
[ConversationChangeEvent.type]: ConversationChangeEvent,
|
|
3202
|
-
[ConversationPushEvent.type]: ConversationPushEvent,
|
|
3203
|
-
[ConversationUndoEvent.type]: ConversationUndoEvent,
|
|
3204
|
-
[ConversationRedoEvent.type]: ConversationRedoEvent,
|
|
3205
|
-
[ConversationSwitchEvent.type]: ConversationSwitchEvent,
|
|
3206
|
-
[MessagesAppendedEvent.type]: MessagesAppendedEvent,
|
|
3207
|
-
[MessagesUpdatedEvent.type]: MessagesUpdatedEvent,
|
|
3208
|
-
[MessagesRemovedEvent.type]: MessagesRemovedEvent,
|
|
3209
|
-
[ToolCallsAppendedEvent.type]: ToolCallsAppendedEvent,
|
|
3210
|
-
[ToolResultsAppendedEvent.type]: ToolResultsAppendedEvent,
|
|
3211
|
-
[StreamStartedEvent.type]: StreamStartedEvent,
|
|
3212
|
-
[StreamUpdatedEvent.type]: StreamUpdatedEvent,
|
|
3213
|
-
[StreamFinalizedEvent.type]: StreamFinalizedEvent,
|
|
3214
|
-
[StreamCancelledEvent.type]: StreamCancelledEvent,
|
|
3215
|
-
[CompactionStartedEvent.type]: CompactionStartedEvent,
|
|
3216
|
-
[CompactionCompletedEvent.type]: CompactionCompletedEvent,
|
|
3217
|
-
[SessionForkedEvent.type]: SessionForkedEvent,
|
|
3218
|
-
[SessionTaggedEvent.type]: SessionTaggedEvent,
|
|
3219
|
-
[SessionRenamedEvent.type]: SessionRenamedEvent
|
|
3220
|
-
};
|
|
3221
|
-
function Y(H, f, R) {
|
|
3222
|
-
let P = R?.bufferSize ?? 256, D = R?.signal, G = [], M = null, B = false;
|
|
3223
|
-
function C(K) {
|
|
3224
|
-
let Q = K;
|
|
3225
|
-
if (M) {
|
|
3226
|
-
let z2 = M;
|
|
3227
|
-
M = null, z2({
|
|
3228
|
-
value: Q,
|
|
3229
|
-
done: false
|
|
3230
|
-
});
|
|
3231
|
-
} else if (G.length < P)
|
|
3232
|
-
G.push(Q);
|
|
3233
|
-
}
|
|
3234
|
-
function J() {
|
|
3235
|
-
if (B)
|
|
3236
|
-
return;
|
|
3237
|
-
if (B = true, H.removeEventListener(f, C), M) {
|
|
3238
|
-
let K = M;
|
|
3239
|
-
M = null, K({
|
|
3240
|
-
value: undefined,
|
|
3241
|
-
done: true
|
|
3242
|
-
});
|
|
3243
|
-
}
|
|
3244
|
-
}
|
|
3245
|
-
if (D?.aborted)
|
|
3246
|
-
B = true;
|
|
3247
|
-
else
|
|
3248
|
-
H.addEventListener(f, C), D?.addEventListener("abort", J, { once: true });
|
|
3249
|
-
return {
|
|
3250
|
-
next() {
|
|
3251
|
-
if (G.length > 0)
|
|
3252
|
-
return Promise.resolve({
|
|
3253
|
-
value: G.shift(),
|
|
3254
|
-
done: false
|
|
3255
|
-
});
|
|
3256
|
-
if (B)
|
|
3257
|
-
return Promise.resolve({
|
|
3258
|
-
value: undefined,
|
|
3259
|
-
done: true
|
|
3260
|
-
});
|
|
3261
|
-
return new Promise((K) => {
|
|
3262
|
-
M = K;
|
|
3263
|
-
});
|
|
3264
|
-
},
|
|
3265
|
-
return() {
|
|
3266
|
-
return J(), Promise.resolve({
|
|
3267
|
-
value: undefined,
|
|
3268
|
-
done: true
|
|
3269
|
-
});
|
|
3270
|
-
},
|
|
3271
|
-
[Symbol.asyncIterator]() {
|
|
3272
|
-
return this;
|
|
3273
|
-
}
|
|
3274
|
-
};
|
|
3275
|
-
}
|
|
3276
|
-
function Z(H, f, R) {
|
|
3277
|
-
return { subscribe(P, D, G) {
|
|
3278
|
-
let M = typeof P === "function" ? {
|
|
3279
|
-
next: P,
|
|
3280
|
-
error: D,
|
|
3281
|
-
complete: G
|
|
3282
|
-
} : P ?? {}, B = new AbortController, C = false, J = {
|
|
3283
|
-
unsubscribe() {
|
|
3284
|
-
if (C)
|
|
3285
|
-
return;
|
|
3286
|
-
C = true, B.abort(), M.complete?.();
|
|
3287
|
-
},
|
|
3288
|
-
get closed() {
|
|
3289
|
-
return C;
|
|
3290
|
-
}
|
|
3291
|
-
};
|
|
3292
|
-
if (M.start?.(J), C)
|
|
3293
|
-
return J;
|
|
3294
|
-
function U(Q) {
|
|
3295
|
-
M.next?.(Q);
|
|
3296
|
-
}
|
|
3297
|
-
let K = R?.signal ? AbortSignal.any([R.signal, B.signal]) : B.signal;
|
|
3298
|
-
if (H.addEventListener(f, U, { signal: K }), R?.signal)
|
|
3299
|
-
R.signal.addEventListener("abort", () => {
|
|
3300
|
-
if (!C)
|
|
3301
|
-
C = true, M.complete?.();
|
|
3302
|
-
}, {
|
|
3303
|
-
once: true,
|
|
3304
|
-
signal: B.signal
|
|
3305
|
-
});
|
|
3306
|
-
return J;
|
|
3307
|
-
} };
|
|
3308
|
-
}
|
|
3309
|
-
var W = class extends EventTarget {
|
|
3310
|
-
constructor() {
|
|
3311
|
-
super();
|
|
3312
|
-
}
|
|
3313
|
-
addEventListener(H, f, R) {
|
|
3314
|
-
super.addEventListener(H, f, R);
|
|
3315
|
-
}
|
|
3316
|
-
removeEventListener(H, f, R) {
|
|
3317
|
-
super.removeEventListener(H, f, R);
|
|
3318
|
-
}
|
|
3319
|
-
dispatch(H) {
|
|
3320
|
-
return this.dispatchEvent(H);
|
|
3321
|
-
}
|
|
3322
|
-
};
|
|
3323
|
-
var j = class extends W {
|
|
3324
|
-
#H = new AbortController;
|
|
3325
|
-
#f = /* @__PURE__ */ new Set;
|
|
3326
|
-
constructor() {
|
|
3327
|
-
super();
|
|
3328
|
-
}
|
|
3329
|
-
get completed() {
|
|
3330
|
-
return this.#H.signal.aborted;
|
|
3331
|
-
}
|
|
3332
|
-
get signal() {
|
|
3333
|
-
return this.#H.signal;
|
|
3334
|
-
}
|
|
3335
|
-
complete() {
|
|
3336
|
-
if (!this.#H.signal.aborted)
|
|
3337
|
-
this.#H.abort();
|
|
3338
|
-
}
|
|
3339
|
-
dispatchEvent(H) {
|
|
3340
|
-
let f = super.dispatchEvent(H);
|
|
3341
|
-
for (let R of this.#f)
|
|
3342
|
-
R(H);
|
|
3343
|
-
return f;
|
|
3344
|
-
}
|
|
3345
|
-
on(H, f) {
|
|
3346
|
-
return Z(this, H, { signal: f?.signal ?? this.signal });
|
|
3347
|
-
}
|
|
3348
|
-
once(H, f) {
|
|
3349
|
-
this.addEventListener(H, f, {
|
|
3350
|
-
once: true,
|
|
3351
|
-
signal: this.signal
|
|
3352
|
-
});
|
|
3353
|
-
}
|
|
3354
|
-
subscribe(H, f, R, P) {
|
|
3355
|
-
return this.on(H).subscribe(f, R, P);
|
|
3356
|
-
}
|
|
3357
|
-
toObservable() {
|
|
3358
|
-
return { subscribe: (H, f, R) => {
|
|
3359
|
-
let P = typeof H === "function" ? {
|
|
3360
|
-
next: H,
|
|
3361
|
-
error: f,
|
|
3362
|
-
complete: R
|
|
3363
|
-
} : H ?? {}, D = false, G = (B) => {
|
|
3364
|
-
if (!D)
|
|
3365
|
-
P.next?.(B);
|
|
3366
|
-
};
|
|
3367
|
-
this.#f.add(G);
|
|
3368
|
-
let M = () => {
|
|
3369
|
-
if (!D)
|
|
3370
|
-
D = true, this.#f.delete(G), P.complete?.();
|
|
3371
|
-
};
|
|
3372
|
-
return this.signal.addEventListener("abort", M, { once: true }), {
|
|
3373
|
-
unsubscribe: () => {
|
|
3374
|
-
if (D)
|
|
3375
|
-
return;
|
|
3376
|
-
D = true, this.#f.delete(G), this.signal.removeEventListener("abort", M), P.complete?.();
|
|
3377
|
-
},
|
|
3378
|
-
get closed() {
|
|
3379
|
-
return D;
|
|
3380
|
-
}
|
|
3381
|
-
};
|
|
3382
|
-
} };
|
|
3383
|
-
}
|
|
3384
|
-
events(H, f) {
|
|
3385
|
-
return Y(this, H, {
|
|
3386
|
-
signal: f?.signal ?? this.signal,
|
|
3387
|
-
bufferSize: f?.bufferSize
|
|
3388
|
-
});
|
|
3389
|
-
}
|
|
3390
|
-
};
|
|
3391
|
-
function diffConversationMessages(previousConversation, nextConversation) {
|
|
3392
|
-
const previousIds = new Set(previousConversation.ids);
|
|
3393
|
-
const nextIds = new Set(nextConversation.ids);
|
|
3394
|
-
const appended = nextConversation.ids.filter((id) => !previousIds.has(id));
|
|
3395
|
-
const removed = previousConversation.ids.filter((id) => !nextIds.has(id));
|
|
3396
|
-
const updated = [];
|
|
3397
|
-
for (const id of nextConversation.ids) {
|
|
3398
|
-
if (!previousIds.has(id))
|
|
3399
|
-
continue;
|
|
3400
|
-
const previousMessage = previousConversation.messages[id];
|
|
3401
|
-
const nextMessage = nextConversation.messages[id];
|
|
3402
|
-
if (!previousMessage || !nextMessage)
|
|
3403
|
-
continue;
|
|
3404
|
-
if (JSON.stringify(previousMessage) !== JSON.stringify(nextMessage))
|
|
3405
|
-
updated.push(id);
|
|
3406
|
-
}
|
|
3407
|
-
return {
|
|
3408
|
-
appended,
|
|
3409
|
-
updated,
|
|
3410
|
-
removed
|
|
3411
|
-
};
|
|
3412
|
-
}
|
|
3413
|
-
function collectToolCallIds(conversation, messageIds) {
|
|
3414
|
-
if (!messageIds || messageIds.length === 0)
|
|
3415
|
-
return;
|
|
3416
|
-
const ids = /* @__PURE__ */ new Set;
|
|
3417
|
-
for (const messageId of messageIds) {
|
|
3418
|
-
const message = conversation.messages[messageId];
|
|
3419
|
-
if (!message)
|
|
3420
|
-
continue;
|
|
3421
|
-
if (message.toolCall?.id)
|
|
3422
|
-
ids.add(message.toolCall.id);
|
|
3423
|
-
if (message.toolResult?.callId)
|
|
3424
|
-
ids.add(message.toolResult.callId);
|
|
3425
|
-
}
|
|
3426
|
-
return ids.size > 0 ? [...ids] : undefined;
|
|
3427
|
-
}
|
|
3428
|
-
async function loadConversationAdapter(provider) {
|
|
3429
|
-
switch (provider) {
|
|
3430
|
-
case "openai":
|
|
3431
|
-
return (await Promise.resolve().then(() => (init_openai(), exports_openai))).openAIConversationAdapter;
|
|
3432
|
-
case "anthropic":
|
|
3433
|
-
return (await Promise.resolve().then(() => (init_anthropic(), exports_anthropic))).anthropicConversationAdapter;
|
|
3434
|
-
case "gemini":
|
|
3435
|
-
return (await Promise.resolve().then(() => (init_gemini(), exports_gemini))).geminiConversationAdapter;
|
|
3436
|
-
}
|
|
3437
|
-
}
|
|
3438
|
-
var Conversation = class Conversation2 {
|
|
3439
|
-
currentNode;
|
|
3440
|
-
environment;
|
|
3441
|
-
emitter = new j;
|
|
3442
|
-
constructor(initial = createConversationHistory(), environment) {
|
|
3443
|
-
this.environment = resolveConversationEnvironment(environment);
|
|
3444
|
-
const safeInitial = ensureConversationSafe(initial);
|
|
3445
|
-
this.currentNode = {
|
|
3446
|
-
conversation: safeInitial,
|
|
3447
|
-
parent: null,
|
|
3448
|
-
children: []
|
|
3449
|
-
};
|
|
3450
|
-
if (this.environment.persistence) {
|
|
3451
|
-
const store = this.environment.persistence;
|
|
3452
|
-
const setTimeoutFunction = this.environment.setTimeoutFunction ?? ((callback, milliseconds) => setTimeout(() => {
|
|
3453
|
-
callback();
|
|
3454
|
-
}, milliseconds));
|
|
3455
|
-
const clearTimeoutFunction = this.environment.clearTimeoutFunction ?? ((handle) => clearTimeout(handle));
|
|
3456
|
-
const debounceMilliseconds = this.environment.persistenceDebounceMilliseconds ?? 100;
|
|
3457
|
-
let debounceTimer;
|
|
3458
|
-
let hasDebounceTimer = false;
|
|
3459
|
-
this.addEventListener("change", () => {
|
|
3460
|
-
if (hasDebounceTimer)
|
|
3461
|
-
clearTimeoutFunction(debounceTimer);
|
|
3462
|
-
debounceTimer = setTimeoutFunction(() => {
|
|
3463
|
-
const conversation = this.current;
|
|
3464
|
-
return Promise.all([store.set(`session:${conversation.id}`, JSON.stringify(conversation)), store.set(`session-info:${conversation.id}`, JSON.stringify(toSessionInfo(conversation)))]).then(() => {
|
|
3465
|
-
return;
|
|
3466
|
-
}).catch((error) => {
|
|
3467
|
-
this.emitter.dispatchEvent(new PersistenceErrorEvent(error));
|
|
3468
|
-
});
|
|
3469
|
-
}, debounceMilliseconds);
|
|
3470
|
-
hasDebounceTimer = true;
|
|
3471
|
-
});
|
|
3472
|
-
}
|
|
3473
|
-
}
|
|
3474
|
-
buildEventDetail(action, previousConversation, context = {}) {
|
|
3475
|
-
return {
|
|
3476
|
-
action,
|
|
3477
|
-
conversation: this.current,
|
|
3478
|
-
previousConversation,
|
|
3479
|
-
...context.messageIds && context.messageIds.length > 0 ? { messageIds: context.messageIds } : {},
|
|
3480
|
-
...context.toolCallIds && context.toolCallIds.length > 0 ? { toolCallIds: context.toolCallIds } : {}
|
|
3481
|
-
};
|
|
3482
|
-
}
|
|
3483
|
-
emitConversationEvent(type, detail) {
|
|
3484
|
-
const EventConstructor = conversationEventConstructors[type];
|
|
3485
|
-
if (EventConstructor)
|
|
3486
|
-
this.emitter.dispatchEvent(new EventConstructor(detail));
|
|
3487
|
-
}
|
|
3488
|
-
commit(next, changeAction, emittedEvents, context) {
|
|
3489
|
-
const previousConversation = this.current;
|
|
3490
|
-
const newNode = {
|
|
3491
|
-
conversation: next,
|
|
3492
|
-
parent: this.currentNode,
|
|
3493
|
-
children: []
|
|
3494
|
-
};
|
|
3495
|
-
this.currentNode.children.push(newNode);
|
|
3496
|
-
this.currentNode = newNode;
|
|
3497
|
-
if (this.environment.maxHistoryDepth !== undefined)
|
|
3498
|
-
this.pruneToDepth(this.environment.maxHistoryDepth);
|
|
3499
|
-
this.emitConversationEvent("change", this.buildEventDetail(changeAction, previousConversation, context));
|
|
3500
|
-
for (const eventType of emittedEvents) {
|
|
3501
|
-
if (eventType === "change")
|
|
3502
|
-
continue;
|
|
3503
|
-
const eventAction = eventType;
|
|
3504
|
-
this.emitConversationEvent(eventType, this.buildEventDetail(eventAction, previousConversation, context));
|
|
3505
|
-
}
|
|
3506
|
-
}
|
|
3507
|
-
pruneToDepth(maxDepth) {
|
|
3508
|
-
let depth = 0;
|
|
3509
|
-
let node = this.currentNode;
|
|
3510
|
-
while (node) {
|
|
3511
|
-
depth++;
|
|
3512
|
-
node = node.parent;
|
|
3513
|
-
}
|
|
3514
|
-
while (depth > maxDepth) {
|
|
3515
|
-
let root = this.currentNode;
|
|
3516
|
-
while (root.parent)
|
|
3517
|
-
root = root.parent;
|
|
3518
|
-
const childOnPath = root.children.find((child) => {
|
|
3519
|
-
let curr = this.currentNode;
|
|
3520
|
-
while (curr) {
|
|
3521
|
-
if (curr === child)
|
|
3522
|
-
return true;
|
|
3523
|
-
curr = curr.parent;
|
|
3524
|
-
}
|
|
3525
|
-
return false;
|
|
3526
|
-
});
|
|
3527
|
-
if (!childOnPath)
|
|
3528
|
-
break;
|
|
3529
|
-
childOnPath.parent = null;
|
|
3530
|
-
depth--;
|
|
3531
|
-
}
|
|
3532
|
-
}
|
|
3533
|
-
addEventListener(type, callback, options) {
|
|
3534
|
-
const resolved = typeof options === "boolean" ? { capture: options } : { ...options };
|
|
3535
|
-
resolved.signal ??= this.emitter.signal;
|
|
3536
|
-
this.emitter.addEventListener(type, callback, resolved);
|
|
3537
|
-
}
|
|
3538
|
-
removeEventListener(type, callback, options) {
|
|
3539
|
-
this.emitter.removeEventListener(type, callback, options);
|
|
3540
|
-
}
|
|
3541
|
-
dispatchEvent(event) {
|
|
3542
|
-
return this.emitter.dispatchEvent(event);
|
|
3543
|
-
}
|
|
3544
|
-
watch(run) {
|
|
3545
|
-
run(this.current);
|
|
3546
|
-
const handler = (event) => {
|
|
3547
|
-
run(event.conversation);
|
|
3548
|
-
};
|
|
3549
|
-
this.emitter.addEventListener("change", handler, { signal: this.emitter.signal });
|
|
3550
|
-
return () => {
|
|
3551
|
-
this.emitter.removeEventListener("change", handler);
|
|
3552
|
-
};
|
|
3553
|
-
}
|
|
3554
|
-
on(type) {
|
|
3555
|
-
return this.emitter.on(type);
|
|
3556
|
-
}
|
|
3557
|
-
once(type, listener) {
|
|
3558
|
-
this.emitter.once(type, listener);
|
|
3559
|
-
}
|
|
3560
|
-
subscribe(type, observerOrNext, error, complete) {
|
|
3561
|
-
return this.emitter.subscribe(type, observerOrNext, error, complete);
|
|
3562
|
-
}
|
|
3563
|
-
toObservable() {
|
|
3564
|
-
return this.emitter.toObservable();
|
|
3565
|
-
}
|
|
3566
|
-
events(type, options) {
|
|
3567
|
-
return this.emitter.events(type, options);
|
|
3568
|
-
}
|
|
3569
|
-
complete() {
|
|
3570
|
-
this.emitter.complete();
|
|
3571
|
-
}
|
|
3572
|
-
get completed() {
|
|
3573
|
-
return this.emitter.completed;
|
|
3574
|
-
}
|
|
3575
|
-
getSnapshot() {
|
|
3576
|
-
return this.current;
|
|
3577
|
-
}
|
|
3578
|
-
get current() {
|
|
3579
|
-
return this.currentNode.conversation;
|
|
3580
|
-
}
|
|
3581
|
-
get ids() {
|
|
3582
|
-
return getMessageIds(this.current);
|
|
3583
|
-
}
|
|
3584
|
-
get canUndo() {
|
|
3585
|
-
return this.currentNode.parent !== null;
|
|
3586
|
-
}
|
|
3587
|
-
get canRedo() {
|
|
3588
|
-
return this.currentNode.children.length > 0;
|
|
3589
|
-
}
|
|
3590
|
-
get env() {
|
|
3591
|
-
return this.environment;
|
|
3592
|
-
}
|
|
3593
|
-
get branchCount() {
|
|
3594
|
-
return this.currentNode.parent ? this.currentNode.parent.children.length : 1;
|
|
3595
|
-
}
|
|
3596
|
-
get branchIndex() {
|
|
3597
|
-
if (!this.currentNode.parent)
|
|
3598
|
-
return 0;
|
|
3599
|
-
return this.currentNode.parent.children.indexOf(this.currentNode);
|
|
3600
|
-
}
|
|
3601
|
-
get redoCount() {
|
|
3602
|
-
return this.currentNode.children.length;
|
|
3603
|
-
}
|
|
3604
|
-
createChangeContext(previousConversation, nextConversation, action) {
|
|
3605
|
-
const diff = diffConversationMessages(previousConversation, nextConversation);
|
|
3606
|
-
const messageIds = action === "messages.appended" ? diff.appended : action === "messages.updated" ? diff.updated : diff.removed;
|
|
3607
|
-
const toolCallIds = collectToolCallIds(action === "messages.removed" ? previousConversation : nextConversation, messageIds);
|
|
3608
|
-
return {
|
|
3609
|
-
...messageIds.length > 0 ? { messageIds } : {},
|
|
3610
|
-
...toolCallIds ? { toolCallIds } : {}
|
|
3611
|
-
};
|
|
3612
|
-
}
|
|
3613
|
-
pushWithEvents(next, changeAction, context) {
|
|
3614
|
-
this.commit(next, changeAction, ["push", changeAction], context);
|
|
3615
|
-
}
|
|
3616
|
-
push(next) {
|
|
3617
|
-
this.commit(next, "push", ["push"]);
|
|
3618
|
-
}
|
|
3619
|
-
undo() {
|
|
3620
|
-
if (this.currentNode.parent) {
|
|
3621
|
-
const previousConversation = this.current;
|
|
3622
|
-
this.currentNode = this.currentNode.parent;
|
|
3623
|
-
this.emitConversationEvent("change", this.buildEventDetail("undo", previousConversation));
|
|
3624
|
-
this.emitConversationEvent("undo", this.buildEventDetail("undo", previousConversation));
|
|
3625
|
-
return this.current;
|
|
3626
|
-
}
|
|
3627
|
-
}
|
|
3628
|
-
redo(childIndex = 0) {
|
|
3629
|
-
const next = this.currentNode.children[childIndex];
|
|
3630
|
-
if (next) {
|
|
3631
|
-
const previousConversation = this.current;
|
|
3632
|
-
this.currentNode = next;
|
|
3633
|
-
this.emitConversationEvent("change", this.buildEventDetail("redo", previousConversation));
|
|
3634
|
-
this.emitConversationEvent("redo", this.buildEventDetail("redo", previousConversation));
|
|
3635
|
-
return this.current;
|
|
3636
|
-
}
|
|
3637
|
-
}
|
|
3638
|
-
switchToBranch(index) {
|
|
3639
|
-
if (this.currentNode.parent) {
|
|
3640
|
-
const target = this.currentNode.parent.children[index];
|
|
3641
|
-
if (target) {
|
|
3642
|
-
const previousConversation = this.current;
|
|
3643
|
-
this.currentNode = target;
|
|
3644
|
-
this.emitConversationEvent("change", this.buildEventDetail("switch", previousConversation));
|
|
3645
|
-
this.emitConversationEvent("switch", this.buildEventDetail("switch", previousConversation));
|
|
3646
|
-
return this.current;
|
|
3647
|
-
}
|
|
3648
|
-
}
|
|
3649
|
-
}
|
|
3650
|
-
fork(messageId) {
|
|
3651
|
-
const previous = this.current;
|
|
3652
|
-
const cloned = JSON.parse(JSON.stringify(this.current));
|
|
3653
|
-
let forkedHistory;
|
|
3654
|
-
if (messageId) {
|
|
3655
|
-
const messageIndex = cloned.ids.indexOf(messageId);
|
|
3656
|
-
if (messageIndex === -1)
|
|
3657
|
-
throw new Error(`Message with id "${messageId}" not found`);
|
|
3658
|
-
const truncatedIds = cloned.ids.slice(0, messageIndex + 1);
|
|
3659
|
-
const truncatedMessages = {};
|
|
3660
|
-
for (const id of truncatedIds) {
|
|
3661
|
-
const message = cloned.messages[id];
|
|
3662
|
-
if (message)
|
|
3663
|
-
truncatedMessages[id] = message;
|
|
3664
|
-
}
|
|
3665
|
-
forkedHistory = {
|
|
3666
|
-
...cloned,
|
|
3667
|
-
id: this.environment.randomId(),
|
|
3668
|
-
ids: truncatedIds,
|
|
3669
|
-
messages: truncatedMessages,
|
|
3670
|
-
updatedAt: this.environment.now()
|
|
3671
|
-
};
|
|
3672
|
-
} else
|
|
3673
|
-
forkedHistory = {
|
|
3674
|
-
...cloned,
|
|
3675
|
-
id: this.environment.randomId(),
|
|
3676
|
-
updatedAt: this.environment.now()
|
|
3677
|
-
};
|
|
3678
|
-
const detail = this.buildEventDetail("session.forked", previous);
|
|
3679
|
-
this.emitConversationEvent("session.forked", detail);
|
|
3680
|
-
this.emitConversationEvent("change", detail);
|
|
3681
|
-
return new Conversation2(forkedHistory, this.environment);
|
|
3682
|
-
}
|
|
3683
|
-
tag(label) {
|
|
3684
|
-
const previous = this.current;
|
|
3685
|
-
const existingTags = previous.metadata["_tags"] ?? [];
|
|
3686
|
-
if (existingTags.includes(label))
|
|
3687
|
-
return;
|
|
3688
|
-
const next = {
|
|
3689
|
-
...previous,
|
|
3690
|
-
metadata: {
|
|
3691
|
-
...previous.metadata,
|
|
3692
|
-
_tags: [...existingTags, label]
|
|
3693
|
-
},
|
|
3694
|
-
updatedAt: this.environment.now()
|
|
3695
|
-
};
|
|
3696
|
-
this.commit(next, "session.tagged", ["push", "session.tagged"]);
|
|
3697
|
-
}
|
|
3698
|
-
rename(title) {
|
|
3699
|
-
const previous = this.current;
|
|
3700
|
-
if (previous.title === title)
|
|
3701
|
-
return;
|
|
3702
|
-
const next = {
|
|
3703
|
-
...previous,
|
|
3704
|
-
title,
|
|
3705
|
-
updatedAt: this.environment.now()
|
|
3706
|
-
};
|
|
3707
|
-
this.commit(next, "session.renamed", ["push", "session.renamed"]);
|
|
3708
|
-
}
|
|
3709
|
-
getPath() {
|
|
3710
|
-
const path = [];
|
|
3711
|
-
let curr = this.currentNode;
|
|
3712
|
-
while (curr) {
|
|
3713
|
-
path.unshift(curr.conversation);
|
|
3714
|
-
curr = curr.parent;
|
|
3715
|
-
}
|
|
3716
|
-
return path;
|
|
3717
|
-
}
|
|
3718
|
-
getMessages(options) {
|
|
3719
|
-
return getMessages(this.current, options);
|
|
3720
|
-
}
|
|
3721
|
-
getMessageAtPosition(position) {
|
|
3722
|
-
return getMessageAtPosition(this.current, position);
|
|
3723
|
-
}
|
|
3724
|
-
getMessageIds() {
|
|
3725
|
-
return getMessageIds(this.current);
|
|
3726
|
-
}
|
|
3727
|
-
getMessageById(id) {
|
|
3728
|
-
return getMessageById(this.current, id);
|
|
3729
|
-
}
|
|
3730
|
-
get(id) {
|
|
3731
|
-
return getMessageById(this.current, id);
|
|
3732
|
-
}
|
|
3733
|
-
searchMessages(predicate) {
|
|
3734
|
-
return searchConversationMessages(this.current, predicate);
|
|
3735
|
-
}
|
|
3736
|
-
getStatistics() {
|
|
3737
|
-
return getStatistics(this.current);
|
|
3738
|
-
}
|
|
3739
|
-
hasSystemMessage() {
|
|
3740
|
-
return hasSystemMessage(this.current);
|
|
3741
|
-
}
|
|
3742
|
-
getFirstSystemMessage() {
|
|
3743
|
-
return getFirstSystemMessage(this.current);
|
|
3744
|
-
}
|
|
3745
|
-
getSystemMessages() {
|
|
3746
|
-
return getSystemMessages(this.current);
|
|
3747
|
-
}
|
|
3748
|
-
toChatMessages() {
|
|
3749
|
-
return toChatMessages(this.current);
|
|
3750
|
-
}
|
|
3751
|
-
estimateTokens(estimator) {
|
|
3752
|
-
return estimateConversationTokens(this.current, estimator, this.env);
|
|
3753
|
-
}
|
|
3754
|
-
getRecentMessages(count, options) {
|
|
3755
|
-
return getRecentMessages(this.current, count, options);
|
|
3756
|
-
}
|
|
3757
|
-
getStreamingMessage() {
|
|
3758
|
-
return getStreamingMessage(this.current);
|
|
3759
|
-
}
|
|
3760
|
-
appendMessages(...inputs) {
|
|
3761
|
-
const previousConversation = this.current;
|
|
3762
|
-
const nextConversation = appendMessages(this.current, ...inputs, this.env);
|
|
3763
|
-
this.pushWithEvents(nextConversation, "messages.appended", this.createChangeContext(previousConversation, nextConversation, "messages.appended"));
|
|
3764
|
-
}
|
|
3765
|
-
appendUserMessage(content, metadata) {
|
|
3766
|
-
const previousConversation = this.current;
|
|
3767
|
-
const nextConversation = appendUserMessage(this.current, content, metadata, this.env);
|
|
3768
|
-
this.pushWithEvents(nextConversation, "messages.appended", this.createChangeContext(previousConversation, nextConversation, "messages.appended"));
|
|
3769
|
-
}
|
|
3770
|
-
appendAssistantMessage(content, metadata) {
|
|
3771
|
-
const previousConversation = this.current;
|
|
3772
|
-
const nextConversation = appendAssistantMessage(this.current, content, metadata, this.env);
|
|
3773
|
-
this.pushWithEvents(nextConversation, "messages.appended", this.createChangeContext(previousConversation, nextConversation, "messages.appended"));
|
|
3774
|
-
}
|
|
3775
|
-
appendSystemMessage(content, metadata) {
|
|
3776
|
-
const previousConversation = this.current;
|
|
3777
|
-
const nextConversation = appendSystemMessage(this.current, content, metadata, this.env);
|
|
3778
|
-
this.pushWithEvents(nextConversation, "messages.appended", this.createChangeContext(previousConversation, nextConversation, "messages.appended"));
|
|
3779
|
-
}
|
|
3780
|
-
prependSystemMessage(content, metadata) {
|
|
3781
|
-
const previousConversation = this.current;
|
|
3782
|
-
const nextConversation = prependSystemMessage(this.current, content, metadata, this.env);
|
|
3783
|
-
this.pushWithEvents(nextConversation, "messages.appended", this.createChangeContext(previousConversation, nextConversation, "messages.appended"));
|
|
3784
|
-
}
|
|
3785
|
-
replaceSystemMessage(content, metadata) {
|
|
3786
|
-
const previousConversation = this.current;
|
|
3787
|
-
const nextConversation = replaceSystemMessage(this.current, content, metadata, this.env);
|
|
3788
|
-
this.pushWithEvents(nextConversation, "messages.updated", this.createChangeContext(previousConversation, nextConversation, "messages.updated"));
|
|
3789
|
-
}
|
|
3790
|
-
collapseSystemMessages() {
|
|
3791
|
-
const previousConversation = this.current;
|
|
3792
|
-
const nextConversation = collapseSystemMessages(this.current, this.env);
|
|
3793
|
-
const action = previousConversation.ids.length === nextConversation.ids.length ? "messages.updated" : "messages.removed";
|
|
3794
|
-
this.pushWithEvents(nextConversation, action, this.createChangeContext(previousConversation, nextConversation, action));
|
|
3795
|
-
}
|
|
3796
|
-
redactMessageAtPosition(position, placeholderOrOptions) {
|
|
3797
|
-
const previousConversation = this.current;
|
|
3798
|
-
const nextConversation = redactMessageAtPosition(this.current, position, placeholderOrOptions, this.env);
|
|
3799
|
-
this.pushWithEvents(nextConversation, "messages.updated", this.createChangeContext(previousConversation, nextConversation, "messages.updated"));
|
|
3800
|
-
}
|
|
3801
|
-
truncateFromPosition(position, options) {
|
|
3802
|
-
const previousConversation = this.current;
|
|
3803
|
-
const nextConversation = truncateFromPosition(this.current, position, options, this.env);
|
|
3804
|
-
this.pushWithEvents(nextConversation, "messages.removed", this.createChangeContext(previousConversation, nextConversation, "messages.removed"));
|
|
3805
|
-
}
|
|
3806
|
-
truncateToTokenLimit(maxTokens, options) {
|
|
3807
|
-
const previousConversation = this.current;
|
|
3808
|
-
const nextConversation = truncateToTokenLimit(this.current, maxTokens, options, this.env);
|
|
3809
|
-
this.pushWithEvents(nextConversation, "messages.removed", this.createChangeContext(previousConversation, nextConversation, "messages.removed"));
|
|
3810
|
-
}
|
|
3811
|
-
async compact(summarizer, options) {
|
|
3812
|
-
const previous = this.current;
|
|
3813
|
-
this.emitConversationEvent("compaction.started", this.buildEventDetail("compaction.started", previous));
|
|
3814
|
-
const { conversation, result } = await compactConversation(this.current, summarizer, options, this.env);
|
|
3815
|
-
if (result.compacted)
|
|
3816
|
-
this.pushWithEvents(conversation, "compaction.completed", this.createChangeContext(previous, conversation, "messages.removed"));
|
|
3817
|
-
else
|
|
3818
|
-
this.emitConversationEvent("compaction.completed", this.buildEventDetail("compaction.completed", previous));
|
|
3819
|
-
return result;
|
|
3820
|
-
}
|
|
3821
|
-
appendStreamingMessage(role, metadata) {
|
|
3822
|
-
const { conversation, messageId } = appendStreamingMessage(this.current, role, metadata, this.env);
|
|
3823
|
-
this.commit(conversation, "stream.started", [
|
|
3824
|
-
"push",
|
|
3825
|
-
"messages.appended",
|
|
3826
|
-
"stream.started"
|
|
3827
|
-
], { messageIds: [messageId] });
|
|
3828
|
-
return messageId;
|
|
3829
|
-
}
|
|
3830
|
-
updateStreamingMessage(messageId, content) {
|
|
3831
|
-
const nextConversation = updateStreamingMessage(this.current, messageId, content, this.env);
|
|
3832
|
-
this.commit(nextConversation, "stream.updated", [
|
|
3833
|
-
"push",
|
|
3834
|
-
"messages.updated",
|
|
3835
|
-
"stream.updated"
|
|
3836
|
-
], { messageIds: [messageId] });
|
|
3837
|
-
}
|
|
3838
|
-
finalizeStreamingMessage(messageId, options) {
|
|
3839
|
-
const nextConversation = finalizeStreamingMessage(this.current, messageId, options, this.env);
|
|
3840
|
-
this.commit(nextConversation, "stream.finalized", [
|
|
3841
|
-
"push",
|
|
3842
|
-
"messages.updated",
|
|
3843
|
-
"stream.finalized"
|
|
3844
|
-
], { messageIds: [messageId] });
|
|
3845
|
-
}
|
|
3846
|
-
cancelStreamingMessage(messageId) {
|
|
3847
|
-
const nextConversation = cancelStreamingMessage(this.current, messageId, this.env);
|
|
3848
|
-
this.commit(nextConversation, "stream.cancelled", [
|
|
3849
|
-
"push",
|
|
3850
|
-
"messages.removed",
|
|
3851
|
-
"stream.cancelled"
|
|
3852
|
-
], { messageIds: [messageId] });
|
|
3853
|
-
}
|
|
3854
|
-
appendToolCall(toolCall, options) {
|
|
3855
|
-
const nextConversation = appendToolCall(this.current, toolCall, options, this.env);
|
|
3856
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3857
|
-
this.commit(nextConversation, "tool-calls.appended", [
|
|
3858
|
-
"push",
|
|
3859
|
-
"messages.appended",
|
|
3860
|
-
"tool-calls.appended"
|
|
3861
|
-
], context);
|
|
3862
|
-
}
|
|
3863
|
-
appendToolCalls(toolCalls) {
|
|
3864
|
-
const nextConversation = appendToolCalls(this.current, toolCalls, this.env);
|
|
3865
|
-
if (nextConversation === this.current)
|
|
3866
|
-
return;
|
|
3867
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3868
|
-
this.commit(nextConversation, "tool-calls.appended", [
|
|
3869
|
-
"push",
|
|
3870
|
-
"messages.appended",
|
|
3871
|
-
"tool-calls.appended"
|
|
3872
|
-
], context);
|
|
3873
|
-
}
|
|
3874
|
-
appendToolResult(toolResult, options) {
|
|
3875
|
-
const nextConversation = appendToolResult(this.current, toolResult, options, this.env);
|
|
3876
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3877
|
-
this.commit(nextConversation, "tool-results.appended", [
|
|
3878
|
-
"push",
|
|
3879
|
-
"messages.appended",
|
|
3880
|
-
"tool-results.appended"
|
|
3881
|
-
], context);
|
|
3882
|
-
}
|
|
3883
|
-
appendToolResults(toolResults) {
|
|
3884
|
-
const nextConversation = appendToolResults(this.current, toolResults, this.env);
|
|
3885
|
-
if (nextConversation === this.current)
|
|
3886
|
-
return;
|
|
3887
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3888
|
-
this.commit(nextConversation, "tool-results.appended", [
|
|
3889
|
-
"push",
|
|
3890
|
-
"messages.appended",
|
|
3891
|
-
"tool-results.appended"
|
|
3892
|
-
], context);
|
|
3893
|
-
}
|
|
3894
|
-
async appendToolResultAsync(toolResult, options) {
|
|
3895
|
-
const nextConversation = await appendToolResultAsync(this.current, toolResult, options, this.env);
|
|
3896
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3897
|
-
this.commit(nextConversation, "tool-results.appended", [
|
|
3898
|
-
"push",
|
|
3899
|
-
"messages.appended",
|
|
3900
|
-
"tool-results.appended"
|
|
3901
|
-
], context);
|
|
3902
|
-
}
|
|
3903
|
-
async appendToolResultsAsync(toolResults) {
|
|
3904
|
-
const nextConversation = await appendToolResultsAsync(this.current, toolResults, this.env);
|
|
3905
|
-
if (nextConversation === this.current)
|
|
3906
|
-
return;
|
|
3907
|
-
const context = this.createChangeContext(this.current, nextConversation, "messages.appended");
|
|
3908
|
-
this.commit(nextConversation, "tool-results.appended", [
|
|
3909
|
-
"push",
|
|
3910
|
-
"messages.appended",
|
|
3911
|
-
"tool-results.appended"
|
|
3912
|
-
], context);
|
|
3913
|
-
}
|
|
3914
|
-
getPendingToolCalls() {
|
|
3915
|
-
return getPendingToolCalls(this.current);
|
|
3916
|
-
}
|
|
3917
|
-
getToolInteractions() {
|
|
3918
|
-
return getToolInteractions(this.current);
|
|
3919
|
-
}
|
|
3920
|
-
static async fromProvider(provider, payload, environment) {
|
|
3921
|
-
return new Conversation2((await loadConversationAdapter(provider)).import(payload), environment);
|
|
3922
|
-
}
|
|
3923
|
-
async toProvider(provider, options) {
|
|
3924
|
-
return (await loadConversationAdapter(provider)).export(this.current, options);
|
|
3925
|
-
}
|
|
3926
|
-
async appendProvider(provider, payload) {
|
|
3927
|
-
const nextConversation = (await loadConversationAdapter(provider)).append(this.current, payload);
|
|
3928
|
-
if (nextConversation === this.current)
|
|
3929
|
-
return;
|
|
3930
|
-
const diff = diffConversationMessages(this.current, nextConversation);
|
|
3931
|
-
const appendedIds = diff.appended;
|
|
3932
|
-
const updatedIds = diff.updated;
|
|
3933
|
-
const removedIds = diff.removed;
|
|
3934
|
-
const action = removedIds.length > 0 ? "messages.removed" : updatedIds.length > 0 ? "messages.updated" : "messages.appended";
|
|
3935
|
-
const messageIds = action === "messages.removed" ? removedIds : action === "messages.updated" ? updatedIds : appendedIds;
|
|
3936
|
-
const toolCallIds = collectToolCallIds(nextConversation, messageIds);
|
|
3937
|
-
this.commit(nextConversation, action, ["push", action], {
|
|
3938
|
-
...messageIds.length > 0 ? { messageIds } : {},
|
|
3939
|
-
...toolCallIds ? { toolCallIds } : {}
|
|
3940
|
-
});
|
|
3941
|
-
}
|
|
3942
|
-
snapshot() {
|
|
3943
|
-
const getPath = (node) => {
|
|
3944
|
-
const path = [];
|
|
3945
|
-
let curr = node;
|
|
3946
|
-
while (curr.parent) {
|
|
3947
|
-
path.unshift(curr.parent.children.indexOf(curr));
|
|
3948
|
-
curr = curr.parent;
|
|
3949
|
-
}
|
|
3950
|
-
return path;
|
|
3951
|
-
};
|
|
3952
|
-
const serializeNode = (node) => ({
|
|
3953
|
-
conversation: node.conversation,
|
|
3954
|
-
children: node.children.map(serializeNode)
|
|
3955
|
-
});
|
|
3956
|
-
let root = this.currentNode;
|
|
3957
|
-
while (root.parent)
|
|
3958
|
-
root = root.parent;
|
|
3959
|
-
return {
|
|
3960
|
-
root: serializeNode(root),
|
|
3961
|
-
currentPath: getPath(this.currentNode)
|
|
3962
|
-
};
|
|
3963
|
-
}
|
|
3964
|
-
static from(json, environment) {
|
|
3965
|
-
const conversation = new Conversation2(deserializeConversationHistory(json.root.conversation), environment);
|
|
3966
|
-
const buildTree = (nodeJSON, parentNode) => {
|
|
3967
|
-
const node = {
|
|
3968
|
-
conversation: deserializeConversationHistory(nodeJSON.conversation),
|
|
3969
|
-
parent: parentNode,
|
|
3970
|
-
children: []
|
|
3971
|
-
};
|
|
3972
|
-
node.children = nodeJSON.children.map((child) => buildTree(child, node));
|
|
3973
|
-
return node;
|
|
3974
|
-
};
|
|
3975
|
-
const rootNode = conversation.currentNode;
|
|
3976
|
-
rootNode.children = json.root.children.map((child) => buildTree(child, rootNode));
|
|
3977
|
-
let current = rootNode;
|
|
3978
|
-
for (const index of json.currentPath) {
|
|
3979
|
-
const target = current.children[index];
|
|
3980
|
-
if (target)
|
|
3981
|
-
current = target;
|
|
3982
|
-
}
|
|
3983
|
-
conversation.currentNode = current;
|
|
3984
|
-
return conversation;
|
|
3985
|
-
}
|
|
3986
|
-
static async fromOpenAIMessages(messages, environment) {
|
|
3987
|
-
return Conversation2.fromProvider("openai", [...messages], environment);
|
|
3988
|
-
}
|
|
3989
|
-
static async fromAnthropicMessages(payload, environment) {
|
|
3990
|
-
return Conversation2.fromProvider("anthropic", payload, environment);
|
|
3991
|
-
}
|
|
3992
|
-
static async fromGeminiMessages(payload, environment) {
|
|
3993
|
-
return Conversation2.fromProvider("gemini", payload, environment);
|
|
3994
|
-
}
|
|
3995
|
-
async toOpenAIMessages() {
|
|
3996
|
-
return this.toProvider("openai", { groupToolCalls: false });
|
|
3997
|
-
}
|
|
3998
|
-
async toOpenAIMessagesGrouped() {
|
|
3999
|
-
return this.toProvider("openai", { groupToolCalls: true });
|
|
4000
|
-
}
|
|
4001
|
-
async toAnthropicMessages() {
|
|
4002
|
-
return this.toProvider("anthropic");
|
|
4003
|
-
}
|
|
4004
|
-
async toGeminiMessages() {
|
|
4005
|
-
return this.toProvider("gemini");
|
|
4006
|
-
}
|
|
4007
|
-
bind(fn) {
|
|
4008
|
-
return (...args) => {
|
|
4009
|
-
const result = fn(this.current, ...args, this.env);
|
|
4010
|
-
if (isConversationHistory(result))
|
|
4011
|
-
this.push(result);
|
|
4012
|
-
return result;
|
|
4013
|
-
};
|
|
4014
|
-
}
|
|
4015
|
-
[Symbol.dispose]() {
|
|
4016
|
-
this.complete();
|
|
4017
|
-
let root = this.currentNode;
|
|
4018
|
-
while (root?.parent)
|
|
4019
|
-
root = root.parent;
|
|
4020
|
-
const clearNode = (node) => {
|
|
4021
|
-
for (const child of node.children)
|
|
4022
|
-
clearNode(child);
|
|
4023
|
-
node.children = [];
|
|
4024
|
-
const n = node;
|
|
4025
|
-
n.parent = null;
|
|
4026
|
-
n.conversation = null;
|
|
4027
|
-
};
|
|
4028
|
-
if (root)
|
|
4029
|
-
clearNode(root);
|
|
4030
|
-
}
|
|
4031
|
-
};
|
|
4032
|
-
function isConversationHistory(value) {
|
|
4033
|
-
return value !== null && typeof value === "object" && typeof value.schemaVersion === "number" && typeof value.id === "string" && typeof value.status === "string" && value.metadata !== null && typeof value.metadata === "object" && Array.isArray(value.ids) && typeof value.messages === "object" && value.messages !== null && !Array.isArray(value.messages) && typeof value.createdAt === "string" && typeof value.updatedAt === "string";
|
|
61
|
+
// src/utilities/class-names.ts
|
|
62
|
+
function classNames(...parts) {
|
|
63
|
+
return parts.filter((part) => typeof part === "string" && part.length > 0).join(" ");
|
|
4034
64
|
}
|
|
4035
65
|
|
|
4036
|
-
// ../../node_modules/conversationalist/dist/utilities/index.js
|
|
4037
|
-
init_append_l_g0hmrj();
|
|
4038
|
-
init_message_BEjqY7Jv();
|
|
4039
|
-
init_transient_Bk0RaCFf();
|
|
4040
|
-
|
|
4041
66
|
// src/components/chat/utilities/conversation.ts
|
|
67
|
+
import { getMessages } from "conversationalist";
|
|
68
|
+
import { pairToolCallsWithResults as pairConversationalistToolCallsWithResults } from "conversationalist/utilities";
|
|
4042
69
|
function hasPairableToolField(message) {
|
|
4043
70
|
return message.role === "tool-call" && message.toolCall !== undefined || message.role === "tool-result" && message.toolResult !== undefined;
|
|
4044
71
|
}
|
|
4045
|
-
function
|
|
4046
|
-
return
|
|
72
|
+
function pairToolCallsWithResults(messages) {
|
|
73
|
+
return pairConversationalistToolCallsWithResults(messages.filter(hasPairableToolField));
|
|
4047
74
|
}
|
|
4048
75
|
// src/components/chat/utilities/utilities.ts
|
|
4049
76
|
var CINDER_REASONING_METADATA_KEY = "cinder:reasoning";
|
|
@@ -4095,7 +122,7 @@ function resolveMessageSuggestions(message, fromProp) {
|
|
|
4095
122
|
const suggestions = candidate.filter((entry) => typeof entry === "string");
|
|
4096
123
|
return suggestions.length > 0 ? suggestions : undefined;
|
|
4097
124
|
}
|
|
4098
|
-
function
|
|
125
|
+
function toMultiModalArray(input) {
|
|
4099
126
|
if (typeof input === "string")
|
|
4100
127
|
return [{ type: "text", text: input }];
|
|
4101
128
|
return "type" in input ? [input] : input;
|
|
@@ -4126,7 +153,7 @@ function getServerContentText(part) {
|
|
|
4126
153
|
}
|
|
4127
154
|
}
|
|
4128
155
|
function getMessageParts(message) {
|
|
4129
|
-
return
|
|
156
|
+
return toMultiModalArray(message.content);
|
|
4130
157
|
}
|
|
4131
158
|
function getMessageText(message) {
|
|
4132
159
|
return getMessageParts(message).map(getServerContentText).filter((text) => typeof text === "string" && text.length > 0).join(`
|
|
@@ -4493,4 +520,4 @@ export {
|
|
|
4493
520
|
Chat_conversation_list as ChatConversationList
|
|
4494
521
|
};
|
|
4495
522
|
|
|
4496
|
-
//# debugId=
|
|
523
|
+
//# debugId=FA6DE2C72C326CFB64756E2164756E21
|