@ateam-ai-mobile/core 0.1.26 → 0.1.28
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.js +21 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +21 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/acs/ws.ts +2 -3
- package/src/chat/store.ts +19 -12
- package/src/config.ts +15 -0
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.28",
|
|
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",
|
package/src/acs/ws.ts
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* the WS is healthy. This file is the realtime cherry on top.
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
-
import {
|
|
27
|
+
import { openCoreWs } from '../config';
|
|
28
28
|
import { useWidgetsStore } from './widgetsStore';
|
|
29
29
|
import { useHomeActionsStore } from './homeActionsStore';
|
|
30
30
|
|
|
@@ -133,9 +133,8 @@ function openSocket() {
|
|
|
133
133
|
if (_ws) return;
|
|
134
134
|
if (!_token || !_tenant) return;
|
|
135
135
|
|
|
136
|
-
const url = `${getCoreWsUrl()}/ws?token=${encodeURIComponent(_token)}&tenant=${encodeURIComponent(_tenant)}`;
|
|
137
136
|
console.log(`[acsWs] opening`);
|
|
138
|
-
const ws =
|
|
137
|
+
const ws = openCoreWs(_tenant, _token);
|
|
139
138
|
_ws = ws;
|
|
140
139
|
|
|
141
140
|
// IDENTITY GUARD. `connectAcsWs()` (tenant switch / re-auth) and reconnects
|
package/src/chat/store.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* from the host via initAdas().
|
|
13
13
|
*/
|
|
14
14
|
import { create } from 'zustand';
|
|
15
|
-
import { getCoreApiUrl,
|
|
15
|
+
import { getCoreApiUrl, openCoreWs, getToken, getTenant, getUser, getConfig } from '../config';
|
|
16
16
|
import { pluginCommandEmitter } from '../utils/emitter';
|
|
17
17
|
import type { OutgoingAttachment } from '../storage/types';
|
|
18
18
|
import type { Message, MessageAttachment, JobProgress, UIAction } from './types';
|
|
@@ -744,8 +744,7 @@ let _currentJobId: string | null = null;
|
|
|
744
744
|
function ensureWs(tenant: string, token: string | null): WebSocket {
|
|
745
745
|
if (_ws && _wsReady) return _ws;
|
|
746
746
|
|
|
747
|
-
const
|
|
748
|
-
const ws = new WebSocket(wsUrl);
|
|
747
|
+
const ws = openCoreWs(tenant, token);
|
|
749
748
|
_ws = ws;
|
|
750
749
|
_wsReady = false;
|
|
751
750
|
|
|
@@ -953,10 +952,26 @@ async function fetchTurnResult(jobId: string, jobSnap: any) {
|
|
|
953
952
|
// Polling fallback (when WS is unavailable)
|
|
954
953
|
// ---------------------------------------------------------------------------
|
|
955
954
|
|
|
955
|
+
/** Is this job's bubble still on screen waiting for its answer? (A chat cleared or a job that ended another way stops the poll.) */
|
|
956
|
+
function isWaitingOnScreen(jobId: string): boolean {
|
|
957
|
+
return useChatStore.getState().messages.some(m => {
|
|
958
|
+
const p = m.jobProgress;
|
|
959
|
+
return p?.jobId === jobId && p.status !== 'done' && p.status !== 'error' && p.status !== 'paused';
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/** Wait between two reads: 1.5 s, then 5 s once the job has run a few minutes. */
|
|
964
|
+
function pollDelayMs(attempt: number): number {
|
|
965
|
+
return attempt < 120 ? 1500 : 5000;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// No give-up time: a long job is still running, and saying "timed out" on a job that then finishes is a wrong statement. The poll ends
|
|
969
|
+
// when the job ends, when Core no longer knows it (404), or when its bubble is no longer waiting on screen.
|
|
956
970
|
async function pollJob(jobId: string, token: string | null) {
|
|
957
971
|
const { updateJobProgress } = useChatStore.getState();
|
|
958
972
|
|
|
959
|
-
for (let i = 0;
|
|
973
|
+
for (let i = 0; isWaitingOnScreen(jobId); i++) {
|
|
974
|
+
if (i > 0) await new Promise(r => setTimeout(r, pollDelayMs(i)));
|
|
960
975
|
try {
|
|
961
976
|
const res = await fetch(`${getCoreApiUrl()}/api/job/${jobId}/status`, {
|
|
962
977
|
headers: {
|
|
@@ -985,13 +1000,5 @@ async function pollJob(jobId: string, token: string | null) {
|
|
|
985
1000
|
return;
|
|
986
1001
|
}
|
|
987
1002
|
} catch {}
|
|
988
|
-
|
|
989
|
-
await new Promise(r => setTimeout(r, 1500));
|
|
990
1003
|
}
|
|
991
|
-
|
|
992
|
-
// Timeout
|
|
993
|
-
updateJobProgress(jobId, {
|
|
994
|
-
status: 'error',
|
|
995
|
-
resultMessage: 'Job timed out. Please try again.',
|
|
996
|
-
});
|
|
997
1004
|
}
|
package/src/config.ts
CHANGED
|
@@ -120,6 +120,21 @@ export function getCoreWsUrl(): string {
|
|
|
120
120
|
return getCoreApiUrl().replace(/^http/, 'ws');
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
// Core's /ws protocol. The sign-in token travels as the "adas.bearer.<token>"
|
|
124
|
+
// subprotocol next to "adas.v1" (the one the server selects, so the token is
|
|
125
|
+
// never echoed), not in the URL: a URL is written to every access log it passes
|
|
126
|
+
// (nginx, Core's request log, Cloudflare). Core: apps/backend/utils/urlToken.js.
|
|
127
|
+
export const CORE_WS_PROTOCOL = 'adas.v1';
|
|
128
|
+
const CORE_WS_BEARER_PREFIX = 'adas.bearer.';
|
|
129
|
+
|
|
130
|
+
/** Open Core's /ws for a tenant, with the token in the subprotocol, never the URL. */
|
|
131
|
+
export function openCoreWs(tenant: string, token: string | null): WebSocket {
|
|
132
|
+
const url = `${getCoreWsUrl()}/ws?tenant=${encodeURIComponent(tenant)}`;
|
|
133
|
+
const protocols = [CORE_WS_PROTOCOL];
|
|
134
|
+
if (token) protocols.push(CORE_WS_BEARER_PREFIX + token);
|
|
135
|
+
return new WebSocket(url, protocols);
|
|
136
|
+
}
|
|
137
|
+
|
|
123
138
|
export function getVoiceApiUrl(): string {
|
|
124
139
|
const url = getConfig().voiceUrl;
|
|
125
140
|
if (!url) throw new Error('[adas] voiceUrl not configured');
|