@molecule/app-ide-panel-logic 1.3.1 → 1.4.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.
Files changed (66) hide show
  1. package/README.md +277 -1
  2. package/dist/chatTimelineOrdering.d.ts +175 -1
  3. package/dist/chatTimelineOrdering.d.ts.map +1 -1
  4. package/dist/chatTimelineOrdering.js +192 -1
  5. package/dist/chatTimelineOrdering.js.map +1 -1
  6. package/dist/components/chat-card-event-utilities.d.ts +320 -0
  7. package/dist/components/chat-card-event-utilities.d.ts.map +1 -0
  8. package/dist/components/chat-card-event-utilities.js +198 -0
  9. package/dist/components/chat-card-event-utilities.js.map +1 -0
  10. package/dist/components/chat-composer-utilities.d.ts +472 -0
  11. package/dist/components/chat-composer-utilities.d.ts.map +1 -0
  12. package/dist/components/chat-composer-utilities.js +519 -0
  13. package/dist/components/chat-composer-utilities.js.map +1 -0
  14. package/dist/components/chat-dictation-controller.d.ts +376 -0
  15. package/dist/components/chat-dictation-controller.d.ts.map +1 -0
  16. package/dist/components/chat-dictation-controller.js +559 -0
  17. package/dist/components/chat-dictation-controller.js.map +1 -0
  18. package/dist/components/chat-effort-utilities.d.ts +24 -0
  19. package/dist/components/chat-effort-utilities.d.ts.map +1 -1
  20. package/dist/components/chat-effort-utilities.js +41 -0
  21. package/dist/components/chat-effort-utilities.js.map +1 -1
  22. package/dist/components/chat-format-utilities.d.ts +71 -0
  23. package/dist/components/chat-format-utilities.d.ts.map +1 -0
  24. package/dist/components/chat-format-utilities.js +192 -0
  25. package/dist/components/chat-format-utilities.js.map +1 -0
  26. package/dist/components/chat-models-utilities.d.ts +168 -0
  27. package/dist/components/chat-models-utilities.d.ts.map +1 -1
  28. package/dist/components/chat-models-utilities.js +253 -0
  29. package/dist/components/chat-models-utilities.js.map +1 -1
  30. package/dist/components/chat-settings-utilities.d.ts +153 -1
  31. package/dist/components/chat-settings-utilities.d.ts.map +1 -1
  32. package/dist/components/chat-settings-utilities.js +204 -1
  33. package/dist/components/chat-settings-utilities.js.map +1 -1
  34. package/dist/components/chat-stream-utilities.d.ts +165 -1
  35. package/dist/components/chat-stream-utilities.d.ts.map +1 -1
  36. package/dist/components/chat-stream-utilities.js +155 -1
  37. package/dist/components/chat-stream-utilities.js.map +1 -1
  38. package/dist/components/chat-tests-controller.d.ts +249 -0
  39. package/dist/components/chat-tests-controller.d.ts.map +1 -0
  40. package/dist/components/chat-tests-controller.js +504 -0
  41. package/dist/components/chat-tests-controller.js.map +1 -0
  42. package/dist/components/chat-timeline-utilities.d.ts +90 -0
  43. package/dist/components/chat-timeline-utilities.d.ts.map +1 -0
  44. package/dist/components/chat-timeline-utilities.js +71 -0
  45. package/dist/components/chat-timeline-utilities.js.map +1 -0
  46. package/dist/components/chat-ui-constants.d.ts +98 -0
  47. package/dist/components/chat-ui-constants.d.ts.map +1 -0
  48. package/dist/components/chat-ui-constants.js +146 -0
  49. package/dist/components/chat-ui-constants.js.map +1 -0
  50. package/dist/components/chat-undo-utilities.d.ts +47 -0
  51. package/dist/components/chat-undo-utilities.d.ts.map +1 -0
  52. package/dist/components/chat-undo-utilities.js +73 -0
  53. package/dist/components/chat-undo-utilities.js.map +1 -0
  54. package/dist/components/context-usage-utilities.d.ts +25 -3
  55. package/dist/components/context-usage-utilities.d.ts.map +1 -1
  56. package/dist/components/context-usage-utilities.js +42 -2
  57. package/dist/components/context-usage-utilities.js.map +1 -1
  58. package/dist/index.d.ts +2 -0
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +2 -0
  61. package/dist/index.js.map +1 -1
  62. package/dist/panels.d.ts +6 -0
  63. package/dist/panels.d.ts.map +1 -1
  64. package/dist/panels.js +6 -0
  65. package/dist/panels.js.map +1 -1
  66. package/package.json +2 -2
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The pure derivations behind the chat timeline's card list: which recorded
3
+ * limit card the live banner already states, which local commit cards a history
4
+ * reload has made redundant, and the context handed to the host's
5
+ * `buildUpgradeCta` for a refusal.
6
+ *
7
+ * Every framework's chat panel builds the same card list from the same inputs,
8
+ * so these rules live here, framework-neutral. Nothing in this module reads
9
+ * React state, refs, props or hooks: the caller passes every input in.
10
+ *
11
+ * @module
12
+ */
13
+ /**
14
+ * Whether a recorded card states the SAME limit the live error banner is
15
+ * already stating, so rendering it too would state one fact twice. The banner
16
+ * is never the one suppressed — it is the refused send's only feedback.
17
+ *
18
+ * Only the plain card qualifies: a `/help` or skills notice has its own body,
19
+ * so it is never the redundant copy of the banner.
20
+ *
21
+ * @param card - The recorded card.
22
+ * @param liveLimitType - The limit rule the live error carries, if any.
23
+ * @returns True when the card is redundant while that error is showing.
24
+ */
25
+ export function cardCoveredByLimit(card, liveLimitType) {
26
+ return !!liveLimitType && card.variant === undefined && card.coversLimitType === liveLimitType;
27
+ }
28
+ /**
29
+ * The commit hashes the persisted transcript already carries as `commitRecord`
30
+ * messages — a history reload pulls a committed record into `messages`, and the
31
+ * local card for the same commit is then a duplicate.
32
+ *
33
+ * @param messages - The visible transcript.
34
+ * @returns The set of persisted commit hashes.
35
+ */
36
+ export function persistedCommitHashes(messages) {
37
+ const hashes = new Set();
38
+ for (const message of messages) {
39
+ const hash = message.commitRecord?.hash;
40
+ if (hash)
41
+ hashes.add(hash);
42
+ }
43
+ return hashes;
44
+ }
45
+ /**
46
+ * Whether a local commit card still renders: it has no hash yet (the commit is
47
+ * in flight) or its hash is not in the persisted transcript.
48
+ *
49
+ * @param card - The local commit card.
50
+ * @param persisted - The persisted commit hashes.
51
+ * @returns True when the card is not a duplicate.
52
+ */
53
+ export function commitCardVisible(card, persisted) {
54
+ return !card.hash || !persisted.has(card.hash);
55
+ }
56
+ /**
57
+ * The exact context the panel forwards to the host's `buildUpgradeCta` for a
58
+ * refusal: every field, present or undefined.
59
+ *
60
+ * @param meta - The error's limit metadata, or null.
61
+ * @returns The context React forwards (every field, present or undefined).
62
+ */
63
+ export function upgradeContextFromError(meta) {
64
+ return {
65
+ requiresSignup: meta?.requiresSignup,
66
+ limitType: meta?.limitType,
67
+ billingAction: meta?.billingAction,
68
+ upgradeTier: meta?.upgradeTier,
69
+ };
70
+ }
71
+ //# sourceMappingURL=chat-timeline-utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-timeline-utilities.js","sourceRoot":"","sources":["../../src/components/chat-timeline-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAgBH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAuB,EACvB,aAAiC;IAEjC,OAAO,CAAC,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,eAAe,KAAK,aAAa,CAAA;AAChG,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAgC;IACpE,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAA;IAChC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,MAAM,IAAI,GAAG,OAAO,CAAC,YAAY,EAAE,IAAI,CAAA;QACvC,IAAI,IAAI;YAAE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC5B,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAA8B,EAC9B,SAA8B;IAE9B,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAChD,CAAC;AA8BD;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAA0B;IAChE,OAAO;QACL,cAAc,EAAE,IAAI,EAAE,cAAc;QACpC,SAAS,EAAE,IAAI,EAAE,SAAS;QAC1B,aAAa,EAAE,IAAI,EAAE,aAAa;QAClC,WAAW,EAAE,IAAI,EAAE,WAAW;KAC/B,CAAA;AACH,CAAC"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * UI constants of the IDE chat panel: the inline card styles, the injected
3
+ * user-message accent stylesheet, attachment limits, and the timeline spacing
4
+ * and notice-tone tables. Shared verbatim by every framework's chat panel
5
+ * (React's `ChatPanel` re-exports them through `@molecule/app-ide-react`).
6
+ *
7
+ * @module
8
+ */
9
+ import type { IconName } from '@molecule/app-icons';
10
+ import type { ChatEventCardAction } from '../customEventCards.js';
11
+ /** Subtle inline monospace code style for command/identifier spans in chat cards. */
12
+ export declare const CHAT_CARD_CODE_STYLE: ChatCardCodeStyle;
13
+ /** Inline monospace code style for command/identifier spans in chat cards. */
14
+ export interface ChatCardCodeStyle {
15
+ /** Monospace font stack. */
16
+ fontFamily: string;
17
+ /** Slightly smaller than the card body text. */
18
+ fontSize: string;
19
+ /** Tight padding around the code span. */
20
+ padding: string;
21
+ /** Corner radius in pixels. */
22
+ borderRadius: number;
23
+ /** Subtle gray chip background. */
24
+ background: string;
25
+ /** The 1px chip frame. */
26
+ border: string;
27
+ /** Commands never wrap inside a chat card line. */
28
+ whiteSpace: string;
29
+ }
30
+ /** Inline link style for action segments in chat cards (theme primary, underlined). */
31
+ export declare const CHAT_CARD_LINK_STYLE: ChatCardLinkStyle;
32
+ /** Inline link style for action segments in chat cards. */
33
+ export interface ChatCardLinkStyle {
34
+ /** The theme primary, so a link reads clickable in any theme. */
35
+ color: string;
36
+ /** Underline — the affordance that says "clickable". */
37
+ textDecoration: string;
38
+ /** Pointer cursor. */
39
+ cursor: string;
40
+ }
41
+ /**
42
+ * User-message accent stripe. The left edge of a real user message draws a vertical,
43
+ * multi-tone BLUE gradient swept gently up and down (the same smooth single-gradient
44
+ * technique as the composer ring, but vertical + blue) — NOT a repeating barber-pole,
45
+ * which read busy + janky. The host supplies the curated blues via
46
+ * `--mol-chat-accent-gradient`; the fallback is a lightened primary so it stays blue +
47
+ * never dark in any theme (the theme's primary-light/-dark tokens are actually a medium
48
+ * + a dark blue with no light tone). It's a full-box gradient (so the sweep has room to
49
+ * travel) clipped to a 3px LEFT band that follows the row's rounded corners. A
50
+ * `::before` + its keyframe can't be expressed inline (and inline `animation` can't be
51
+ * media-queried), so — like AutoCommitBadge — it is injected once and gated on the
52
+ * existing `data-mol-id`; the auto-sent row keeps its solid success accent.
53
+ */
54
+ export declare const USER_ACCENT_STYLE = "\n[data-mol-id=\"chat-user-message\"] { position: relative; }\n[data-mol-id=\"chat-user-message\"]::before {\n content: '';\n position: absolute;\n inset: 0;\n border-radius: inherit;\n background: var(--mol-chat-accent-gradient, linear-gradient(to bottom, color-mix(in srgb, var(--mol-color-primary, #3060c0) 85%, #fff), color-mix(in srgb, var(--mol-color-primary, #3060c0) 50%, #fff) 50%, color-mix(in srgb, var(--mol-color-primary, #3060c0) 85%, #fff)));\n background-size: 100% 300%;\n animation: mol-chat-accent-flow 6s ease-in-out infinite;\n -webkit-mask: linear-gradient(to right, #000 3px, transparent 3px);\n mask: linear-gradient(to right, #000 3px, transparent 3px);\n pointer-events: none;\n}\n@keyframes mol-chat-accent-flow {\n 0%, 100% { background-position: 50% 0%; }\n 50% { background-position: 50% 100%; }\n}\n@media (prefers-reduced-motion: reduce) {\n [data-mol-id=\"chat-user-message\"]::before { animation: none; }\n}";
55
+ /** Maximum file size for attachments (20 MB). */
56
+ export declare const MAX_ATTACHMENT_SIZE: number;
57
+ /** MIME types accepted by the file input for AI provider attachments. */
58
+ export declare const ACCEPTED_FILE_TYPES = "image/jpeg,image/png,image/gif,image/webp,image/svg+xml,application/pdf,audio/mpeg,audio/wav,audio/ogg,audio/flac,audio/webm,video/mp4,video/webm";
59
+ /** Height threshold (px) above which user messages get collapsed. */
60
+ export declare const USER_MSG_COLLAPSE_HEIGHT = 150;
61
+ /**
62
+ * Distance from the top (px) at which scrolling up pages in older timeline
63
+ * items. Same idea as the 80px bottom threshold of the stick-to-bottom
64
+ * detector: a margin, so the trigger fires while the user is still moving,
65
+ * not only pinned exactly at scrollTop 0.
66
+ */
67
+ export declare const LOAD_EARLIER_THRESHOLD_PX = 120;
68
+ /**
69
+ * The version this IDE reports — read by the `/version` command output AND the
70
+ * `/version` entry's description in the slash-command menu, so the menu shows
71
+ * the real number, not a stale literal.
72
+ */
73
+ export declare const APP_VERSION = "0.1.0";
74
+ /** Standard bottom margin (px) every chat-timeline item owns. 8px-grid rhythm unit. */
75
+ export declare const TIMELINE_ITEM_GAP = 16;
76
+ /** Roomier bottom margin (px) for discovery-phase message cards (intentionally looser). */
77
+ export declare const TIMELINE_ITEM_GAP_DISCOVERY = 24;
78
+ /**
79
+ * Accent (success green) for an auto-sent message card — drives its tint, 1px border
80
+ * and `sync` icon via {@link chatCardStyle}, so an agent-sent message is unmistakably
81
+ * green while sharing the exact card shape of the user message and info cards.
82
+ */
83
+ export declare const AUTO_SENT_ACCENT = "var(--mol-color-success, #16a34a)";
84
+ /** The accent + default icon per notice tone, keyed by tone id. */
85
+ export declare const NOTICE_TONE: Record<'info' | 'gold' | 'upgrade' | 'success' | 'signup', {
86
+ accent: string;
87
+ icon: IconName;
88
+ }>;
89
+ /**
90
+ * The SEMANTIC button colour each notice tone falls back to when the host gives
91
+ * an action no explicit `color`. Every notice-card action is a design-system
92
+ * button; this is only about which semantic it takes when the caller did not
93
+ * say. Without it a coloured action and a colourless one rendered side by side
94
+ * in the SAME card with two different box models (13px/3px vs a hand-rolled
95
+ * 12px/6px) — the exact mismatch this treatment exists to prevent.
96
+ */
97
+ export declare const NOTICE_TONE_BUTTON_COLOR: Record<'info' | 'gold' | 'upgrade' | 'success' | 'signup', NonNullable<ChatEventCardAction['color']>>;
98
+ //# sourceMappingURL=chat-ui-constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-ui-constants.d.ts","sourceRoot":"","sources":["../../src/components/chat-ui-constants.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAEnD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAEjE,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AAM,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,EAClD;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,CAWnC,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,EAAE,MAAM,CAC3C,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,EAClD,WAAW,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAO1C,CAAA"}
@@ -0,0 +1,146 @@
1
+ /**
2
+ * UI constants of the IDE chat panel: the inline card styles, the injected
3
+ * user-message accent stylesheet, attachment limits, and the timeline spacing
4
+ * and notice-tone tables. Shared verbatim by every framework's chat panel
5
+ * (React's `ChatPanel` re-exports them through `@molecule/app-ide-react`).
6
+ *
7
+ * @module
8
+ */
9
+ /** Subtle inline monospace code style for command/identifier spans in chat cards. */
10
+ export const CHAT_CARD_CODE_STYLE = {
11
+ fontFamily: 'var(--mol-font-mono, monospace)',
12
+ fontSize: '0.92em',
13
+ padding: '1px 5px',
14
+ borderRadius: 4,
15
+ background: 'rgba(128,128,128,0.16)',
16
+ border: '1px solid rgba(128,128,128,0.18)',
17
+ whiteSpace: 'nowrap',
18
+ };
19
+ /** Inline link style for action segments in chat cards (theme primary, underlined). */
20
+ export const CHAT_CARD_LINK_STYLE = {
21
+ color: 'var(--color-primary, #4070e0)',
22
+ textDecoration: 'underline',
23
+ cursor: 'pointer',
24
+ };
25
+ /**
26
+ * User-message accent stripe. The left edge of a real user message draws a vertical,
27
+ * multi-tone BLUE gradient swept gently up and down (the same smooth single-gradient
28
+ * technique as the composer ring, but vertical + blue) — NOT a repeating barber-pole,
29
+ * which read busy + janky. The host supplies the curated blues via
30
+ * `--mol-chat-accent-gradient`; the fallback is a lightened primary so it stays blue +
31
+ * never dark in any theme (the theme's primary-light/-dark tokens are actually a medium
32
+ * + a dark blue with no light tone). It's a full-box gradient (so the sweep has room to
33
+ * travel) clipped to a 3px LEFT band that follows the row's rounded corners. A
34
+ * `::before` + its keyframe can't be expressed inline (and inline `animation` can't be
35
+ * media-queried), so — like AutoCommitBadge — it is injected once and gated on the
36
+ * existing `data-mol-id`; the auto-sent row keeps its solid success accent.
37
+ */
38
+ export const USER_ACCENT_STYLE = `
39
+ [data-mol-id="chat-user-message"] { position: relative; }
40
+ [data-mol-id="chat-user-message"]::before {
41
+ content: '';
42
+ position: absolute;
43
+ inset: 0;
44
+ border-radius: inherit;
45
+ background: var(--mol-chat-accent-gradient, linear-gradient(to bottom, color-mix(in srgb, var(--mol-color-primary, #3060c0) 85%, #fff), color-mix(in srgb, var(--mol-color-primary, #3060c0) 50%, #fff) 50%, color-mix(in srgb, var(--mol-color-primary, #3060c0) 85%, #fff)));
46
+ background-size: 100% 300%;
47
+ animation: mol-chat-accent-flow 6s ease-in-out infinite;
48
+ -webkit-mask: linear-gradient(to right, #000 3px, transparent 3px);
49
+ mask: linear-gradient(to right, #000 3px, transparent 3px);
50
+ pointer-events: none;
51
+ }
52
+ @keyframes mol-chat-accent-flow {
53
+ 0%, 100% { background-position: 50% 0%; }
54
+ 50% { background-position: 50% 100%; }
55
+ }
56
+ @media (prefers-reduced-motion: reduce) {
57
+ [data-mol-id="chat-user-message"]::before { animation: none; }
58
+ }`;
59
+ /** Maximum file size for attachments (20 MB). */
60
+ export const MAX_ATTACHMENT_SIZE = 20 * 1024 * 1024;
61
+ /** MIME types accepted by the file input for AI provider attachments. */
62
+ export const ACCEPTED_FILE_TYPES = 'image/jpeg,image/png,image/gif,image/webp,image/svg+xml,application/pdf,audio/mpeg,audio/wav,audio/ogg,audio/flac,audio/webm,video/mp4,video/webm';
63
+ /** Height threshold (px) above which user messages get collapsed. */
64
+ export const USER_MSG_COLLAPSE_HEIGHT = 150;
65
+ /**
66
+ * Distance from the top (px) at which scrolling up pages in older timeline
67
+ * items. Same idea as the 80px bottom threshold of the stick-to-bottom
68
+ * detector: a margin, so the trigger fires while the user is still moving,
69
+ * not only pinned exactly at scrollTop 0.
70
+ */
71
+ export const LOAD_EARLIER_THRESHOLD_PX = 120;
72
+ // The single source of truth for the version this IDE reports — used BOTH by the
73
+ // `/version` command output AND by the `/version` entry's description in the
74
+ // slash-command menu (so the menu shows the real number, not a stale literal).
75
+ //
76
+ // This shared package owns no product branding, and there is currently no
77
+ // `version` prop on ChatPanelProps (and `types.ts` is owned elsewhere), so this
78
+ // is the minimal in-package wiring: one constant the menu + the command both
79
+ // read. When the host's real build version becomes available via a prop, point
80
+ // both sites at that prop instead of this fallback — the menu interpolation
81
+ // (`{{version}}`) already flows through, so only the value source changes.
82
+ /**
83
+ * The version this IDE reports — read by the `/version` command output AND the
84
+ * `/version` entry's description in the slash-command menu, so the menu shows
85
+ * the real number, not a stale literal.
86
+ */
87
+ export const APP_VERSION = '0.1.0';
88
+ // ONE convention for EVERY item rendered into the chat timeline — message rows,
89
+ // the "Now using <model>" / "Switched to <mode>" / "Building your app" system
90
+ // cards, toned-tip + emphasized cards, commit cards, activity cards:
91
+ //
92
+ // 1. Each timeline item owns its spacing as a SINGLE BOTTOM MARGIN only.
93
+ // 2. NO timeline item sets a TOP margin — and NEVER a NEGATIVE one.
94
+ //
95
+ // The gap between any two stacked items is therefore exactly the upper item's
96
+ // bottom margin (no margin-collapsing surprises, no negatives), so adjacent
97
+ // items can never collide. The scale follows the 8px grid (DESIGN.md): 16px is
98
+ // the standard rhythm unit; discovery messages get a roomier 24px (kept
99
+ // intentional, but consistent).
100
+ //
101
+ // Why bottom-margin-only with no negatives: the old code gave a message
102
+ // `marginTop: -12px` whenever it shared a role with the *previous message*
103
+ // (computed from `prevMsg`, which skips intervening cards). In the timeline DOM
104
+ // a system card often sits between two same-role assistant messages, so that
105
+ // -12px yanked the message UP over the card's 10px bottom margin — net -2px,
106
+ // i.e. the message visually ATE the "Now using <model>" card's bottom spacing
107
+ // (the exact collision reported in P4-05). Removing the negative and giving
108
+ // every item the same bottom margin fixes it at the root for ALL item pairs.
109
+ /** Standard bottom margin (px) every chat-timeline item owns. 8px-grid rhythm unit. */
110
+ export const TIMELINE_ITEM_GAP = 16;
111
+ /** Roomier bottom margin (px) for discovery-phase message cards (intentionally looser). */
112
+ export const TIMELINE_ITEM_GAP_DISCOVERY = 24;
113
+ /**
114
+ * Accent (success green) for an auto-sent message card — drives its tint, 1px border
115
+ * and `sync` icon via {@link chatCardStyle}, so an agent-sent message is unmistakably
116
+ * green while sharing the exact card shape of the user message and info cards.
117
+ */
118
+ export const AUTO_SENT_ACCENT = 'var(--mol-color-success, #16a34a)';
119
+ /** The accent + default icon per notice tone, keyed by tone id. */
120
+ export const NOTICE_TONE = {
121
+ info: { accent: 'var(--mol-color-primary, #6366f1)', icon: 'info-circle' },
122
+ gold: { accent: '#e0a100', icon: 'lightbulb' },
123
+ // Limit / degraded / budget / upgrade notices: a `clock` icon — an OUTLINE ring
124
+ // built exactly like `info-circle`, so it matches the other tone icons' weight
125
+ // (the solid `exclamation-triangle`/`sparkle` glyphs read far heavier). The accent
126
+ // is the theme WARNING token so the border stays visible on light backgrounds too.
127
+ upgrade: { accent: 'var(--mol-color-warning, #e0a100)', icon: 'clock' },
128
+ success: { accent: '#3fb950', icon: 'check-circle' },
129
+ signup: { accent: 'var(--mol-color-primary, #6366f1)', icon: 'sign-in' },
130
+ };
131
+ /**
132
+ * The SEMANTIC button colour each notice tone falls back to when the host gives
133
+ * an action no explicit `color`. Every notice-card action is a design-system
134
+ * button; this is only about which semantic it takes when the caller did not
135
+ * say. Without it a coloured action and a colourless one rendered side by side
136
+ * in the SAME card with two different box models (13px/3px vs a hand-rolled
137
+ * 12px/6px) — the exact mismatch this treatment exists to prevent.
138
+ */
139
+ export const NOTICE_TONE_BUTTON_COLOR = {
140
+ info: 'primary',
141
+ gold: 'warning',
142
+ upgrade: 'warning',
143
+ success: 'success',
144
+ signup: 'primary',
145
+ };
146
+ //# sourceMappingURL=chat-ui-constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-ui-constants.js","sourceRoot":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AChC,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,gFAAgF;AAChF,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,4EAA4E;AAC5E,6EAA6E;AAE7E,uFAAuF;AACvF,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAEnC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAA;AAE7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,mCAAmC,CAAA;AAEnE,mEAAmE;AACnE,MAAM,CAAC,MAAM,WAAW,GAGpB;IACF,IAAI,EAAE,EAAE,MAAM,EAAE,mCAAmC,EAAE,IAAI,EAAE,aAAa,EAAE;IAC1E,IAAI,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE;IAC9C,gFAAgF;IAChF,+EAA+E;IAC/E,mFAAmF;IACnF,mFAAmF;IACnF,OAAO,EAAE,EAAE,MAAM,EAAE,mCAAmC,EAAE,IAAI,EAAE,OAAO,EAAE;IACvE,OAAO,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,cAAc,EAAE;IACpD,MAAM,EAAE,EAAE,MAAM,EAAE,mCAAmC,EAAE,IAAI,EAAE,SAAS,EAAE;CACzE,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAGjC;IACF,IAAI,EAAE,SAAS;IACf,IAAI,EAAE,SAAS;IACf,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,SAAS;CAClB,CAAA"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Pure helpers backing the chat panel's `/undo` flow and the individual undo
3
+ * buttons on persisted tool calls.
4
+ *
5
+ * The panel tracks which tool calls are currently in the "undone" state so
6
+ * `/undo` can flip them in bulk and individual undo buttons stay in sync. This
7
+ * module owns the two derivations that feed that tracking: the initial undone
8
+ * set read from a loaded conversation's persisted `isUndone` flags, and the
9
+ * collection of the last assistant turn's file-changing tool calls (with the
10
+ * earliest original content per file) that `/undo` reverts.
11
+ *
12
+ * @module
13
+ */
14
+ import type { ChatMessage } from '@molecule/app-ai-chat';
15
+ /**
16
+ * The tool call ids already marked undone on a conversation's loaded messages.
17
+ * Initializes the panel's undone set, and re-syncs it when history bulk-loads.
18
+ *
19
+ * @param messages - The loaded conversation messages, newest last.
20
+ * @returns Every tool call id whose persisted `isUndone` flag is set.
21
+ */
22
+ export declare function initialUndoneToolCallIds(messages: ReadonlyArray<Pick<ChatMessage, 'toolCalls'>>): Set<string>;
23
+ /** The file changes `/undo` reverts, and the tool calls to mark undone. */
24
+ export interface UndoTargets {
25
+ /**
26
+ * Original content per workspace-relative path. A Map so that when multiple
27
+ * tool calls touched the same file, the earliest original wins (first write
28
+ * wins).
29
+ */
30
+ fileOriginals: Map<string, string>;
31
+ /** The ids of the file-changing tool calls being reverted. */
32
+ tcIdsToUndo: string[];
33
+ }
34
+ /**
35
+ * Collects the file changes `/undo` reverts: the last assistant turn that has
36
+ * file-changing tool calls (`write_file` / `edit_file`) with a `fileDiff`
37
+ * snapshot that are not already undone, and those calls' earliest original
38
+ * content per file.
39
+ *
40
+ * An empty {@link UndoTargets.fileOriginals} means there is nothing to undo.
41
+ *
42
+ * @param messages - The loaded conversation messages, newest last.
43
+ * @param undoneTcIds - Tool call ids already marked undone, which are skipped.
44
+ * @returns The file originals per path and the tool call ids to mark undone.
45
+ */
46
+ export declare function collectUndoTargets(messages: ReadonlyArray<ChatMessage>, undoneTcIds: ReadonlySet<string>): UndoTargets;
47
+ //# sourceMappingURL=chat-undo-utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-undo-utilities.d.ts","sourceRoot":"","sources":["../../src/components/chat-undo-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAExD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,aAAa,CAAC,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,GACtD,GAAG,CAAC,MAAM,CAAC,CAUb;AAED,2EAA2E;AAC3E,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAClC,8DAA8D;IAC9D,WAAW,EAAE,MAAM,EAAE,CAAA;CACtB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,aAAa,CAAC,WAAW,CAAC,EACpC,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,GAC/B,WAAW,CAmCb"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Pure helpers backing the chat panel's `/undo` flow and the individual undo
3
+ * buttons on persisted tool calls.
4
+ *
5
+ * The panel tracks which tool calls are currently in the "undone" state so
6
+ * `/undo` can flip them in bulk and individual undo buttons stay in sync. This
7
+ * module owns the two derivations that feed that tracking: the initial undone
8
+ * set read from a loaded conversation's persisted `isUndone` flags, and the
9
+ * collection of the last assistant turn's file-changing tool calls (with the
10
+ * earliest original content per file) that `/undo` reverts.
11
+ *
12
+ * @module
13
+ */
14
+ /**
15
+ * The tool call ids already marked undone on a conversation's loaded messages.
16
+ * Initializes the panel's undone set, and re-syncs it when history bulk-loads.
17
+ *
18
+ * @param messages - The loaded conversation messages, newest last.
19
+ * @returns Every tool call id whose persisted `isUndone` flag is set.
20
+ */
21
+ export function initialUndoneToolCallIds(messages) {
22
+ const ids = new Set();
23
+ for (const msg of messages) {
24
+ if (msg.toolCalls) {
25
+ for (const tc of msg.toolCalls) {
26
+ if (tc.isUndone)
27
+ ids.add(tc.id);
28
+ }
29
+ }
30
+ }
31
+ return ids;
32
+ }
33
+ /**
34
+ * Collects the file changes `/undo` reverts: the last assistant turn that has
35
+ * file-changing tool calls (`write_file` / `edit_file`) with a `fileDiff`
36
+ * snapshot that are not already undone, and those calls' earliest original
37
+ * content per file.
38
+ *
39
+ * An empty {@link UndoTargets.fileOriginals} means there is nothing to undo.
40
+ *
41
+ * @param messages - The loaded conversation messages, newest last.
42
+ * @param undoneTcIds - Tool call ids already marked undone, which are skipped.
43
+ * @returns The file originals per path and the tool call ids to mark undone.
44
+ */
45
+ export function collectUndoTargets(messages, undoneTcIds) {
46
+ // Find the last assistant turn that has file-changing tool calls with fileDiff snapshots.
47
+ // Skip tool calls that are already undone.
48
+ const lastTurn = [...messages]
49
+ .reverse()
50
+ .find((m) => m.role === 'assistant' &&
51
+ m.toolCalls?.some((tc) => (tc.name === 'write_file' || tc.name === 'edit_file') &&
52
+ tc.fileDiff &&
53
+ !undoneTcIds.has(tc.id)));
54
+ // Collect the tool calls to undo and their original content.
55
+ // Use a Map so that if multiple tool calls touched the same file, we restore
56
+ // to the earliest original (first write wins).
57
+ const fileOriginals = new Map();
58
+ const tcIdsToUndo = [];
59
+ for (const tc of lastTurn?.toolCalls ?? []) {
60
+ if ((tc.name === 'write_file' || tc.name === 'edit_file') &&
61
+ tc.fileDiff &&
62
+ !undoneTcIds.has(tc.id)) {
63
+ const path = tc.input?.path;
64
+ const original = tc.fileDiff.original;
65
+ if (path && !fileOriginals.has(path)) {
66
+ fileOriginals.set(path, original);
67
+ }
68
+ tcIdsToUndo.push(tc.id);
69
+ }
70
+ }
71
+ return { fileOriginals, tcIdsToUndo };
72
+ }
73
+ //# sourceMappingURL=chat-undo-utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-undo-utilities.js","sourceRoot":"","sources":["../../src/components/chat-undo-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAuD;IAEvD,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAA;IAC7B,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,GA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I,EAAE,CAAC,QAAiC,CAAC,QAAQ,CAAA;YAC/D,IAAI,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBACrC,aAAa,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;YACnC,CAAC;YACD,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA;QACzB,CAAC;IACH,CAAC;IACD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,CAAA;AACvC,CAAC"}
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Label helpers for the context-usage breakdown (the `Request composition`
3
- * table), shared by every framework's chat panel.
2
+ * Context-usage helpers shared by every framework's chat panel: the labels of
3
+ * the usage breakdown (the `Request composition` table) and the merge of
4
+ * usage samples that feeds the token history chart.
4
5
  *
