@rise-x/apps-sdk 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +20 -11
- package/README.md +112 -1
- package/build/connectors/agents.d.ts +5 -0
- package/build/connectors/agents.js +40 -0
- package/build/connectors/assets.d.ts +5 -2
- package/build/connectors/assets.js +23 -6
- package/build/connectors/index.d.ts +4 -3
- package/build/connectors/index.js +3 -1
- package/build/connectors/project.d.ts +7 -0
- package/build/connectors/project.js +58 -0
- package/build/connectors/types.d.ts +16 -0
- package/build/connectors/types.js +22 -8
- package/build/connectors/work.d.ts +38 -0
- package/build/connectors/work.js +125 -9
- package/build/dependencies/bound.d.ts +2 -2
- package/build/dictation/index.d.ts +34 -0
- package/build/dictation/index.js +205 -0
- package/build/query/assets.d.ts +4 -4
- package/build/query/assets.js +34 -20
- package/build/query/shared.d.ts +16 -1
- package/build/query/shared.js +20 -2
- package/build/query/work.js +25 -17
- package/build/ui/demo.html +166 -166
- package/build/ui/standalone.js +12 -12
- package/dictation/package.json +8 -0
- package/package.json +6 -1
|
@@ -11,8 +11,28 @@ export interface SubmitWorkArgs {
|
|
|
11
11
|
workId: string;
|
|
12
12
|
actionName: string;
|
|
13
13
|
stepName?: string;
|
|
14
|
+
to?: WorkInvitee[];
|
|
15
|
+
cc?: WorkInvitee[];
|
|
16
|
+
invitation?: WorkInvitation;
|
|
14
17
|
signal?: AbortSignal;
|
|
15
18
|
}
|
|
19
|
+
export interface WorkInvitee {
|
|
20
|
+
email?: string;
|
|
21
|
+
id?: string;
|
|
22
|
+
name?: string;
|
|
23
|
+
partyName?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface WorkInvitation {
|
|
26
|
+
destinations: WorkInvitationDestination[];
|
|
27
|
+
}
|
|
28
|
+
export interface WorkInvitationDestination {
|
|
29
|
+
targetStep?: string;
|
|
30
|
+
targetStepName?: string;
|
|
31
|
+
to?: WorkInvitee[];
|
|
32
|
+
cc?: WorkInvitee[];
|
|
33
|
+
message?: string;
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
}
|
|
16
36
|
export type WorkPatchOperation = "Set" | "Push" | "Pull" | "Unset" | "AddToSet" | "Merge";
|
|
17
37
|
export interface WorkDataPatch {
|
|
18
38
|
originId: string;
|
|
@@ -53,6 +73,14 @@ export type SearchWorkArgs = SearchArgs & {
|
|
|
53
73
|
filter: SearchFilter;
|
|
54
74
|
};
|
|
55
75
|
export type WorkSearchPage = SearchPage<WorkSummary>;
|
|
76
|
+
export interface GetWorkSummaryOptions {
|
|
77
|
+
/**
|
|
78
|
+
* The projection to rebuild, as a search takes it: `data.<section>.<field>` paths select what
|
|
79
|
+
* lands in the row's `data`. Without any, `data` is left off and only the work itself is read.
|
|
80
|
+
*/
|
|
81
|
+
fields?: readonly string[];
|
|
82
|
+
signal?: AbortSignal;
|
|
83
|
+
}
|
|
56
84
|
export interface ListRelatedWorkArgs {
|
|
57
85
|
workId: string;
|
|
58
86
|
relationships: string[];
|
|
@@ -72,6 +100,15 @@ export interface WorkConnector {
|
|
|
72
100
|
get(workId: string, opts?: {
|
|
73
101
|
signal?: AbortSignal;
|
|
74
102
|
}): Promise<WorkDetail | null>;
|
|
103
|
+
/**
|
|
104
|
+
* One work as a search row, read from the platform now: the work itself, plus its data document
|
|
105
|
+
* when `fields` names any `data.*` path. A search is cached on the server for minutes, so this
|
|
106
|
+
* is how an app brings one row of a list up to date after a change it was told about, without
|
|
107
|
+
* reading the list again. `data` carries only the projected paths (see `projectDataPaths`),
|
|
108
|
+
* like a search row's. Resolves `null` when the work does not exist; never answers from the
|
|
109
|
+
* offline cache.
|
|
110
|
+
*/
|
|
111
|
+
getSummary(workId: string, opts?: GetWorkSummaryOptions): Promise<WorkSummary | null>;
|
|
75
112
|
getData(workId: string, opts?: {
|
|
76
113
|
path?: string;
|
|
77
114
|
signal?: AbortSignal;
|
|
@@ -98,5 +135,6 @@ export interface WorkConnector {
|
|
|
98
135
|
}
|
|
99
136
|
export declare const resolveFlowRef: (flow: FlowRef | null | undefined) => string;
|
|
100
137
|
export declare function buildAdditionalProperties(properties?: Record<string, string>): unknown[];
|
|
138
|
+
export declare function readWorkBack(deps: ConnectorDeps, workId: string, written: WorkDetail, signal?: AbortSignal): Promise<WorkDetail>;
|
|
101
139
|
export declare function createWorkConnector(deps?: ConnectorDeps): WorkConnector;
|
|
102
140
|
export declare const work: WorkConnector;
|
package/build/connectors/work.js
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.work = exports.resolveFlowRef = void 0;
|
|
4
4
|
exports.buildAdditionalProperties = buildAdditionalProperties;
|
|
5
|
+
exports.readWorkBack = readWorkBack;
|
|
5
6
|
exports.createWorkConnector = createWorkConnector;
|
|
6
7
|
const _internal_1 = require("../_internal");
|
|
7
8
|
const errors_1 = require("./errors");
|
|
9
|
+
const project_1 = require("./project");
|
|
8
10
|
const readPolicy_1 = require("./readPolicy");
|
|
9
11
|
const search_1 = require("./search");
|
|
10
12
|
const types_1 = require("./types");
|
|
@@ -19,6 +21,8 @@ const PATCH_OPERATION_CODES = {
|
|
|
19
21
|
};
|
|
20
22
|
// Wire code of the platform's ResourceType enum for work items.
|
|
21
23
|
const RESOURCE_TYPE_WORK = 2;
|
|
24
|
+
// The work's actions submit() would run for `actionName` (the shell's eventName ?? name rule).
|
|
25
|
+
const actionsNamed = (detail, actionName) => (detail?.actions ?? []).filter((a) => (a.eventName ?? a.name) === actionName);
|
|
22
26
|
const DEFAULT_PAGE_SIZE = 50;
|
|
23
27
|
// Exported so the query layer's cache keys can resolve a FlowRef the same
|
|
24
28
|
// way the request body does (see query/work.ts) — an object ref and its bare
|
|
@@ -39,22 +43,53 @@ function buildAdditionalProperties(properties) {
|
|
|
39
43
|
isExpandedProperty: false,
|
|
40
44
|
}));
|
|
41
45
|
}
|
|
46
|
+
// v3 answers carry actions without event names and a step state as `status`, so writes that
|
|
47
|
+
// answer from v3 read the item from v4. The write already happened: a failed read must not hide
|
|
48
|
+
// it. Network only, since a cached copy predates the write.
|
|
49
|
+
async function readWorkBack(deps, workId, written, signal) {
|
|
50
|
+
let data;
|
|
51
|
+
try {
|
|
52
|
+
({ data } = await deps
|
|
53
|
+
.shell()
|
|
54
|
+
.getApiV4("work")
|
|
55
|
+
.get(`/api/v4/work/${workId}`, {
|
|
56
|
+
signal,
|
|
57
|
+
timeout: readPolicy_1.READ_TIMEOUT_MS,
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// A host with no v4 work client counts as a failed read, not a failed write.
|
|
62
|
+
}
|
|
63
|
+
const detail = (0, types_1.mapWorkDetail)(data ?? {});
|
|
64
|
+
return detail.id
|
|
65
|
+
? detail
|
|
66
|
+
: {
|
|
67
|
+
...written,
|
|
68
|
+
id: workId,
|
|
69
|
+
displayName: written.displayName || workId,
|
|
70
|
+
status: undefined,
|
|
71
|
+
actions: [],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
42
74
|
function createWorkConnector(deps = { shell: _internal_1.requireShell }) {
|
|
43
75
|
const workApi = () => deps.shell().getApiV4("work");
|
|
76
|
+
const readBack = (workId, written, signal) => readWorkBack(deps, workId, written, signal);
|
|
44
77
|
// start/submit go through the v3 flow engine — the same endpoints the shell
|
|
45
78
|
// uses. The v4 `work/submit/*` routes only ever existed on the unreleased
|
|
46
79
|
// flow-engine branch and 404 on deployed APIs.
|
|
47
80
|
const start = async (args) => {
|
|
81
|
+
let created;
|
|
48
82
|
try {
|
|
49
83
|
const { data } = await deps
|
|
50
84
|
.shell()
|
|
51
85
|
.getApi()
|
|
52
86
|
.post(`/api/v3/flow/${args.flowId}/startAt`, args.data ?? {}, { params: { stepName: args.stepName }, signal: args.signal });
|
|
53
|
-
|
|
87
|
+
created = (0, types_1.mapWorkDetail)(data ?? {});
|
|
54
88
|
}
|
|
55
89
|
catch (e) {
|
|
56
90
|
throw (0, errors_1.toConnectorError)(e);
|
|
57
91
|
}
|
|
92
|
+
return readBack(created.id, created, args.signal);
|
|
58
93
|
};
|
|
59
94
|
const get = (workId, opts = {}) => (0, readPolicy_1.resolveRead)({
|
|
60
95
|
shell: deps.shell,
|
|
@@ -69,6 +104,47 @@ function createWorkConnector(deps = { shell: _internal_1.requireShell }) {
|
|
|
69
104
|
return poco ? (0, types_1.mapWorkDetail)(poco) : null;
|
|
70
105
|
},
|
|
71
106
|
});
|
|
107
|
+
const getSummary = async (workId, opts = {}) => {
|
|
108
|
+
const fields = opts.fields ?? [];
|
|
109
|
+
const wholeDocument = fields.includes("data");
|
|
110
|
+
const paths = fields
|
|
111
|
+
.filter((f) => f.startsWith("data."))
|
|
112
|
+
.map((f) => f.slice("data.".length));
|
|
113
|
+
const wantsData = wholeDocument || paths.length > 0;
|
|
114
|
+
const [poco, doc] = await Promise.allSettled([
|
|
115
|
+
workApi().get(`/api/v4/work/${workId}`, {
|
|
116
|
+
signal: opts.signal,
|
|
117
|
+
}),
|
|
118
|
+
wantsData
|
|
119
|
+
? workApi().get(`/api/v4/work/${workId}/data`, {
|
|
120
|
+
signal: opts.signal,
|
|
121
|
+
})
|
|
122
|
+
: Promise.resolve(null),
|
|
123
|
+
]);
|
|
124
|
+
if (poco.status === "rejected") {
|
|
125
|
+
const failure = (0, errors_1.toConnectorError)(poco.reason);
|
|
126
|
+
if (failure.code === "NOT_FOUND")
|
|
127
|
+
return null;
|
|
128
|
+
throw failure;
|
|
129
|
+
}
|
|
130
|
+
// A work with no data document yet reads as an empty one.
|
|
131
|
+
const dataFailure = doc.status === "rejected" ? (0, errors_1.toConnectorError)(doc.reason) : null;
|
|
132
|
+
if (dataFailure && dataFailure.code !== "NOT_FOUND")
|
|
133
|
+
throw dataFailure;
|
|
134
|
+
const document = doc.status === "fulfilled" ? doc.value?.data ?? null : null;
|
|
135
|
+
const raw = poco.value.data ?? {};
|
|
136
|
+
return (0, types_1.mapWorkSummary)({
|
|
137
|
+
...raw,
|
|
138
|
+
// The v4 work names these differently to a search row.
|
|
139
|
+
workCode: raw.workCode ?? raw.code,
|
|
140
|
+
flowState: raw.flowState ?? raw.state,
|
|
141
|
+
data: !wantsData
|
|
142
|
+
? undefined
|
|
143
|
+
: wholeDocument
|
|
144
|
+
? document ?? {}
|
|
145
|
+
: (0, project_1.projectDataPaths)(document, paths),
|
|
146
|
+
});
|
|
147
|
+
};
|
|
72
148
|
// `async` so the path guard below rejects rather than throwing synchronously.
|
|
73
149
|
const getData = async (workId, opts = {}) => {
|
|
74
150
|
// Rejected before a source is chosen: resolved locally `..` yields an array, the endpoint a scalar.
|
|
@@ -121,12 +197,9 @@ function createWorkConnector(deps = { shell: _internal_1.requireShell }) {
|
|
|
121
197
|
};
|
|
122
198
|
// Without the action set's name, `Submit` matches the step's autosubmit event: a 200 that moves
|
|
123
199
|
// nothing. Any miss sends no name, as before, so a call that worked without one still does.
|
|
124
|
-
const resolveStepName = async (args) => {
|
|
200
|
+
const resolveStepName = async (args, detail) => {
|
|
125
201
|
try {
|
|
126
|
-
const
|
|
127
|
-
const actionSetIds = new Set((detail?.actions ?? [])
|
|
128
|
-
.filter((a) => (a.eventName ?? a.name) === args.actionName)
|
|
129
|
-
.map((a) => a.actionSetId));
|
|
202
|
+
const actionSetIds = new Set(actionsNamed(detail, args.actionName).map((a) => a.actionSetId));
|
|
130
203
|
const [actionSetId] = actionSetIds;
|
|
131
204
|
if (!detail?.flowId || actionSetIds.size !== 1 || !actionSetId) {
|
|
132
205
|
return undefined;
|
|
@@ -147,18 +220,60 @@ function createWorkConnector(deps = { shell: _internal_1.requireShell }) {
|
|
|
147
220
|
return undefined;
|
|
148
221
|
}
|
|
149
222
|
};
|
|
223
|
+
// The platform merges a destination's to/cc into its own default for the step it names, and
|
|
224
|
+
// into the one at the same position when none matches. An action that lists no destination
|
|
225
|
+
// gets one unnamed entry, which the platform applies to its first.
|
|
226
|
+
const buildInvitation = (args, detail) => {
|
|
227
|
+
if (args.invitation)
|
|
228
|
+
return args.invitation;
|
|
229
|
+
if (!args.to?.length && !args.cc?.length)
|
|
230
|
+
return undefined;
|
|
231
|
+
const targets = actionsNamed(detail, args.actionName).flatMap((a) => a.destinations);
|
|
232
|
+
const steps = targets.length
|
|
233
|
+
? targets.map((d) => d.targetStep)
|
|
234
|
+
: [undefined];
|
|
235
|
+
return {
|
|
236
|
+
destinations: steps.map((targetStep) => ({
|
|
237
|
+
targetStep,
|
|
238
|
+
targetStepName: targetStep,
|
|
239
|
+
to: args.to ?? [],
|
|
240
|
+
cc: args.cc ?? [],
|
|
241
|
+
})),
|
|
242
|
+
};
|
|
243
|
+
};
|
|
150
244
|
const submit = async (args) => {
|
|
245
|
+
if (args.invitation && (args.to?.length || args.cc?.length)) {
|
|
246
|
+
throw new errors_1.ConnectorError("INVALID_ARG", "work.submit takes `invitation` or `to`/`cc`, not both.");
|
|
247
|
+
}
|
|
248
|
+
let submitted;
|
|
151
249
|
try {
|
|
152
|
-
const
|
|
250
|
+
const sendsInvitees = !args.invitation && !!(args.to?.length || args.cc?.length);
|
|
251
|
+
const needsWorkRead = !args.stepName || sendsInvitees;
|
|
252
|
+
let detail = null;
|
|
253
|
+
if (needsWorkRead) {
|
|
254
|
+
// A failed read only costs the step-name lookup, but invitees sent without the action's
|
|
255
|
+
// destinations would reach its first destination only: reject instead.
|
|
256
|
+
detail = await get(args.workId, { signal: args.signal }).catch((e) => {
|
|
257
|
+
if (sendsInvitees)
|
|
258
|
+
throw e;
|
|
259
|
+
return null;
|
|
260
|
+
});
|
|
261
|
+
if (!detail && sendsInvitees) {
|
|
262
|
+
throw new errors_1.ConnectorError("NOT_FOUND", `work.submit could not read work ${args.workId} to address its to/cc invitees.`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const stepName = args.stepName ?? (await resolveStepName(args, detail));
|
|
266
|
+
const invitation = buildInvitation(args, detail);
|
|
153
267
|
const { data } = await deps
|
|
154
268
|
.shell()
|
|
155
269
|
.getApi()
|
|
156
|
-
.post(`/api/v3/flow/submit/${args.workId}`, { stepName }, { params: { eventName: args.actionName }, signal: args.signal });
|
|
157
|
-
|
|
270
|
+
.post(`/api/v3/flow/submit/${args.workId}`, invitation ? { stepName, invitation } : { stepName }, { params: { eventName: args.actionName }, signal: args.signal });
|
|
271
|
+
submitted = (0, types_1.mapWorkDetail)(data ?? {});
|
|
158
272
|
}
|
|
159
273
|
catch (e) {
|
|
160
274
|
throw (0, errors_1.toConnectorError)(e);
|
|
161
275
|
}
|
|
276
|
+
return readBack(args.workId, submitted, args.signal);
|
|
162
277
|
};
|
|
163
278
|
const del = async (workId, opts = {}) => {
|
|
164
279
|
try {
|
|
@@ -274,6 +389,7 @@ function createWorkConnector(deps = { shell: _internal_1.requireShell }) {
|
|
|
274
389
|
return {
|
|
275
390
|
start,
|
|
276
391
|
get,
|
|
392
|
+
getSummary,
|
|
277
393
|
getData,
|
|
278
394
|
getMyAccess,
|
|
279
395
|
patchData,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AgentChatSession, type AgentsConnector, type CreateAgentChatArgs, type ListChatsArgs, type RunAgentArgs } from "../connectors/agents";
|
|
2
|
-
import { type AssetRowPage, type AssetSearchPage, type AssetsConnector, type CreateAssetArgs, type IterateAssetRowsArgs, type ListAssetRowsArgs, type QuickSearchAssetsArgs } from "../connectors/assets";
|
|
2
|
+
import { type AssetDraft, type AssetRowPage, type AssetSearchPage, type AssetsConnector, type CreateAssetArgs, type IterateAssetRowsArgs, type ListAssetRowsArgs, type QuickSearchAssetsArgs } from "../connectors/assets";
|
|
3
3
|
import { type FlowsConnector } from "../connectors/flows";
|
|
4
4
|
import { type IntegrationsConnector, type InvokeIntegrationEndpointArgs } from "../connectors/integrations";
|
|
5
5
|
import type { SearchArgs } from "../connectors/search";
|
|
@@ -25,7 +25,7 @@ export interface BoundAssetsSurface {
|
|
|
25
25
|
iterate(args?: Omit<IterateAssetRowsArgs, "type">): AsyncIterable<AssetSummary>;
|
|
26
26
|
search(args?: SearchArgs): Promise<AssetSearchPage>;
|
|
27
27
|
quickSearch(args?: Omit<QuickSearchAssetsArgs, "typeOriginId">): Promise<AssetSummary[]>;
|
|
28
|
-
create(args?: Omit<CreateAssetArgs, "type">): Promise<
|
|
28
|
+
create(args?: Omit<CreateAssetArgs, "type">): Promise<AssetDraft>;
|
|
29
29
|
}
|
|
30
30
|
export interface BoundAgentSurface {
|
|
31
31
|
get(opts?: {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** What the hook is doing right now. `idle` includes the moment the browser is asking for the microphone. */
|
|
2
|
+
export type DictationState = "idle" | "recording" | "transcribing";
|
|
3
|
+
/**
|
|
4
|
+
* Why the last attempt failed. `denied`: the user or the browser refused microphone access, so
|
|
5
|
+
* only a change to the site's permissions can fix it. `failed`: anything else (no microphone, a
|
|
6
|
+
* busy device, the transcription request failing).
|
|
7
|
+
*/
|
|
8
|
+
export type DictationError = "denied" | "failed";
|
|
9
|
+
export interface UseDictationOptions {
|
|
10
|
+
/** ISO-639-1 code such as `"en"`. Omit it and the language is detected from the audio. */
|
|
11
|
+
language?: string;
|
|
12
|
+
/** Called once per clip with the transcript, never with an empty string. */
|
|
13
|
+
onText?: (text: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export interface UseDictationResult {
|
|
16
|
+
/**
|
|
17
|
+
* `false` when the browser cannot record audio, or the host offers no AI. Hide the mic button
|
|
18
|
+
* then: `start()` does nothing.
|
|
19
|
+
*/
|
|
20
|
+
supported: boolean;
|
|
21
|
+
state: DictationState;
|
|
22
|
+
/** Set when an attempt fails; cleared by the next `start()` or by `cancel()`. */
|
|
23
|
+
error: DictationError | null;
|
|
24
|
+
/**
|
|
25
|
+
* Asks for the default microphone and starts recording. Resolves once recording has begun or
|
|
26
|
+
* failed (see `error`), and never rejects. Does nothing while recording or transcribing.
|
|
27
|
+
*/
|
|
28
|
+
start(): Promise<void>;
|
|
29
|
+
/** Ends the recording and transcribes it. A no-op unless recording. */
|
|
30
|
+
stop(): void;
|
|
31
|
+
/** Ends the recording and discards it, or abandons a transcription in flight. `onText` is not called. */
|
|
32
|
+
cancel(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function useDictation(options?: UseDictationOptions): UseDictationResult;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDictation = useDictation;
|
|
4
|
+
// @rise-x/apps-sdk/dictation: a mic-button hook that records speech and turns it into text.
|
|
5
|
+
//
|
|
6
|
+
// The hook records the default microphone with `MediaRecorder` and sends the clip to the host's
|
|
7
|
+
// AI through `agents.transcribe`. The transcript arrives in one piece when recording stops, so
|
|
8
|
+
// there are no interim words to show. Nothing is stored: no chat or run is created.
|
|
9
|
+
//
|
|
10
|
+
// Not re-exported from the package root: it reads the agents connector, which the root barrel
|
|
11
|
+
// stays free of.
|
|
12
|
+
const react_1 = require("react");
|
|
13
|
+
const accessors_1 = require("../accessors");
|
|
14
|
+
const agents_1 = require("../connectors/agents");
|
|
15
|
+
// First container the browser will actually record. Chrome and Firefox take webm/opus and Safari
|
|
16
|
+
// only mp4. Left to its own default, MediaRecorder may pick one the gateway does not accept.
|
|
17
|
+
const PREFERRED_TYPES = [
|
|
18
|
+
"audio/webm;codecs=opus",
|
|
19
|
+
"audio/webm",
|
|
20
|
+
"audio/mp4",
|
|
21
|
+
"audio/ogg",
|
|
22
|
+
];
|
|
23
|
+
// Below this a clip is a mis-tap, not speech. Transcription models make up words for
|
|
24
|
+
// near-silence, so an accidental double-tap would otherwise insert text nobody said.
|
|
25
|
+
const MIN_CLIP_MS = 600;
|
|
26
|
+
function pickMimeType() {
|
|
27
|
+
if (typeof MediaRecorder === "undefined" || !MediaRecorder.isTypeSupported) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return PREFERRED_TYPES.find((type) => MediaRecorder.isTypeSupported(type));
|
|
31
|
+
}
|
|
32
|
+
// Read on every call rather than captured: the host can finish wiring its AI after the first
|
|
33
|
+
// render, and the browser APIs are absent outside a secure context.
|
|
34
|
+
function isSupported() {
|
|
35
|
+
return (typeof window !== "undefined" &&
|
|
36
|
+
typeof MediaRecorder !== "undefined" &&
|
|
37
|
+
typeof navigator !== "undefined" &&
|
|
38
|
+
!!navigator.mediaDevices?.getUserMedia &&
|
|
39
|
+
(0, accessors_1.getShellAi)() !== null);
|
|
40
|
+
}
|
|
41
|
+
function useDictation(options = {}) {
|
|
42
|
+
const [state, setState] = (0, react_1.useState)("idle");
|
|
43
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
44
|
+
const recorder = (0, react_1.useRef)(null);
|
|
45
|
+
const stream = (0, react_1.useRef)(null);
|
|
46
|
+
// Claimed synchronously: `recorder` is only set after the getUserMedia await, so without this
|
|
47
|
+
// two fast clicks both pass the guard and the second recorder replaces the first while its
|
|
48
|
+
// stream stays open, with the microphone indicator on.
|
|
49
|
+
const starting = (0, react_1.useRef)(false);
|
|
50
|
+
const startedAt = (0, react_1.useRef)(0);
|
|
51
|
+
const upload = (0, react_1.useRef)(null);
|
|
52
|
+
// The user's intent. A stop that is already flushing must not be undone by the `stop` event
|
|
53
|
+
// that follows, and a cancel during the getUserMedia await must still win once the stream arrives.
|
|
54
|
+
const discarded = (0, react_1.useRef)(false);
|
|
55
|
+
// The latest options, so `start` stays stable and a re-render never changes what an in-flight
|
|
56
|
+
// clip reports to.
|
|
57
|
+
const latest = (0, react_1.useRef)(options);
|
|
58
|
+
latest.current = options;
|
|
59
|
+
const releaseStream = (0, react_1.useCallback)(() => {
|
|
60
|
+
stream.current?.getTracks().forEach((track) => track.stop());
|
|
61
|
+
stream.current = null;
|
|
62
|
+
}, []);
|
|
63
|
+
const transcribeClip = (0, react_1.useCallback)(async (clip) => {
|
|
64
|
+
const controller = new AbortController();
|
|
65
|
+
upload.current = controller;
|
|
66
|
+
setState("transcribing");
|
|
67
|
+
try {
|
|
68
|
+
const text = await agents_1.agents.transcribe(clip, {
|
|
69
|
+
language: latest.current.language,
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
});
|
|
72
|
+
if (!controller.signal.aborted && text.trim()) {
|
|
73
|
+
latest.current.onText?.(text);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// An abort is our own cancel, not a failure worth reporting.
|
|
78
|
+
if (!controller.signal.aborted)
|
|
79
|
+
setError("failed");
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
// Only the clip that is still current settles the state: after a cancel, a new recording
|
|
83
|
+
// may already own it.
|
|
84
|
+
if (upload.current === controller) {
|
|
85
|
+
upload.current = null;
|
|
86
|
+
setState("idle");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}, []);
|
|
90
|
+
const start = (0, react_1.useCallback)(async () => {
|
|
91
|
+
if (!isSupported() ||
|
|
92
|
+
recorder.current ||
|
|
93
|
+
starting.current ||
|
|
94
|
+
upload.current) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
starting.current = true;
|
|
98
|
+
// Cleared after the guards, so a no-op start cannot undo a pending cancel.
|
|
99
|
+
discarded.current = false;
|
|
100
|
+
setError(null);
|
|
101
|
+
let media;
|
|
102
|
+
try {
|
|
103
|
+
media = await navigator.mediaDevices.getUserMedia({
|
|
104
|
+
audio: {
|
|
105
|
+
echoCancellation: true,
|
|
106
|
+
noiseSuppression: true,
|
|
107
|
+
autoGainControl: true,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
starting.current = false;
|
|
113
|
+
if (discarded.current)
|
|
114
|
+
return;
|
|
115
|
+
// Only a permission problem is `denied`: a missing or busy microphone is `failed`,
|
|
116
|
+
// because sending the user to their browser settings cannot fix it.
|
|
117
|
+
const name = e?.name;
|
|
118
|
+
setError(name === "NotAllowedError" || name === "SecurityError"
|
|
119
|
+
? "denied"
|
|
120
|
+
: "failed");
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// Cancelled, or unmounted, while the browser was asking: the stream is no longer wanted.
|
|
124
|
+
if (discarded.current) {
|
|
125
|
+
media.getTracks().forEach((track) => track.stop());
|
|
126
|
+
starting.current = false;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const mimeType = pickMimeType();
|
|
130
|
+
let instance;
|
|
131
|
+
try {
|
|
132
|
+
instance = new MediaRecorder(media, mimeType ? { mimeType } : undefined);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// Leaving the stream open here would keep the microphone indicator on with nothing recording.
|
|
136
|
+
media.getTracks().forEach((track) => track.stop());
|
|
137
|
+
starting.current = false;
|
|
138
|
+
setError("failed");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const chunks = [];
|
|
142
|
+
instance.ondataavailable = (event) => {
|
|
143
|
+
if (event.data.size)
|
|
144
|
+
chunks.push(event.data);
|
|
145
|
+
};
|
|
146
|
+
instance.onstop = () => {
|
|
147
|
+
releaseStream();
|
|
148
|
+
recorder.current = null;
|
|
149
|
+
const clip = new Blob(chunks, { type: instance.mimeType || mimeType });
|
|
150
|
+
if (discarded.current ||
|
|
151
|
+
!clip.size ||
|
|
152
|
+
Date.now() - startedAt.current < MIN_CLIP_MS) {
|
|
153
|
+
setState("idle");
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
void transcribeClip(clip);
|
|
157
|
+
};
|
|
158
|
+
stream.current = media;
|
|
159
|
+
recorder.current = instance;
|
|
160
|
+
startedAt.current = Date.now();
|
|
161
|
+
try {
|
|
162
|
+
instance.start();
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
recorder.current = null;
|
|
166
|
+
releaseStream();
|
|
167
|
+
starting.current = false;
|
|
168
|
+
setError("failed");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
starting.current = false;
|
|
172
|
+
setState("recording");
|
|
173
|
+
}, [releaseStream, transcribeClip]);
|
|
174
|
+
// The state guard matters: `stop()` flips the recorder to `inactive` at once but fires its
|
|
175
|
+
// events a tick later, so a second call in that window would throw InvalidStateError. A
|
|
176
|
+
// double-clicked button, a key repeat, or the unmount cleanup on top of either all land here.
|
|
177
|
+
const stop = (0, react_1.useCallback)(() => {
|
|
178
|
+
const instance = recorder.current;
|
|
179
|
+
if (instance && instance.state !== "inactive")
|
|
180
|
+
instance.stop();
|
|
181
|
+
}, []);
|
|
182
|
+
const cancel = (0, react_1.useCallback)(() => {
|
|
183
|
+
discarded.current = true;
|
|
184
|
+
upload.current?.abort();
|
|
185
|
+
upload.current = null;
|
|
186
|
+
// A recorder that is already flushing releases the stream from its own `stop` event.
|
|
187
|
+
if (recorder.current)
|
|
188
|
+
stop();
|
|
189
|
+
else
|
|
190
|
+
releaseStream();
|
|
191
|
+
setError(null);
|
|
192
|
+
setState("idle");
|
|
193
|
+
}, [releaseStream, stop]);
|
|
194
|
+
(0, react_1.useEffect)(() => {
|
|
195
|
+
return () => {
|
|
196
|
+
// Unmount: the microphone indicator must go out even mid-clip.
|
|
197
|
+
discarded.current = true;
|
|
198
|
+
upload.current?.abort();
|
|
199
|
+
upload.current = null;
|
|
200
|
+
stop();
|
|
201
|
+
releaseStream();
|
|
202
|
+
};
|
|
203
|
+
}, [releaseStream, stop]);
|
|
204
|
+
return { supported: isSupported(), state, error, start, stop, cancel };
|
|
205
|
+
}
|
package/build/query/assets.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type InfiniteData, type UseInfiniteQueryResult, type UseQueryResult } from "@tanstack/react-query";
|
|
2
|
-
import { type AssetRowPage, type AssetSearchPage, type AssetsConnector, type CreateAssetArgs, type ListAssetRowsArgs, type ListRelatedAssetsArgs, type QuickSearchAssetsArgs, type SearchAssetsArgs, type StartEditAssetArgs } from "../connectors/assets";
|
|
2
|
+
import { type AssetDraft, type AssetRowPage, type AssetSearchPage, type AssetsConnector, type CreateAssetArgs, type ListAssetRowsArgs, type ListRelatedAssetsArgs, type QuickSearchAssetsArgs, type SearchAssetsArgs, type StartEditAssetArgs } from "../connectors/assets";
|
|
3
3
|
import type { ConnectorError } from "../connectors/errors";
|
|
4
|
-
import type { AssetDetail, AssetSummary, AssetType
|
|
4
|
+
import type { AssetDetail, AssetSummary, AssetType } from "../connectors/types";
|
|
5
5
|
import { type QueryEnvId } from "./keys";
|
|
6
6
|
import { type SdkMutationOptions, type SdkQueryOptions } from "./shared";
|
|
7
7
|
export type SearchAssetsQueryArgs = Omit<SearchAssetsArgs, "page" | "signal">;
|
|
@@ -73,9 +73,9 @@ export declare function useAssetQuickSearch(args: QuickSearchAssetsQueryArgs, op
|
|
|
73
73
|
export declare function useAssetRows(args: ListAssetRowsQueryArgs, options?: SdkQueryOptions): UseInfiniteQueryResult<InfiniteData<AssetRowPage>, ConnectorError>;
|
|
74
74
|
export declare function useRelatedAssets(args: ListRelatedAssetsQueryArgs, options?: SdkQueryOptions): UseQueryResult<AssetSummary[], ConnectorError>;
|
|
75
75
|
export type CreateAssetVariables = Omit<CreateAssetArgs, "signal">;
|
|
76
|
-
export declare function useCreateAsset(options?: SdkMutationOptions<
|
|
76
|
+
export declare function useCreateAsset(options?: SdkMutationOptions<AssetDraft, CreateAssetVariables>): import("@tanstack/react-query").UseMutationResult<AssetDraft, ConnectorError, CreateAssetVariables, unknown>;
|
|
77
77
|
export type StartEditAssetVariables = Omit<StartEditAssetArgs, "signal">;
|
|
78
|
-
export declare function useStartEditAsset(options?: SdkMutationOptions<
|
|
78
|
+
export declare function useStartEditAsset(options?: SdkMutationOptions<AssetDraft, StartEditAssetVariables>): import("@tanstack/react-query").UseMutationResult<AssetDraft, ConnectorError, StartEditAssetVariables, unknown>;
|
|
79
79
|
export interface CloneAssetVariables {
|
|
80
80
|
assetId: string;
|
|
81
81
|
}
|
package/build/query/assets.js
CHANGED
|
@@ -16,10 +16,11 @@ exports.useDeleteAsset = useDeleteAsset;
|
|
|
16
16
|
// their cache-consistency rules. Built-in invalidation runs before the
|
|
17
17
|
// caller's onSuccess — composed, never replaced.
|
|
18
18
|
//
|
|
19
|
-
// Note the platform's edit model: create/startEdit return a DRAFT WORK ITEM
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
19
|
+
// Note the platform's edit model: create/startEdit return a DRAFT WORK ITEM,
|
|
20
|
+
// saved via work.submit (useSubmitWork). Both mutations invalidate the WORK
|
|
21
|
+
// listings here. create also saves a draft asset at once and startEdit sets
|
|
22
|
+
// the asset's draftWorkId, so they invalidate asset caches too; useSubmitWork
|
|
23
|
+
// (query/work.ts) invalidates the assets scope when the draft is saved.
|
|
23
24
|
const react_query_1 = require("@tanstack/react-query");
|
|
24
25
|
const assets_1 = require("../connectors/assets");
|
|
25
26
|
const search_1 = require("../connectors/search");
|
|
@@ -124,46 +125,57 @@ function useRelatedAssets(args, options) {
|
|
|
124
125
|
// Returns the draft work item — fill it with usePatchWorkData and save it
|
|
125
126
|
// with useSubmitWork (which also refreshes the asset listings).
|
|
126
127
|
function useCreateAsset(options) {
|
|
127
|
-
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.create(vars), (client) => {
|
|
128
|
+
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.create(vars), (client, _data, _vars, searches) => {
|
|
128
129
|
const envId = (0, shared_1.getLiveEnvId)();
|
|
129
|
-
// CROSS-DOMAIN: the draft shows up as a WORK item
|
|
130
|
-
//
|
|
130
|
+
// CROSS-DOMAIN: the draft shows up as a WORK item, and the platform
|
|
131
|
+
// saves the asset as a draft at once, so asset listings go stale too.
|
|
131
132
|
void client.invalidateQueries({
|
|
132
133
|
queryKey: keys_1.queryKeys.work.allRows(envId),
|
|
133
134
|
});
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
if (searches) {
|
|
136
|
+
void client.invalidateQueries({
|
|
137
|
+
queryKey: keys_1.queryKeys.work.searchAll(envId),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
void (0, shared_1.invalidateScope)(client, keys_1.queryKeys.assets.all(envId), searches);
|
|
137
141
|
}, options);
|
|
138
142
|
}
|
|
139
143
|
function useStartEditAsset(options) {
|
|
140
|
-
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.startEdit(vars), (client) => {
|
|
144
|
+
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.startEdit(vars), (client, _data, vars, searches) => {
|
|
141
145
|
const envId = (0, shared_1.getLiveEnvId)();
|
|
142
146
|
// CROSS-DOMAIN: same draft-as-work-item model as useCreateAsset.
|
|
143
147
|
void client.invalidateQueries({
|
|
144
148
|
queryKey: keys_1.queryKeys.work.allRows(envId),
|
|
145
149
|
});
|
|
150
|
+
if (searches) {
|
|
151
|
+
void client.invalidateQueries({
|
|
152
|
+
queryKey: keys_1.queryKeys.work.searchAll(envId),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// The asset's draftWorkId now names this draft.
|
|
146
156
|
void client.invalidateQueries({
|
|
147
|
-
queryKey: keys_1.queryKeys.
|
|
157
|
+
queryKey: keys_1.queryKeys.assets.detail(envId, vars.assetId),
|
|
148
158
|
});
|
|
149
159
|
}, options);
|
|
150
160
|
}
|
|
151
161
|
function useCloneAsset(options) {
|
|
152
|
-
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.clone(vars.assetId), (client) => {
|
|
162
|
+
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.clone(vars.assetId), (client, _data, _vars, searches) => {
|
|
153
163
|
const envId = (0, shared_1.getLiveEnvId)();
|
|
154
164
|
void client.invalidateQueries({
|
|
155
165
|
queryKey: keys_1.queryKeys.assets.allRows(envId),
|
|
156
166
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
167
|
+
if (searches) {
|
|
168
|
+
void client.invalidateQueries({
|
|
169
|
+
queryKey: keys_1.queryKeys.assets.searchAll(envId),
|
|
170
|
+
});
|
|
171
|
+
}
|
|
160
172
|
void client.invalidateQueries({
|
|
161
173
|
queryKey: keys_1.queryKeys.assets.quickSearchAll(envId),
|
|
162
174
|
});
|
|
163
175
|
}, options);
|
|
164
176
|
}
|
|
165
177
|
function useDeleteAsset(options) {
|
|
166
|
-
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.delete(vars.assetId), (client, _data, vars) => {
|
|
178
|
+
return (0, shared_1.useSdkMutation)((vars) => assets_1.assets.delete(vars.assetId), (client, _data, vars, searches) => {
|
|
167
179
|
const envId = (0, shared_1.getLiveEnvId)();
|
|
168
180
|
client.removeQueries({
|
|
169
181
|
queryKey: keys_1.queryKeys.assets.detail(envId, vars.assetId),
|
|
@@ -171,9 +183,11 @@ function useDeleteAsset(options) {
|
|
|
171
183
|
void client.invalidateQueries({
|
|
172
184
|
queryKey: keys_1.queryKeys.assets.allRows(envId),
|
|
173
185
|
});
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
186
|
+
if (searches) {
|
|
187
|
+
void client.invalidateQueries({
|
|
188
|
+
queryKey: keys_1.queryKeys.assets.searchAll(envId),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
177
191
|
void client.invalidateQueries({
|
|
178
192
|
queryKey: keys_1.queryKeys.assets.quickSearchAll(envId),
|
|
179
193
|
});
|