@acornly/ui 0.1.0 → 0.3.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 (64) hide show
  1. package/README.md +122 -4
  2. package/dist/brand.d.ts +50 -0
  3. package/dist/brand.d.ts.map +1 -0
  4. package/dist/brand.js +84 -0
  5. package/dist/brand.js.map +1 -0
  6. package/dist/card.d.ts +24 -1
  7. package/dist/card.d.ts.map +1 -1
  8. package/dist/card.js +49 -9
  9. package/dist/card.js.map +1 -1
  10. package/dist/chat-model.d.ts +165 -0
  11. package/dist/chat-model.d.ts.map +1 -0
  12. package/dist/chat-model.js +576 -0
  13. package/dist/chat-model.js.map +1 -0
  14. package/dist/chat-panel.d.ts +36 -0
  15. package/dist/chat-panel.d.ts.map +1 -0
  16. package/dist/chat-panel.js +760 -0
  17. package/dist/chat-panel.js.map +1 -0
  18. package/dist/chat-picker.d.ts +56 -0
  19. package/dist/chat-picker.d.ts.map +1 -0
  20. package/dist/chat-picker.js +284 -0
  21. package/dist/chat-picker.js.map +1 -0
  22. package/dist/chat-protocol.d.ts +151 -0
  23. package/dist/chat-protocol.d.ts.map +1 -0
  24. package/dist/chat-protocol.js +113 -0
  25. package/dist/chat-protocol.js.map +1 -0
  26. package/dist/chat-speech.d.ts +61 -0
  27. package/dist/chat-speech.d.ts.map +1 -0
  28. package/dist/chat-speech.js +119 -0
  29. package/dist/chat-speech.js.map +1 -0
  30. package/dist/chat.d.ts +148 -0
  31. package/dist/chat.d.ts.map +1 -0
  32. package/dist/chat.js +119 -0
  33. package/dist/chat.js.map +1 -0
  34. package/dist/compose.d.ts +1 -1
  35. package/dist/compose.d.ts.map +1 -1
  36. package/dist/compose.js.map +1 -1
  37. package/dist/composer.d.ts +26 -1
  38. package/dist/composer.d.ts.map +1 -1
  39. package/dist/composer.js +73 -5
  40. package/dist/composer.js.map +1 -1
  41. package/dist/index.d.ts +1 -1
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/media.d.ts.map +1 -1
  45. package/dist/media.js +0 -1
  46. package/dist/media.js.map +1 -1
  47. package/dist/painter.d.ts +20 -2
  48. package/dist/painter.d.ts.map +1 -1
  49. package/dist/painter.js +41 -6
  50. package/dist/painter.js.map +1 -1
  51. package/dist/panel.js +5 -5
  52. package/dist/parts.d.ts +2 -0
  53. package/dist/parts.d.ts.map +1 -1
  54. package/dist/parts.js +2 -0
  55. package/dist/parts.js.map +1 -1
  56. package/dist/pins.d.ts.map +1 -1
  57. package/dist/pins.js +14 -9
  58. package/dist/pins.js.map +1 -1
  59. package/dist/theme.d.ts +1 -1
  60. package/dist/theme.d.ts.map +1 -1
  61. package/dist/theme.js +14 -34
  62. package/dist/theme.js.map +1 -1
  63. package/dist/toolbar.js +3 -3
  64. package/package.json +11 -3