5
6
  * The context-usage categories come from `@molecule/app-ai-chat`'s
6
7
  * {@link ContextCategoryId} union. Their display names used to live in the
@@ -14,7 +15,7 @@
14
15
  *
15
16
  * @module
16
17
  */
17
- import type { ContextCategoryId } from '@molecule/app-ai-chat';
18
+ import type { ContextCategoryId, ContextUsage } from '@molecule/app-ai-chat';
18
19
  import type { TranslateFunction } from '@molecule/app-i18n';
19
20
  /**
20
21
  * The localized name of one context-usage category (the
@@ -29,4 +30,25 @@ import type { TranslateFunction } from '@molecule/app-i18n';
29
30
  * @returns The localized category name.
30
31
  */
31
32
  export declare function contextCategoryLabel(t: TranslateFunction, id: ContextCategoryId): string;
33
+ /**
34
+ * Merges usage samples into a conversation's history without counting one
35
+ * request twice. Samples are keyed by `requestId` (or `at` when a sample has
36
+ * no request id; a sample with neither is dropped). A later sample for the same
37
+ * request replaces the earlier one, so a request's final usage replaces its
38
+ * pre-dispatch estimate, except that an estimate never replaces usage the
39
+ * provider reported (`composition.actualInputTokens`): a slow history response
40
+ * can arrive after the final usage it predates. The result is sorted by `at`
41
+ * and bounded to 128 samples, keeping the first so the chart's origin survives a
42
+ * long conversation.
43
+ *
44
+ * @param history - The samples already held.
45
+ * @param incoming - The samples that just arrived.
46
+ * @returns The merged samples, oldest first.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * setContextHistory((previous) => mergeContextHistory(previous, [effect.usage]))
51
+ * ```
52
+ */
53
+ export declare function mergeContextHistory(history: ContextUsage[], incoming: ContextUsage[]): ContextUsage[];
32
54
  //# sourceMappingURL=context-usage-utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context-usage-utilities.d.ts","sourceRoot":"","sources":["../../src/components/context-usage-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,iBAAiB,EAAE,EAAE,EAAE,iBAAiB,GAAG,MAAM,CA+CxF"}
