@ateam-ai-mobile/core 0.1.32 → 0.1.35
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/dist/index.d.mts +166 -22
- package/dist/index.d.ts +166 -22
- package/dist/index.js +297 -46
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +285 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/chat/attachments.ts +139 -0
- package/src/chat/blockText.ts +253 -0
- package/src/chat/store.ts +19 -74
- package/src/chat/types.ts +2 -0
- package/src/config.ts +4 -1
- package/src/index.ts +19 -1
- package/src/storage/types.ts +57 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ateam-ai-mobile/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.35",
|
|
4
4
|
"description": "Headless ADAS SDK core — auth bridge, API client, chat/job streaming, MCP calls, theme tokens. No UI, no Expo dependencies.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turning the attachments a user staged into what `POST /api/chat` takes.
|
|
3
|
+
*
|
|
4
|
+
* A file given by `uri` is uploaded first, as the raw request body of `POST /api/artifacts` (the same contract as the web
|
|
5
|
+
* client's uploadArtifact.js: `Content-Type: application/octet-stream`, the real type in `?mimeType=`, the name in `?name=`,
|
|
6
|
+
* the bearer token as the credential). Core stores it (up to 90 MiB) and answers `{ hash }`; the chat message then names the
|
|
7
|
+
* file by that hash. Nothing big ever rides inside the chat JSON.
|
|
8
|
+
*
|
|
9
|
+
* An OLD Core has no raw path: it parses the body as JSON and answers 400. For a file that still fits the old 10 MB base64
|
|
10
|
+
* path the attachment is then sent inline exactly as before; a bigger one fails with a plain message.
|
|
11
|
+
*/
|
|
12
|
+
import { getConfig, getCoreApiUrl, getToken, refreshToken, notifyAuthFailure } from '../config';
|
|
13
|
+
import { MAX_ATTACHMENT_BYTES, MAX_INLINE_ATTACHMENT_BYTES } from '../storage/types';
|
|
14
|
+
import type { OutgoingAttachment, FileUploader } from '../storage/types';
|
|
15
|
+
|
|
16
|
+
/** One attachment as `/api/chat` takes it: a stored file by hash, or small inline base64. */
|
|
17
|
+
export type WireAttachment =
|
|
18
|
+
| { hash: string; name: string; mimeType: string }
|
|
19
|
+
| { data: string; mimeType: string; name: string };
|
|
20
|
+
|
|
21
|
+
const mb = (bytes: number) => Math.round(bytes / 1024 / 1024);
|
|
22
|
+
|
|
23
|
+
/** base64 length of `bytes` raw bytes. */
|
|
24
|
+
const base64Length = (bytes: number) => Math.ceil(bytes / 3) * 4;
|
|
25
|
+
|
|
26
|
+
/** The most base64 characters all inline attachments of ONE message may add (see MAX_INLINE_ATTACHMENT_BYTES). */
|
|
27
|
+
const MAX_INLINE_CHARS = base64Length(MAX_INLINE_ATTACHMENT_BYTES);
|
|
28
|
+
|
|
29
|
+
function tooLarge(name: string): Error {
|
|
30
|
+
return new Error(`"${name}" is too large (max ${mb(MAX_ATTACHMENT_BYTES)} MB)`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function errorFrom(status: number, body: string, name: string): Error {
|
|
34
|
+
let message = '';
|
|
35
|
+
try { message = String(JSON.parse(body)?.error || ''); } catch { /* an HTML error page from a proxy */ }
|
|
36
|
+
if (status === 413) {
|
|
37
|
+
// Core's own 413 is JSON with the real limit; an nginx 413 (an old Core stops at 20 MB) is HTML and names nothing.
|
|
38
|
+
return new Error(message || `This server can't take files this big yet ("${name}")`);
|
|
39
|
+
}
|
|
40
|
+
if (status === 429) return new Error(message || 'Too many uploads: wait a moment and try again');
|
|
41
|
+
return new Error(message || `The upload of "${name}" failed (${status})`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function sendRaw(
|
|
45
|
+
uploader: FileUploader,
|
|
46
|
+
att: OutgoingAttachment,
|
|
47
|
+
onProgress: (fraction: number) => void,
|
|
48
|
+
): Promise<{ status: number; body: string }> {
|
|
49
|
+
const query = new URLSearchParams({ mimeType: att.mimeType || 'application/octet-stream', name: att.name || 'upload' });
|
|
50
|
+
const url = `${getCoreApiUrl()}/api/artifacts?${query.toString()}`;
|
|
51
|
+
const attempt = () => {
|
|
52
|
+
const token = getToken();
|
|
53
|
+
return uploader.upload({
|
|
54
|
+
url,
|
|
55
|
+
uri: att.uri as string,
|
|
56
|
+
headers: {
|
|
57
|
+
'Content-Type': 'application/octet-stream',
|
|
58
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
59
|
+
},
|
|
60
|
+
onProgress,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
let res = await attempt();
|
|
64
|
+
if (res.status === 401 && getToken()) {
|
|
65
|
+
// Same semantics as apiFetch: refresh once and retry; only a dead session signs the user out.
|
|
66
|
+
const outcome = await refreshToken();
|
|
67
|
+
if (outcome.ok === 'ok') {
|
|
68
|
+
res = await attempt();
|
|
69
|
+
if (res.status === 401 || res.status === 403) {
|
|
70
|
+
notifyAuthFailure();
|
|
71
|
+
throw new Error('Session expired: sign in again to upload this file');
|
|
72
|
+
}
|
|
73
|
+
} else if (outcome.ok === 'auth_failed') {
|
|
74
|
+
notifyAuthFailure();
|
|
75
|
+
throw new Error('Session expired: sign in again to upload this file');
|
|
76
|
+
} else {
|
|
77
|
+
// A temporary failure (network, 5xx): the session is fine, the person can simply try again.
|
|
78
|
+
throw new Error('Could not reach the server to refresh your sign-in: check your connection and try again');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return res;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `budget.chars` is what the message may still add inline; it is spent here when this file goes inline. */
|
|
85
|
+
async function uploadOne(att: OutgoingAttachment, onProgress: (fraction: number) => void, budget: { chars: number }): Promise<WireAttachment> {
|
|
86
|
+
const name = att.name || 'attachment';
|
|
87
|
+
if (att.data !== undefined) return { data: att.data, mimeType: att.mimeType, name: att.name };
|
|
88
|
+
if (!att.uri) throw new Error(`The attachment "${name}" has no file to send`);
|
|
89
|
+
if (att.size !== undefined && att.size > MAX_ATTACHMENT_BYTES) throw tooLarge(name);
|
|
90
|
+
|
|
91
|
+
const uploader = getConfig().fileUploader;
|
|
92
|
+
if (!uploader) throw new Error('This app cannot upload files yet: initAdas needs a fileUploader');
|
|
93
|
+
|
|
94
|
+
const res = await sendRaw(uploader, att, onProgress);
|
|
95
|
+
if (res.status >= 200 && res.status < 300) {
|
|
96
|
+
let parsed: { ok?: boolean; hash?: string; mimeType?: string } | null = null;
|
|
97
|
+
try { parsed = JSON.parse(res.body); } catch { /* fall through to the error below */ }
|
|
98
|
+
if (parsed?.ok && typeof parsed.hash === 'string') {
|
|
99
|
+
onProgress(1);
|
|
100
|
+
return { hash: parsed.hash, name: att.name, mimeType: parsed.mimeType || att.mimeType };
|
|
101
|
+
}
|
|
102
|
+
throw errorFrom(res.status, res.body, name);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (res.status === 400) {
|
|
106
|
+
// An old Core: POST /api/artifacts does not know a raw body. Fall back to base64 in the chat JSON, if it fits.
|
|
107
|
+
// Decide from the SIZE, before a byte is read: Core's 10 MB limit covers the whole chat body, all attachments together.
|
|
108
|
+
const size = att.size !== undefined ? att.size : await uploader.fileSize?.(att.uri);
|
|
109
|
+
if (size === undefined) {
|
|
110
|
+
throw new Error(`Cannot tell how big "${name}" is, and this server cannot take large files yet`);
|
|
111
|
+
}
|
|
112
|
+
if (base64Length(size) > budget.chars) {
|
|
113
|
+
throw new Error(`"${name}" is too big for this server, which cannot take more than ${mb(MAX_INLINE_ATTACHMENT_BYTES)} MB of files in one message yet`);
|
|
114
|
+
}
|
|
115
|
+
const data = await uploader.readBase64(att.uri);
|
|
116
|
+
budget.chars -= data.length;
|
|
117
|
+
onProgress(1);
|
|
118
|
+
return { data, mimeType: att.mimeType, name: att.name };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
throw errorFrom(res.status, res.body, name);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Upload what needs uploading and return the attachments as `/api/chat` takes them, in order.
|
|
126
|
+
* `onProgress` is the overall fraction 0..1 across all attachments.
|
|
127
|
+
*/
|
|
128
|
+
export async function prepareAttachments(
|
|
129
|
+
attachments: OutgoingAttachment[],
|
|
130
|
+
onProgress?: (fraction: number) => void,
|
|
131
|
+
): Promise<WireAttachment[]> {
|
|
132
|
+
// Inline attachments already in the message use up the old path's budget before any fallback does.
|
|
133
|
+
const budget = { chars: MAX_INLINE_CHARS - attachments.reduce((n, a) => n + (a.data !== undefined ? a.data.length : 0), 0) };
|
|
134
|
+
const out: WireAttachment[] = [];
|
|
135
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
136
|
+
out.push(await uploadOne(attachments[i], f => onProgress?.((i + f) / attachments.length), budget));
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the phone's chat bubble is handed for a reply: the blocks of a conversation turn as ONE flat string, and that string
|
|
3
|
+
* split back into what the bubble draws. Pure and import-free, so a test runs it in node (it used to sit in store.ts).
|
|
4
|
+
*
|
|
5
|
+
* Nothing a reply holds is dropped. This file used to throw away every json and code block that sat beside a text block (a copy of
|
|
6
|
+
* the web chat's March rule), so a script, a command or a diagram under the answer never reached the person. Which raw data is
|
|
7
|
+
* shown CLOSED is Core's call, made once, at read (backend utils/blockDisplay.js marks it `meta.display: "folded"`); this file only
|
|
8
|
+
* carries that mark to the bubble: a marked block's text is wrapped in the DETAILS sentinels, and the bubble draws that segment as
|
|
9
|
+
* a closed "Details" row. It never decides what folds.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Sentinel delimiters used to embed structured table data inside a flat message string. UI layers split on these and render the
|
|
14
|
+
* contents with a native table component instead of markdown tables, which look terrible on narrow phone screens.
|
|
15
|
+
*
|
|
16
|
+
* Unicode Private Use Area chars so they never collide with anything the model could legitimately emit.
|
|
17
|
+
*/
|
|
18
|
+
export const TABLE_BLOCK_OPEN = '\uE100';
|
|
19
|
+
export const TABLE_BLOCK_CLOSE = '\uE101';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The same, for a block Core marked folded: its text (already rendered to markdown) travels between these, percent-encoded so it
|
|
23
|
+
* can never hold a newline or a sentinel, and the bubble draws it closed under "Details".
|
|
24
|
+
*/
|
|
25
|
+
export const DETAILS_BLOCK_OPEN = '\uE102';
|
|
26
|
+
export const DETAILS_BLOCK_CLOSE = '\uE103';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The same, for a `diagram` block: a straight list of numbered steps with arrows and a colour per status word. Its payload is the
|
|
30
|
+
* clean `DiagramView` (below) as percent-encoded JSON, and the bubble draws it with a native component.
|
|
31
|
+
*/
|
|
32
|
+
export const DIAGRAM_BLOCK_OPEN = '\uE104';
|
|
33
|
+
export const DIAGRAM_BLOCK_CLOSE = '\uE105';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A block's own text can hold one of the six sentinels (a glyph from an icon font, a paste). One inside a text would cut the
|
|
37
|
+
* message in the wrong place (a stray DETAILS mark would swallow the words before it into a closed row), so they are taken out of
|
|
38
|
+
* the text of a block. A table, a folded block and a diagram carry their payload percent-encoded, which cannot hold one.
|
|
39
|
+
*/
|
|
40
|
+
const SENTINEL_CHARS = /[\uE100-\uE105]/g;
|
|
41
|
+
const clean = (text: string): string => text.replace(SENTINEL_CHARS, '');
|
|
42
|
+
|
|
43
|
+
/** A block of a conversation turn, as /api/conversation serves it. */
|
|
44
|
+
export interface ConversationBlock {
|
|
45
|
+
kind: string;
|
|
46
|
+
content?: any;
|
|
47
|
+
language?: string;
|
|
48
|
+
meta?: { display?: string };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Render a `{ columns: [...], rows: [...] }` payload as a sentinel-wrapped JSON marker. Returns null if the shape doesn't match.
|
|
53
|
+
*/
|
|
54
|
+
function renderTableShape(content: any): string | null {
|
|
55
|
+
if (!content || typeof content !== 'object') return null;
|
|
56
|
+
const cols = Array.isArray(content.columns) ? content.columns : null;
|
|
57
|
+
const rows = Array.isArray(content.rows) ? content.rows : null;
|
|
58
|
+
if (!cols || !rows) return null;
|
|
59
|
+
// encodeURIComponent so the JSON can never contain newlines or sentinel chars.
|
|
60
|
+
const payload = encodeURIComponent(JSON.stringify({ columns: cols, rows }));
|
|
61
|
+
return `${TABLE_BLOCK_OPEN}${payload}${TABLE_BLOCK_CLOSE}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The status words a diagram step or badge may carry; none is neutral. The same four as Core's `DIAGRAM_STATUSES` (backend
|
|
66
|
+
* utils/blockShape.js) and the web's (apps/frontend chat/blocks/diagramView.js): a test on each side holds the list. Colours are
|
|
67
|
+
* this app's own; Core never sends one.
|
|
68
|
+
*/
|
|
69
|
+
export const DIAGRAM_STATUSES = ['ok', 'bad', 'warn', 'info'] as const;
|
|
70
|
+
export type DiagramStatus = (typeof DIAGRAM_STATUSES)[number];
|
|
71
|
+
|
|
72
|
+
export interface DiagramView {
|
|
73
|
+
title: string;
|
|
74
|
+
steps: Array<{ title: string; text: string; status: DiagramStatus | ''; badges: Array<{ text: string; status: DiagramStatus | '' }> }>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const str = (v: any): string => (typeof v === 'string' ? v : typeof v === 'number' || typeof v === 'boolean' ? String(v) : '');
|
|
78
|
+
const statusOf = (v: any): DiagramStatus | '' => ((DIAGRAM_STATUSES as readonly string[]).includes(v) ? v : '');
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The diagram a block holds, ready to draw, every field text; null when there is nothing to draw (fewer than 2 steps). Core sends it
|
|
82
|
+
* clean (`diagramOf`); this is the phone's own net, so a field of the wrong type can never reach a native <Text>.
|
|
83
|
+
*/
|
|
84
|
+
export function diagramView(content: any): DiagramView | null {
|
|
85
|
+
if (!content || typeof content !== 'object' || !Array.isArray(content.steps)) return null;
|
|
86
|
+
const steps: DiagramView['steps'] = [];
|
|
87
|
+
for (const s of content.steps) {
|
|
88
|
+
if (!s || typeof s !== 'object') continue;
|
|
89
|
+
const title = str(s.title);
|
|
90
|
+
const text = str(s.text);
|
|
91
|
+
if (!title && !text) continue;
|
|
92
|
+
const badges: DiagramView['steps'][number]['badges'] = [];
|
|
93
|
+
for (const b of Array.isArray(s.badges) ? s.badges : []) {
|
|
94
|
+
const bt = str(b && typeof b === 'object' ? b.text : b);
|
|
95
|
+
if (bt) badges.push({ text: bt, status: statusOf(b && b.status) });
|
|
96
|
+
}
|
|
97
|
+
steps.push({ title: title || text, text: title ? text : '', status: statusOf(s.status), badges });
|
|
98
|
+
}
|
|
99
|
+
return steps.length < 2 ? null : { title: str(content.title), steps };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A diagram as readable text, the same numbered list Core gives other channels (backend `diagramToText`). */
|
|
103
|
+
export function diagramAsText(d: DiagramView): string {
|
|
104
|
+
const lines = d.title ? [d.title] : [];
|
|
105
|
+
d.steps.forEach((s, i) => {
|
|
106
|
+
const badges = s.badges.length ? ` [${s.badges.map(b => b.text).join(', ')}]` : '';
|
|
107
|
+
lines.push(`${i + 1}. ${s.title}${s.text ? ` — ${s.text}` : ''}${badges}`);
|
|
108
|
+
});
|
|
109
|
+
return lines.join('\n');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A markdown fence around `code`, long enough that a run of backticks inside the code cannot close it early. */
|
|
113
|
+
function fence(code: string, language: string): string {
|
|
114
|
+
const longest = (code.match(/`+/g) || []).reduce((n, run) => Math.max(n, run.length), 0);
|
|
115
|
+
const ticks = '`'.repeat(Math.max(3, longest + 1));
|
|
116
|
+
return `${ticks}${language}\n${code}\n${ticks}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The text of one block, before any "folded" wrapping. Empty when the block holds nothing to draw. */
|
|
120
|
+
function blockText(b: ConversationBlock): string {
|
|
121
|
+
if (b.kind === 'text' || b.kind === 'markdown' || b.kind === 'description') {
|
|
122
|
+
return typeof b.content === 'string' ? clean(b.content) : '';
|
|
123
|
+
}
|
|
124
|
+
if (b.kind === 'code') {
|
|
125
|
+
// Core sends { kind: 'code', content: { content: '...', language: '...' } }; a flat string with a `language` also reads.
|
|
126
|
+
const nested = b.content && typeof b.content === 'object';
|
|
127
|
+
const code = nested ? b.content.content : b.content;
|
|
128
|
+
if (typeof code !== 'string' || !code.trim()) return '';
|
|
129
|
+
const lang = clean(String((nested ? b.content.language : b.language) || '').trim());
|
|
130
|
+
return fence(clean(code), lang && lang.toLowerCase() !== 'auto' ? lang : '');
|
|
131
|
+
}
|
|
132
|
+
if (b.kind === 'table' && b.content && typeof b.content === 'object') {
|
|
133
|
+
const md = renderTableShape(b.content);
|
|
134
|
+
if (md) return md;
|
|
135
|
+
// Fall through to generic JSON rendering if shape is unexpected.
|
|
136
|
+
}
|
|
137
|
+
if (b.kind === 'diagram') {
|
|
138
|
+
const view = diagramView(b.content);
|
|
139
|
+
if (view) return `${DIAGRAM_BLOCK_OPEN}${encodeURIComponent(JSON.stringify(view))}${DIAGRAM_BLOCK_CLOSE}`;
|
|
140
|
+
// Not a diagram the phone can draw: fall through to the generic text below, never a lost block.
|
|
141
|
+
}
|
|
142
|
+
if (b.kind === 'json') {
|
|
143
|
+
// Polisher sometimes wraps tables as kind:'json' when the content has columns/rows — render those as table blocks too.
|
|
144
|
+
const md = renderTableShape(b.content);
|
|
145
|
+
if (md) return md;
|
|
146
|
+
const pretty = JSON.stringify(b.content, null, 2); // undefined when there is nothing to write
|
|
147
|
+
return typeof pretty === 'string' ? fence(clean(pretty), 'json') : '';
|
|
148
|
+
}
|
|
149
|
+
if (b.kind === 'html') {
|
|
150
|
+
return typeof b.content === 'string' ? clean(b.content) : '';
|
|
151
|
+
}
|
|
152
|
+
if (typeof b.content === 'string') return clean(b.content);
|
|
153
|
+
return b.content === undefined ? '' : clean(JSON.stringify(b.content) ?? '');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Extract display text from a conversation turn's blocks. Handles multiple block kinds (text, code, json, markdown, table, html).
|
|
158
|
+
* Every block that holds something is in the result, in order; a block Core marked `meta.display: "folded"` is wrapped in the
|
|
159
|
+
* DETAILS sentinels so the bubble draws it closed.
|
|
160
|
+
*/
|
|
161
|
+
export function blocksToText(blocks?: ConversationBlock[]): string {
|
|
162
|
+
if (!blocks || blocks.length === 0) return '';
|
|
163
|
+
return blocks.map(b => {
|
|
164
|
+
if (!b || typeof b !== 'object') return '';
|
|
165
|
+
const text = blockText(b);
|
|
166
|
+
if (!text) return '';
|
|
167
|
+
// A table or a diagram is drawn as itself whatever the mark says (Core never marks one; either inside a closed row would show as characters).
|
|
168
|
+
if (text.startsWith(TABLE_BLOCK_OPEN) || text.startsWith(DIAGRAM_BLOCK_OPEN)) return text;
|
|
169
|
+
return b.meta?.display === 'folded'
|
|
170
|
+
? `${DETAILS_BLOCK_OPEN}${encodeURIComponent(text)}${DETAILS_BLOCK_CLOSE}`
|
|
171
|
+
: text;
|
|
172
|
+
}).filter(Boolean).join('\n\n');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** One piece of a flat message string, as the bubble draws it: markdown, a table payload, a diagram payload, or a closed "Details" payload. */
|
|
176
|
+
export interface BlockSegment {
|
|
177
|
+
kind: 'md' | 'table' | 'details' | 'diagram';
|
|
178
|
+
/** md: the markdown. table / details / diagram: the still percent-encoded payload between the sentinels. */
|
|
179
|
+
content: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const PAIRS: Array<{ kind: 'table' | 'details' | 'diagram'; open: string; close: string }> = [
|
|
183
|
+
{ kind: 'table', open: TABLE_BLOCK_OPEN, close: TABLE_BLOCK_CLOSE },
|
|
184
|
+
{ kind: 'details', open: DETAILS_BLOCK_OPEN, close: DETAILS_BLOCK_CLOSE },
|
|
185
|
+
{ kind: 'diagram', open: DIAGRAM_BLOCK_OPEN, close: DIAGRAM_BLOCK_CLOSE },
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Split a flat message string on the table, details and diagram sentinels, in order. A string with none is one markdown segment. An
|
|
190
|
+
* unterminated marker is kept as markdown from the marker on, so nothing is lost.
|
|
191
|
+
*/
|
|
192
|
+
export function splitBlockSegments(content: string): BlockSegment[] {
|
|
193
|
+
const segments: BlockSegment[] = [];
|
|
194
|
+
if (!content) return segments;
|
|
195
|
+
let cursor = 0;
|
|
196
|
+
while (cursor < content.length) {
|
|
197
|
+
let found: { index: number; pair: (typeof PAIRS)[number] } | null = null;
|
|
198
|
+
for (const pair of PAIRS) {
|
|
199
|
+
const index = content.indexOf(pair.open, cursor);
|
|
200
|
+
if (index !== -1 && (!found || index < found.index)) found = { index, pair };
|
|
201
|
+
}
|
|
202
|
+
if (!found) {
|
|
203
|
+
segments.push({ kind: 'md', content: content.slice(cursor) });
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
if (found.index > cursor) {
|
|
207
|
+
segments.push({ kind: 'md', content: content.slice(cursor, found.index) });
|
|
208
|
+
}
|
|
209
|
+
const close = content.indexOf(found.pair.close, found.index + found.pair.open.length);
|
|
210
|
+
if (close === -1) {
|
|
211
|
+
// Unterminated marker — keep the rest as markdown so we don't lose data.
|
|
212
|
+
segments.push({ kind: 'md', content: content.slice(found.index) });
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
segments.push({ kind: found.pair.kind, content: content.slice(found.index + found.pair.open.length, close) });
|
|
216
|
+
cursor = close + found.pair.close.length;
|
|
217
|
+
}
|
|
218
|
+
return segments;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** A table payload as readable text: its columns, then one line per row, cells separated by " | ". */
|
|
222
|
+
function tableAsText(payload: string): string {
|
|
223
|
+
try {
|
|
224
|
+
const data = JSON.parse(decodeURIComponent(payload));
|
|
225
|
+
const cols: any[] = Array.isArray(data?.columns) ? data.columns : [];
|
|
226
|
+
const rows: any[] = Array.isArray(data?.rows) ? data.rows : [];
|
|
227
|
+
const cell = (v: any) => (v == null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v));
|
|
228
|
+
const line = (cells: any) => (Array.isArray(cells) ? cells : [cells]).map(cell).join(' | ');
|
|
229
|
+
return [line(cols), ...rows.map(line)].join('\n');
|
|
230
|
+
} catch {
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The flat content of a message as the person would read it: a table is its columns and rows as lines, and a "Details" payload is
|
|
237
|
+
* its text. What long-press copy and a screen reader are given; the raw string holds percent-encoded payloads between private-use
|
|
238
|
+
* characters, which nobody should hear or paste.
|
|
239
|
+
*/
|
|
240
|
+
export function plainTextOf(content: string): string {
|
|
241
|
+
if (!content) return '';
|
|
242
|
+
return splitBlockSegments(content).map(seg => {
|
|
243
|
+
if (seg.kind === 'md') return seg.content;
|
|
244
|
+
if (seg.kind === 'table') return tableAsText(seg.content);
|
|
245
|
+
if (seg.kind === 'diagram') {
|
|
246
|
+
try {
|
|
247
|
+
const view = diagramView(JSON.parse(decodeURIComponent(seg.content)));
|
|
248
|
+
return view ? diagramAsText(view) : '';
|
|
249
|
+
} catch { return ''; }
|
|
250
|
+
}
|
|
251
|
+
try { return decodeURIComponent(seg.content); } catch { return seg.content; }
|
|
252
|
+
}).join('');
|
|
253
|
+
}
|
package/src/chat/store.ts
CHANGED
|
@@ -16,6 +16,8 @@ import { getCoreApiUrl, openCoreWs, getToken, getTenant, getUser, getConfig } fr
|
|
|
16
16
|
import { pluginCommandEmitter } from '../utils/emitter';
|
|
17
17
|
import { focusPlugin } from '../utils/hostIntents';
|
|
18
18
|
import type { OutgoingAttachment } from '../storage/types';
|
|
19
|
+
import { prepareAttachments } from './attachments';
|
|
20
|
+
import { blocksToText } from './blockText';
|
|
19
21
|
import type { Message, MessageAttachment, JobProgress, UIAction } from './types';
|
|
20
22
|
|
|
21
23
|
// ---------------------------------------------------------------------------
|
|
@@ -151,76 +153,6 @@ function isTerminalSnapshot(snap: any): boolean {
|
|
|
151
153
|
return snap?.done === true || isTerminalStatus(snap?.status);
|
|
152
154
|
}
|
|
153
155
|
|
|
154
|
-
/**
|
|
155
|
-
* Sentinel delimiters used to embed structured table data inside a flat
|
|
156
|
-
* message string. UI layers split on these and render the contents with a
|
|
157
|
-
* native table component instead of markdown tables — markdown tables look
|
|
158
|
-
* terrible on narrow phone screens.
|
|
159
|
-
*
|
|
160
|
-
* Unicode Private Use Area chars (U+E100/U+E101) so they never collide with
|
|
161
|
-
* anything the model could legitimately emit.
|
|
162
|
-
*/
|
|
163
|
-
export const TABLE_BLOCK_OPEN = '\uE100';
|
|
164
|
-
export const TABLE_BLOCK_CLOSE = '\uE101';
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Render a `{ columns: [...], rows: [...] }` payload as a sentinel-wrapped
|
|
168
|
-
* JSON marker. Returns null if the shape doesn't match.
|
|
169
|
-
*/
|
|
170
|
-
function renderTableShape(content: any): string | null {
|
|
171
|
-
if (!content || typeof content !== 'object') return null;
|
|
172
|
-
const cols = Array.isArray(content.columns) ? content.columns : null;
|
|
173
|
-
const rows = Array.isArray(content.rows) ? content.rows : null;
|
|
174
|
-
if (!cols || !rows) return null;
|
|
175
|
-
// encodeURIComponent so the JSON can never contain newlines or sentinel chars.
|
|
176
|
-
const payload = encodeURIComponent(JSON.stringify({ columns: cols, rows }));
|
|
177
|
-
return `${TABLE_BLOCK_OPEN}${payload}${TABLE_BLOCK_CLOSE}`;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Extract display text from a conversation turn's assistant blocks.
|
|
182
|
-
* Handles multiple block kinds (text, code, json, markdown, table, html).
|
|
183
|
-
*/
|
|
184
|
-
export function blocksToText(blocks?: Array<{ kind: string; content: any }>): string {
|
|
185
|
-
if (!blocks || blocks.length === 0) return '';
|
|
186
|
-
// Mirror the web renderer: when a human-readable text/markdown block exists in
|
|
187
|
-
// the same turn, drop internal-state json/code blocks (planning state,
|
|
188
|
-
// askUser payloads) — otherwise they render as a raw ```json blob AFTER the
|
|
189
|
-
// real answer. Standalone json/code (no text block, e.g. a table-only turn)
|
|
190
|
-
// still renders as before.
|
|
191
|
-
const hasTextBlock = blocks.some(
|
|
192
|
-
b => b.kind === 'text' || b.kind === 'markdown' || b.kind === 'description',
|
|
193
|
-
);
|
|
194
|
-
return blocks.map(b => {
|
|
195
|
-
if ((b.kind === 'json' || b.kind === 'code') && hasTextBlock) return '';
|
|
196
|
-
if (b.kind === 'text' || b.kind === 'markdown' || b.kind === 'description') {
|
|
197
|
-
return typeof b.content === 'string' ? b.content : '';
|
|
198
|
-
}
|
|
199
|
-
if (b.kind === 'code') {
|
|
200
|
-
const code = typeof b.content === 'object' ? b.content.content : b.content;
|
|
201
|
-
return `\`\`\`\n${code}\n\`\`\``;
|
|
202
|
-
}
|
|
203
|
-
if (b.kind === 'table' && b.content && typeof b.content === 'object') {
|
|
204
|
-
const md = renderTableShape(b.content);
|
|
205
|
-
if (md) return md;
|
|
206
|
-
// Fall through to generic JSON rendering if shape is unexpected.
|
|
207
|
-
}
|
|
208
|
-
if (b.kind === 'json') {
|
|
209
|
-
// Skip pendingQuestion state objects — internal sys.askUser data
|
|
210
|
-
if (b.content && typeof b.content === 'object' && 'pendingQuestion' in b.content) return '';
|
|
211
|
-
// Polisher sometimes wraps tables as kind:'json' when the content has
|
|
212
|
-
// columns/rows — render those as table blocks too.
|
|
213
|
-
const md = renderTableShape(b.content);
|
|
214
|
-
if (md) return md;
|
|
215
|
-
return '```json\n' + JSON.stringify(b.content, null, 2) + '\n```';
|
|
216
|
-
}
|
|
217
|
-
if (b.kind === 'html') {
|
|
218
|
-
return typeof b.content === 'string' ? b.content : '';
|
|
219
|
-
}
|
|
220
|
-
return typeof b.content === 'string' ? b.content : JSON.stringify(b.content);
|
|
221
|
-
}).filter(Boolean).join('\n\n');
|
|
222
|
-
}
|
|
223
|
-
|
|
224
156
|
/** Extract image/file attachments from conversation blocks */
|
|
225
157
|
function blocksToAttachments(blocks?: Array<{ kind: string; hash?: string; url?: string; mimeType?: string; name?: string }>): MessageAttachment[] | undefined {
|
|
226
158
|
if (!blocks || blocks.length === 0) return undefined;
|
|
@@ -494,8 +426,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
|
|
494
426
|
attachments: attachments.map(a => ({
|
|
495
427
|
mimeType: a.mimeType,
|
|
496
428
|
name: a.name,
|
|
497
|
-
|
|
498
|
-
|
|
429
|
+
// An image on the phone previews from its own file; an inline one from its base64.
|
|
430
|
+
previewUri: !a.mimeType.startsWith('image/') ? undefined
|
|
431
|
+
: a.data !== undefined ? `data:${a.mimeType};base64,${a.data}` : a.uri,
|
|
432
|
+
})),
|
|
433
|
+
// A file on the phone is uploaded before the message is posted: the bubble shows how far along it is.
|
|
434
|
+
...(attachments.some(a => a.uri && a.data === undefined) ? { uploadProgress: 0 } : {}),
|
|
499
435
|
} : {}),
|
|
500
436
|
};
|
|
501
437
|
|
|
@@ -518,7 +454,16 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
|
|
518
454
|
},
|
|
519
455
|
};
|
|
520
456
|
if (skillSlug) body.skillSlug = skillSlug;
|
|
521
|
-
if (attachments?.length)
|
|
457
|
+
if (attachments?.length) {
|
|
458
|
+
// Files on the phone go up first (raw body, POST /api/artifacts); the chat names them by hash.
|
|
459
|
+
let shown = 0;
|
|
460
|
+
body.attachments = await prepareAttachments(attachments, f => {
|
|
461
|
+
if (f - shown < 0.01 && f < 1) return;
|
|
462
|
+
shown = f;
|
|
463
|
+
set(s => ({ messages: s.messages.map(m => (m.id === userMsg.id && m.uploadProgress !== undefined ? { ...m, uploadProgress: f } : m)) }));
|
|
464
|
+
});
|
|
465
|
+
set(s => ({ messages: s.messages.map(m => (m.id === userMsg.id ? { ...m, uploadProgress: undefined } : m)) }));
|
|
466
|
+
}
|
|
522
467
|
|
|
523
468
|
const res = await fetch(`${getCoreApiUrl()}/api/chat`, {
|
|
524
469
|
method: 'POST',
|
|
@@ -610,7 +555,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
|
|
610
555
|
streamJob(jobId, getTenant(), getToken());
|
|
611
556
|
} catch (err: any) {
|
|
612
557
|
set(s => ({
|
|
613
|
-
messages: [...s.messages, {
|
|
558
|
+
messages: [...s.messages.map(m => (m.id === userMsg.id && m.uploadProgress !== undefined ? { ...m, uploadProgress: undefined } : m)), {
|
|
614
559
|
id: `error-${Date.now()}`,
|
|
615
560
|
role: 'system' as const,
|
|
616
561
|
content: `Error: ${err.message}`,
|
package/src/chat/types.ts
CHANGED
|
@@ -72,6 +72,8 @@ export interface Message {
|
|
|
72
72
|
skillSlug?: string;
|
|
73
73
|
/** Attached images/files */
|
|
74
74
|
attachments?: MessageAttachment[];
|
|
75
|
+
/** 0..1 while this message's files are still uploading; absent once they are stored and the message is posted. */
|
|
76
|
+
uploadProgress?: number;
|
|
75
77
|
/** UI Action chips beneath assistant bubbles */
|
|
76
78
|
actions?: UIAction[];
|
|
77
79
|
/** Chain id from /api/conversation — used to attach live progress to the
|
package/src/config.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* too, and a second tenant/backend inside one app is not a supported v1 case.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import type { AttachmentCache } from './storage/types';
|
|
11
|
+
import type { AttachmentCache, FileUploader } from './storage/types';
|
|
12
12
|
import { tenantFromToken } from './jwt';
|
|
13
13
|
|
|
14
14
|
/** Result of a host-driven token refresh. Mirrors the semantics the API
|
|
@@ -74,6 +74,9 @@ export interface AdasConfig {
|
|
|
74
74
|
* survive app restarts. No-op when omitted (previews live in memory for
|
|
75
75
|
* the session only). @ateam-ai-mobile/ui ships an Expo/RN implementation. */
|
|
76
76
|
attachmentCache?: AttachmentCache;
|
|
77
|
+
/** Moves an attachment's bytes for `sendMessage` (raw upload of a file `uri`). Required to attach a file by `uri`;
|
|
78
|
+
* without it only base64 `data` attachments can be sent. `@ateam-ai-mobile/expo` ships `expoFileUploader`. */
|
|
79
|
+
fileUploader?: FileUploader;
|
|
77
80
|
/** Called by `switchTenant()` with the freshly-minted session for the
|
|
78
81
|
* target tenant. The host MUST persist it here (store the new JWT so
|
|
79
82
|
* tokenProvider.getToken() returns it, and update the active tenant) —
|
package/src/index.ts
CHANGED
|
@@ -146,10 +146,22 @@ export {
|
|
|
146
146
|
setClientDeclarationProvider,
|
|
147
147
|
useChatStore,
|
|
148
148
|
closeChatStream,
|
|
149
|
+
} from './chat/store';
|
|
150
|
+
export {
|
|
149
151
|
blocksToText,
|
|
152
|
+
splitBlockSegments,
|
|
153
|
+
plainTextOf,
|
|
150
154
|
TABLE_BLOCK_OPEN,
|
|
151
155
|
TABLE_BLOCK_CLOSE,
|
|
152
|
-
|
|
156
|
+
DETAILS_BLOCK_OPEN,
|
|
157
|
+
DETAILS_BLOCK_CLOSE,
|
|
158
|
+
DIAGRAM_BLOCK_OPEN,
|
|
159
|
+
DIAGRAM_BLOCK_CLOSE,
|
|
160
|
+
DIAGRAM_STATUSES,
|
|
161
|
+
diagramView,
|
|
162
|
+
diagramAsText,
|
|
163
|
+
} from './chat/blockText';
|
|
164
|
+
export type { ConversationBlock, BlockSegment, DiagramView, DiagramStatus } from './chat/blockText';
|
|
153
165
|
export type {
|
|
154
166
|
Message,
|
|
155
167
|
MessageAttachment,
|
|
@@ -163,7 +175,13 @@ export type {
|
|
|
163
175
|
AttachmentCache,
|
|
164
176
|
AttachmentPreview,
|
|
165
177
|
OutgoingAttachment,
|
|
178
|
+
UploadedArtifact,
|
|
179
|
+
FileUploader,
|
|
180
|
+
FileUploadRequest,
|
|
166
181
|
} from './storage/types';
|
|
182
|
+
export { MAX_ATTACHMENT_BYTES, MAX_INLINE_ATTACHMENT_BYTES } from './storage/types';
|
|
183
|
+
export { prepareAttachments } from './chat/attachments';
|
|
184
|
+
export type { WireAttachment } from './chat/attachments';
|
|
167
185
|
|
|
168
186
|
// Plugin command bus
|
|
169
187
|
export { pluginCommandEmitter, PluginCommandEmitter } from './utils/emitter';
|