@@ -0,0 +1,165 @@
1
+ /**
2
+ * The in-app chat's model calls: building a Messages API request from the app's context, calling
3
+ * the model proxy (or the app's relay) tagged as chat, streaming the reply through the app's guard,
4
+ * and the `read_context` and `offer` tools. Part of the chat's lazy chunk.
5
+ */
6
+ import { type ContextItem, type ContextSnapshot } from "./chat-protocol.js";
7
+ /** The header the model proxy reads to tell chat's calls from the app's own (design doc, section 10). */
8
+ export declare const FEATURE_HEADER = "Acornly-Feature";
9
+ export declare const FEATURE = "chat";
10
+ export type Block = Record<string, unknown> & {
11
+ type: string;
12
+ };
13
+ export interface Message {
14
+ role: "user" | "assistant";
15
+ content: string | Block[];
16
+ }
17
+ export interface MessagesRequest {
18
+ model: string;
19
+ max_tokens: number;
20
+ system: Block[];
21
+ messages: Message[];
22
+ tools?: Record<string, unknown>[];
23
+ tool_choice?: {
24
+ type: "auto" | "none";
25
+ };
26
+ stream: true;
27
+ }
28
+ /** Sends one request; the response is the Messages API's event stream. */
29
+ export type ChatTransport = (body: MessagesRequest, signal: AbortSignal) => Promise<Response>;
30
+ /** Calls the model proxy Worker straight from the browser, with the person's app token. */
31
+ export declare function proxyTransport(options: {
32
+ /** The Worker's address, without a trailing slash. */
33
+ url: string;
34
+ appId: string;
35
+ /** The person's app token, fresh for each call. */
36
+ token: () => string | Promise<string>;
37
+ fetch?: typeof fetch;
38
+ }): ChatTransport;
39
+ /**
40
+ * Posts each request to the app's own endpoint, which adds what the browser must not hold, checks
41
+ * the reply, and calls the proxy with its server token. It must forward `Acornly-Feature`.
42
+ */
43
+ export declare function relayTransport(options: {
44
+ endpoint: string;
45
+ headers?: () => Record<string, string> | Promise<Record<string, string>>;
46
+ fetch?: typeof fetch;
47
+ }): ChatTransport;
48
+ /** One finished exchange, as later turns resend it. */
49
+ export interface Exchange {
50
+ question: string;
51
+ reply: string;
52
+ }
53
+ /** A hand-back the model offered, about an item. */
54
+ export interface Offer {
55
+ handBack: string;
56
+ item: string;
57
+ }
58
+ export interface TurnInput {
59
+ snapshot: ContextSnapshot;
60
+ /** The conversation's focus, kept from its first message. */
61
+ focus: readonly string[];
62
+ /** Items read so far in this conversation, by id. */
63
+ loaded: ReadonlyMap<string, ContextItem>;
64
+ /**
65
+ * The conversation's own copy of its focus items and their ancestors, from when they were last
66
+ * on screen, so a follow-up stays about them after the app moves on.
67
+ */
68
+ kept?: ReadonlyMap<string, ContextItem>;
69
+ history: readonly Exchange[];
70
+ question: string;
71
+ model: string;
72
+ }
73
+ /** The chat's instructions, the app's rules (from the latest snapshot) and its hand-backs. */
74
+ export declare function systemPrompt(snapshot: ContextSnapshot): string;
75
+ /** Tracks what one turn may still carry, so it stays inside the protocol's limits. */
76
+ export declare class Budget {
77
+ chars: 64000;
78
+ images: 4;
79
+ dropped: string[];
80
+ }
81
+ /** An item's content as blocks: its sendable text and images, with a note for what is withheld. */
82
+ export declare function itemBlocks(item: ContextItem, budget: Budget): Promise<Block[]>;
83
+ /** The tree and around as an outline of labels, focus marked: what every turn knows for free. */
84
+ export declare function outline(snapshot: ContextSnapshot, focus: readonly string[], offScreen?: ReadonlySet<string>): string;
85
+ /**
86
+ * The request for a turn's first round. Ordered for prompt caching: instructions and rules, the
87
+ * outline, the focus and what was read, then the conversation.
88
+ */
89
+ /**
90
+ * The app's latest snapshot with the conversation's kept items added where the app no longer lists
91
+ * them (ancestors first), and the ids so added. An item the snapshot still lists is the snapshot's,
92
+ * so its latest `use` applies.
93
+ */
94
+ export declare function conversationView(snapshot: ContextSnapshot, kept: ReadonlyMap<string, ContextItem> | undefined): {
95
+ view: ContextSnapshot;
96
+ offScreen: Set<string>;
97
+ };
98
+ /** The request for a turn's first round, and the turn's budget, which its reads share. */
99
+ export declare function buildRequest(input: TurnInput): Promise<{
100
+ body: MessagesRequest;
101
+ dropped: string[];
102
+ budget: Budget;
103
+ }>;
104
+ /**
105
+ * Input tokens a request will take, roughly: four characters a token, images at 1,600. A thinking
106
+ * block's signature is not counted; a redacted block's data is, standing in for the thinking it
107
+ * holds.
108
+ */
109
+ export declare function estimateTokens(body: MessagesRequest): number;
110
+ /** A failed call, with the proxy's error code when it gave one (`budget_exhausted`, `feature_cap`). */
111
+ export declare class ChatCallError extends Error {
112
+ readonly status: number;
113
+ readonly code?: string | undefined;
114
+ constructor(message: string, status: number, code?: string | undefined);
115
+ }
116
+ interface Streamed {
117
+ content: Block[];
118
+ stopReason: string | null;
119
+ /** The app's relay ended the stream as refused (`{"type":"acornly_refused"}`). */
120
+ refused: boolean;
121
+ }
122
+ /** The event an app's relay sends to say its guard flagged the reply (design doc, section 10). */
123
+ export declare const REFUSED_EVENT = "acornly_refused";
124
+ /** Reads a Messages API event stream, passing text as it arrives. */
125
+ export declare function readStream(response: Response, onText: (text: string) => void): Promise<Streamed>;
126
+ /** Splits streamed text into finished sentences, keeping the unfinished rest. */
127
+ export declare function takeSentences(buffer: string): {
128
+ sentences: string[];
129
+ rest: string;
130
+ };
131
+ /** True when a sentence breaks the app's rules: the reply is then withheld. */
132
+ export type ReplyGuard = (sentence: string, replySoFar: string) => boolean | Promise<boolean>;
133
+ export interface TurnHooks {
134
+ transport: ChatTransport;
135
+ /** The app's `load`, for `read_context`. */
136
+ load: (id: string) => Promise<ContextItem | null>;
137
+ guard?: ReplyGuard | undefined;
138
+ /** Each checked piece of the reply, as it may be shown. */
139
+ onText(text: string): void;
140
+ /** An item was read, for "Read: …" under the reply. */
141
+ onRead?(item: ContextItem): void;
142
+ signal: AbortSignal;
143
+ }
144
+ export interface TurnResult {
145
+ reply: string;
146
+ /** The guard, or the app's relay, flagged the reply: it was replaced by a refusal. */
147
+ refused: boolean;
148
+ offers: Offer[];
149
+ read: ContextItem[];
150
+ dropped: string[];
151
+ }
152
+ /**
153
+ * Runs one turn: up to three model rounds, reading items the model asks for between them, with
154
+ * every finished sentence checked by the guard before it is shown; with no guard, the text is shown
155
+ * as it streams.
156
+ */
157
+ export declare function runTurn(input: TurnInput, hooks: TurnHooks): Promise<TurnResult>;
158
+ /**
159
+ * Drafts the one- or two-line takeaway "Keep" offers, from one exchange, under the latest rules:
160
+ * their `never` in its instructions, and the whole draft through the guard. A draft the guard or the
161
+ * app's relay flags comes back empty, for the person to write their own.
162
+ */
163
+ export declare function draftTakeaway(exchange: Exchange, snapshot: ContextSnapshot, model: string, transport: ChatTransport, signal: AbortSignal, guard?: ReplyGuard): Promise<string>;
164
+ export {};
165
+ //# sourceMappingURL=chat-model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-model.d.ts","sourceRoot":"","sources":["../src/chat-model.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAQL,KAAK,WAAW,EAChB,KAAK,eAAe,EACrB,MAAM,oBAAoB,CAAC;AAE5B,yGAAyG;AACzG,eAAO,MAAM,cAAc,oBAAoB,CAAC;AAChD,eAAO,MAAM,OAAO,SAAS,CAAC;AAE9B,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAC/D,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAC3B,OAAO,E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