1
+ {"version":3,"file":"context-usage-utilities.d.ts","sourceRoot":"","sources":["../../src/components/context-usage-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAC5E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,iBAAiB,EAAE,EAAE,EAAE,iBAAiB,GAAG,MAAM,CA+CxF;AAKD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,YAAY,EAAE,EACvB,QAAQ,EAAE,YAAY,EAAE,GACvB,YAAY,EAAE,CAiBhB"}
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Label helpers for the context-usage breakdown (the `Request composition`
3
- * table), shared by every framework's chat panel.
2
+ * Context-usage helpers shared by every framework's chat panel: the labels of
3
+ * the usage breakdown (the `Request composition` table) and the merge of
4
+ * usage samples that feeds the token history chart.
4
5
  *
5
6
  * The context-usage categories come from `@molecule/app-ai-chat`'s
6
7
  * {@link ContextCategoryId} union. Their display names used to live in the
@@ -74,4 +75,43 @@ export function contextCategoryLabel(t, id) {
74
75
  });
75
76
  }
76
77
  }
78
+ /** The most samples {@link mergeContextHistory} keeps: the first one and the newest 127. */
79
+ const CONTEXT_HISTORY_LIMIT = 128;
80
+ /**
81
+ * Merges usage samples into a conversation's history without counting one
82
+ * request twice. Samples are keyed by `requestId` (or `at` when a sample has
83
+ * no request id; a sample with neither is dropped). A later sample for the same
84
+ * request replaces the earlier one, so a request's final usage replaces its
85
+ * pre-dispatch estimate, except that an estimate never replaces usage the
86
+ * provider reported (`composition.actualInputTokens`): a slow history response
87
+ * can arrive after the final usage it predates. The result is sorted by `at`
88
+ * and bounded to 128 samples, keeping the first so the chart's origin survives a
89
+ * long conversation.
90
+ *
91
+ * @param history - The samples already held.
92
+ * @param incoming - The samples that just arrived.
93
+ * @returns The merged samples, oldest first.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * setContextHistory((previous) => mergeContextHistory(previous, [effect.usage]))
98
+ * ```
99
+ */
100
+ export function mergeContextHistory(history, incoming) {
101
+ const samples = new Map();
102
+ for (const usage of [...history, ...incoming]) {
103
+ if (!usage.requestId && !usage.at)
104
+ continue;
105
+ const key = usage.requestId ?? usage.at ?? '';
106
+ const previous = samples.get(key);
107
+ if (previous?.composition?.actualInputTokens != null &&
108
+ usage.composition?.actualInputTokens == null)
109
+ continue;
110
+ samples.set(key, usage);
111
+ }
112
+ const result = [...samples.values()].sort((a, b) => (a.at ?? '').localeCompare(b.at ?? ''));
113
+ return result.length > CONTEXT_HISTORY_LIMIT
114
+ ? [result[0], ...result.slice(-(CONTEXT_HISTORY_LIMIT - 1))]
115
+ : result;
116
+ }
77
117
  //# sourceMappingURL=context-usage-utilities.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"context-usage-utilities.js","sourceRoot":"","sources":["../../src/components/context-usage-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAAC,CAAoB,EAAE,EAAqB;IAC9E,QAAQ,EAAE,EAAE,CAAC;QACX,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,kCAAkC,EAAE,SAAS,EAAE;gBACtD,YAAY,EAAE,qBAAqB;aACpC,CAAC,CAAA;QACJ,KAAK,SAAS;YACZ,OAAO,CAAC,CAAC,mCAAmC,EAAE,SAAS,EAAE;gBACvD,YAAY,EAAE,kBAAkB;aACjC,CAAC,CAAA;QACJ,KAAK,iBAAiB;YACpB,OAAO,CAAC,CAAC,2CAA2C,EAAE,SAAS,EAAE;gBAC/D,YAAY,EAAE,kBAAkB;aACjC,CAAC,CAAA;QACJ,KAAK,aAAa;YAChB,OAAO,CAAC,CAAC,uCAAuC,EAAE,SAAS,EAAE;gBAC3D,YAAY,EAAE,gBAAgB;aAC/B,CAAC,CAAA;QACJ,KAAK,UAAU;YACb,OAAO,CAAC,CAAC,oCAAoC,EAAE,SAAS,EAAE;gBACxD,YAAY,EAAE,eAAe;aAC9B,CAAC,CAAA;QACJ,KAAK,eAAe;YAClB,OAAO,CAAC,CAAC,yCAAyC,EAAE,SAAS,EAAE;gBAC7D,YAAY,EAAE,oBAAoB;aACnC,CAAC,CAAA;QACJ,KAAK,WAAW;YACd,OAAO,CAAC,CAAC,qCAAqC,EAAE,SAAS,EAAE,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC,CAAA;QAC3F,KAAK,WAAW;YACd,OAAO,CAAC,CAAC,qCAAqC,EAAE,SAAS,EAAE;gBACzD,YAAY,EAAE,gBAAgB;aAC/B,CAAC,CAAA;QACJ,KAAK,aAAa;YAChB,OAAO,CAAC,CAAC,uCAAuC,EAAE,SAAS,EAAE;gBAC3D,YAAY,EAAE,cAAc;aAC7B,CAAC,CAAA;QACJ,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,kCAAkC,EAAE,SAAS,EAAE,EAAE,YAAY,EAAE,QAAQ,EAAE,CAAC,CAAA;QACrF,KAAK,aAAa;YAChB,OAAO,CAAC,CAAC,uCAAuC,EAAE,SAAS,EAAE;gBAC3D,YAAY,EAAE,aAAa;aAC5B,CAAC,CAAA;QACJ,KAAK,oBAAoB;YACvB,OAAO,CAAC,CAAC,8CAA8C,EAAE,SAAS,EAAE;gBAClE,YAAY,EAAE,mBAAmB;aAClC,CAAC,CAAA;IACN,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"context-usage-utilities.js","sourceRoot":"","sources":["../../src/components/context-usage-utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAKH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAAC,CAAoB,EAAE,EAAqB;IAC9E,QAAQ,EAAE,EAAE,CAAC;QACX,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,kCAAkC,EAAE,SAAS,EAAE;gBACtD,YAAY,EAAE,qBAAqB;aACpC,CAAC,CAAA;QACJ,KAAK,SAAS;YACZ,OAAO,CAAC,CAAC,mCAAmC,EAAE,SAAS,EAAE;gBACvD,YAAY,EAAE,kBAAkB;aACjC,CAAC,CAAA;QACJ,KAAK,iBAAiB;YACpB,OAAO,CAAC,CAAC,2CAA2C,EAAE,SAAS,EAAE;gBAC/D,YAAY,EAAE,kBAAkB;aACjC,CAAC,CAAA;QACJ,KAAK,aAAa;YAChB,OAAO,CAAC,CAAC,uCAAuC,EAAE,SAAS,EAAE;gBAC3D,YAAY,EAAE,gBAAgB;aAC/B,CAAC,CAAA;QACJ,KAAK,UAAU;YACb,OAAO,CAAC,CAAC,oCAAoC,EAAE,SAAS,EAAE;gBACxD,YAAY,EAAE,eAAe;aAC9B,CAAC,CAAA;QACJ,KAAK,eAAe;YAClB,OAAO,CAAC,CA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AAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACjC,IACE,QAAQ,EAAE,WAAW,EAAE,iBAAiB,IAAI,IAAI;YAChD,KAAK,CAAC,WAAW,EAAE,iBAAiB,IAAI,IAAI;YAE5C,SAAQ;QACV,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACzB,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;IAC3F,OAAO,MAAM,CAAC,MAAM,GAAG,qBAAqB;QAC1C,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,qBAAqB,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5D,CAAC,CAAC,MAAM,CAAA;AACZ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -34,9 +34,11 @@ export * from './command-metadata.js';
34
34
  export * from './components/activity-utilities.js';
35
35
  export * from './components/chat-autocommit-utilities.js';
36
36
  export * from './components/chat-card-style.js';
37
+ export * from './components/chat-format-utilities.js';
37
38
  export * from './components/chat-report-utilities.js';
38
39
  export * from './components/chat-share-utilities.js';
39
40
  export * from './components/chat-timestamps-utilities.js';
41
+ export * from './components/chat-ui-constants.js';
40
42
  export * from './components/device-cycle.js';
41
43
  export * from './components/search-utilities.js';
42
44
  export * from './components/tests-card-utilities.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAA;AACrC,cAAc,oCAAoC,CAAA;AAClD,cAAc,2CAA2C,CAAA;AACzD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,uCAAuC,CAAA;AACrD,cAAc,sCAAsC,CAAA;AACpD,cAAc,2CAA2C,CAAA;AACzD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,kCAAkC,CAAA;AAChD,cAAc,sCAAsC,CAAA;AACpD,cAAc,uCAAuC,CAAA;AACrD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kCAAkC,CAAA;AAChD,cAAc,wBAAwB,CAAA;AACtC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAA;AACrC,cAAc,oCAAoC,CAAA;AAClD,cAAc,2CAA2C,CAAA;AACzD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,uCAAuC,CAAA;AACrD,cAAc,uCAAuC,CAAA;AACrD,cAAc,sCAAsC,CAAA;AACpD,cAAc,2CAA2C,CAAA;AACzD,cAAc,mCAAmC,CAAA;AACjD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,kCAAkC,CAAA;AAChD,cAAc,sCAAsC,CAAA;AACpD,cAAc,uCAAuC,CAAA;AACrD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kCAAkC,CAAA;AAChD,cAAc,wBAAwB,CAAA;AACtC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA"}
package/dist/index.js CHANGED
@@ -34,9 +34,11 @@ export * from './command-metadata.js';
34
34
  export * from './components/activity-utilities.js';
35
35
  export * from './components/chat-autocommit-utilities.js';
36
36
  export * from './components/chat-card-style.js';
37
+ export * from './components/chat-format-utilities.js';
37
38
  export * from './components/chat-report-utilities.js';
38
39
  export * from './components/chat-share-utilities.js';
39
40
  export * from './components/chat-timestamps-utilities.js';
41
+ export * from './components/chat-ui-constants.js';
40
42
  export * from './components/device-cycle.js';
41
43
  export * from './components/search-utilities.js';
42
44
  export * from './components/tests-card-utilities.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAA;AACrC,cAAc,oCAAoC,CAAA;AAClD,cAAc,2CAA2C,CAAA;AACzD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,uCAAuC,CAAA;AACrD,cAAc,sCAAsC,CAAA;AACpD,cAAc,2CAA2C,CAAA;AACzD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,kCAAkC,CAAA;AAChD,cAAc,sCAAsC,CAAA;AACpD,cAAc,uCAAuC,CAAA;AACrD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kCAAkC,CAAA;AAChD,cAAc,wBAAwB,CAAA;AACtC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,cAAc,uBAAuB,CAAA;AACrC,cAAc,oCAAoC,CAAA;AAClD,cAAc,2CAA2C,CAAA;AACzD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,uCAAuC,CAAA;AACrD,cAAc,uCAAuC,CAAA;AACrD,cAAc,sCAAsC,CAAA;AACpD,cAAc,2CAA2C,CAAA;AACzD,cAAc,mCAAmC,CAAA;AACjD,cAAc,8BAA8B,CAAA;AAC5C,cAAc,kCAAkC,CAAA;AAChD,cAAc,sCAAsC,CAAA;AACpD,cAAc,uCAAuC,CAAA;AACrD,cAAc,4CAA4C,CAAA;AAC1D,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kCAAkC,CAAA;AAChD,cAAc,wBAAwB,CAAA;AACtC,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA"}