@anam-ai/js-sdk 4.25.0 → 4.26.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/README.md +46 -0
- package/dist/main/AnamClient.d.ts +10 -0
- package/dist/main/AnamClient.d.ts.map +1 -1
- package/dist/main/AnamClient.js +10 -0
- package/dist/main/AnamClient.js.map +1 -1
- package/dist/main/lib/constants.js +1 -1
- package/dist/main/types/TalkMessageStream.d.ts +49 -1
- package/dist/main/types/TalkMessageStream.d.ts.map +1 -1
- package/dist/main/types/TalkMessageStream.js +61 -13
- package/dist/main/types/TalkMessageStream.js.map +1 -1
- package/dist/main/types/signalling/TalkMessageStreamPayload.d.ts +1 -0
- package/dist/main/types/signalling/TalkMessageStreamPayload.d.ts.map +1 -1
- package/dist/module/AnamClient.d.ts +10 -0
- package/dist/module/AnamClient.d.ts.map +1 -1
- package/dist/module/AnamClient.js +10 -0
- package/dist/module/AnamClient.js.map +1 -1
- package/dist/module/lib/constants.js +1 -1
- package/dist/module/types/TalkMessageStream.d.ts +49 -1
- package/dist/module/types/TalkMessageStream.d.ts.map +1 -1
- package/dist/module/types/TalkMessageStream.js +61 -13
- package/dist/module/types/TalkMessageStream.js.map +1 -1
- package/dist/module/types/signalling/TalkMessageStreamPayload.d.ts +1 -0
- package/dist/module/types/signalling/TalkMessageStreamPayload.d.ts.map +1 -1
- package/dist/umd/anam.js +1 -1
- package/package.json +1 -1
|
@@ -1,16 +1,64 @@
|
|
|
1
1
|
import { TalkMessageStreamState } from './TalkMessageStreamState';
|
|
2
2
|
import { InternalEventEmitter } from '../modules/InternalEventEmitter';
|
|
3
3
|
import { SignallingClient } from '../modules/SignallingClient';
|
|
4
|
+
/**
|
|
5
|
+
* A stream of text chunks for the persona to speak.
|
|
6
|
+
*
|
|
7
|
+
* Manages the correlationId internally so callers don't need to track it across chunks.
|
|
8
|
+
* All chunks in the same speech sequence share one correlationId, which is what
|
|
9
|
+
* interruptions correlate against. Callers can optionally identify utterances within the
|
|
10
|
+
* sequence by passing an utteranceId to streamMessageChunk. Set an id on the first chunk
|
|
11
|
+
* of an utterance, then omit it on continuation chunks. A new id queues the next
|
|
12
|
+
* utterance after the current one, so the stream can stay open while your application
|
|
13
|
+
* runs a tool.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Streaming LLM output
|
|
18
|
+
* const stream = anamClient.createTalkMessageStream();
|
|
19
|
+
* for (const chunk of llmChunks) {
|
|
20
|
+
* await stream.streamMessageChunk(chunk.text, chunk.isLast);
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Speech before and after a tool call
|
|
24
|
+
* const stream = anamClient.createTalkMessageStream();
|
|
25
|
+
* await stream.streamMessageChunk('Let me ', false, crypto.randomUUID());
|
|
26
|
+
* await stream.streamMessageChunk('check.', false); // Continues the same utterance.
|
|
27
|
+
* const toolResultText = await runToolCall();
|
|
28
|
+
* await stream.streamMessageChunk(toolResultText, true, crypto.randomUUID());
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
4
31
|
export declare class TalkMessageStream {
|
|
5
32
|
private internalEventEmitter;
|
|
6
33
|
private state;
|
|
7
34
|
private correlationId;
|
|
8
35
|
private signallingClient;
|
|
36
|
+
private lastUtteranceId?;
|
|
9
37
|
constructor(correlationId: string, internalEventEmitter: InternalEventEmitter, signallingClient: SignallingClient);
|
|
10
38
|
private onDeactivate;
|
|
11
39
|
private onSignalMessage;
|
|
40
|
+
/**
|
|
41
|
+
* End the stream without sending more content.
|
|
42
|
+
*
|
|
43
|
+
* The terminator carries the most recent utteranceId passed to
|
|
44
|
+
* streamMessageChunk, so it does not start a new untagged utterance.
|
|
45
|
+
*/
|
|
12
46
|
endMessage(): Promise<void>;
|
|
13
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Send a text chunk to be spoken.
|
|
49
|
+
*
|
|
50
|
+
* @param partialMessage The text chunk to speak.
|
|
51
|
+
* @param endOfSpeech Whether this is the final chunk of the speech.
|
|
52
|
+
* @param utteranceId Optional lowercase UUID v4 string marking the utterance this chunk
|
|
53
|
+
* starts. Set it on the first chunk, not on every text chunk; omitting it continues the
|
|
54
|
+
* current utterance. A new id queues the next utterance after the current one without
|
|
55
|
+
* ending the speech sequence, which is what allows speech before and after a tool call,
|
|
56
|
+
* or two ready utterances that must play in order. The most recent value is reused for
|
|
57
|
+
* the terminator sent by endMessage. Throws if it is not a lowercase UUID v4. Needs a
|
|
58
|
+
* Cara 4 avatar: Cara 3 avatars drop the id silently and speak the turn as one
|
|
59
|
+
* utterance.
|
|
60
|
+
*/
|
|
61
|
+
streamMessageChunk(partialMessage: string, endOfSpeech: boolean, utteranceId?: string): Promise<void>;
|
|
14
62
|
getCorrelationId(): string;
|
|
15
63
|
isActive(): boolean;
|
|
16
64
|
getState(): TalkMessageStreamState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAO/D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,gBAAgB,CAAmB;IAC3C,OAAO,CAAC,eAAe,CAAC,CAAS;gBAG/B,aAAa,EAAE,MAAM,EACrB,oBAAoB,EAAE,oBAAoB,EAC1C,gBAAgB,EAAE,gBAAgB;IAYpC,OAAO,CAAC,YAAY;YAON,eAAe;IAa7B;;;;;OAKG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IA2BxC;;;;;;;;;;;;;OAaG;IACU,kBAAkB,CAC7B,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,OAAO,EACpB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC;IAkCT,gBAAgB,IAAI,MAAM;IAI1B,QAAQ,IAAI,OAAO;IAOnB,QAAQ,IAAI,sBAAsB;CAG1C"}
|
|
@@ -9,6 +9,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { InternalEvent, SignalMessageAction } from '.';
|
|
11
11
|
import { TalkMessageStreamState } from './TalkMessageStreamState';
|
|
12
|
+
// Lowercase only: the engine round-trips the canonical form and replaces anything
|
|
13
|
+
// else with an id of its own, so an uppercase id would never reach caption events.
|
|
14
|
+
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
15
|
+
/**
|
|
16
|
+
* A stream of text chunks for the persona to speak.
|
|
17
|
+
*
|
|
18
|
+
* Manages the correlationId internally so callers don't need to track it across chunks.
|
|
19
|
+
* All chunks in the same speech sequence share one correlationId, which is what
|
|
20
|
+
* interruptions correlate against. Callers can optionally identify utterances within the
|
|
21
|
+
* sequence by passing an utteranceId to streamMessageChunk. Set an id on the first chunk
|
|
22
|
+
* of an utterance, then omit it on continuation chunks. A new id queues the next
|
|
23
|
+
* utterance after the current one, so the stream can stay open while your application
|
|
24
|
+
* runs a tool.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* // Streaming LLM output
|
|
29
|
+
* const stream = anamClient.createTalkMessageStream();
|
|
30
|
+
* for (const chunk of llmChunks) {
|
|
31
|
+
* await stream.streamMessageChunk(chunk.text, chunk.isLast);
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* // Speech before and after a tool call
|
|
35
|
+
* const stream = anamClient.createTalkMessageStream();
|
|
36
|
+
* await stream.streamMessageChunk('Let me ', false, crypto.randomUUID());
|
|
37
|
+
* await stream.streamMessageChunk('check.', false); // Continues the same utterance.
|
|
38
|
+
* const toolResultText = await runToolCall();
|
|
39
|
+
* await stream.streamMessageChunk(toolResultText, true, crypto.randomUUID());
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
12
42
|
export class TalkMessageStream {
|
|
13
43
|
constructor(correlationId, internalEventEmitter, signallingClient) {
|
|
14
44
|
this.state = TalkMessageStreamState.UNSTARTED;
|
|
@@ -31,6 +61,12 @@ export class TalkMessageStream {
|
|
|
31
61
|
}
|
|
32
62
|
});
|
|
33
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* End the stream without sending more content.
|
|
66
|
+
*
|
|
67
|
+
* The terminator carries the most recent utteranceId passed to
|
|
68
|
+
* streamMessageChunk, so it does not start a new untagged utterance.
|
|
69
|
+
*/
|
|
34
70
|
endMessage() {
|
|
35
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
72
|
if (this.state === TalkMessageStreamState.ENDED) {
|
|
@@ -41,30 +77,42 @@ export class TalkMessageStream {
|
|
|
41
77
|
console.warn('Talk stream is not in an active state: ' + this.state);
|
|
42
78
|
return;
|
|
43
79
|
}
|
|
44
|
-
const payload = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
endOfSpeech: true,
|
|
48
|
-
correlationId: this.correlationId,
|
|
49
|
-
};
|
|
80
|
+
const payload = Object.assign({ content: '', startOfSpeech: false, endOfSpeech: true, correlationId: this.correlationId }, (this.lastUtteranceId != null
|
|
81
|
+
? { utteranceId: this.lastUtteranceId }
|
|
82
|
+
: {}));
|
|
50
83
|
yield this.signallingClient.sendTalkMessage(payload);
|
|
51
84
|
this.state = TalkMessageStreamState.ENDED;
|
|
52
85
|
this.onDeactivate();
|
|
53
86
|
});
|
|
54
87
|
}
|
|
55
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Send a text chunk to be spoken.
|
|
90
|
+
*
|
|
91
|
+
* @param partialMessage The text chunk to speak.
|
|
92
|
+
* @param endOfSpeech Whether this is the final chunk of the speech.
|
|
93
|
+
* @param utteranceId Optional lowercase UUID v4 string marking the utterance this chunk
|
|
94
|
+
* starts. Set it on the first chunk, not on every text chunk; omitting it continues the
|
|
95
|
+
* current utterance. A new id queues the next utterance after the current one without
|
|
96
|
+
* ending the speech sequence, which is what allows speech before and after a tool call,
|
|
97
|
+
* or two ready utterances that must play in order. The most recent value is reused for
|
|
98
|
+
* the terminator sent by endMessage. Throws if it is not a lowercase UUID v4. Needs a
|
|
99
|
+
* Cara 4 avatar: Cara 3 avatars drop the id silently and speak the turn as one
|
|
100
|
+
* utterance.
|
|
101
|
+
*/
|
|
102
|
+
streamMessageChunk(partialMessage, endOfSpeech, utteranceId) {
|
|
56
103
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
104
|
if (this.state !== TalkMessageStreamState.STREAMING &&
|
|
58
105
|
this.state !== TalkMessageStreamState.UNSTARTED) {
|
|
59
106
|
// throw error
|
|
60
107
|
throw new Error('Talk stream is not in an active state: ' + this.state);
|
|
61
108
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
109
|
+
if (utteranceId != null && !UUID_V4.test(utteranceId)) {
|
|
110
|
+
throw new Error('utteranceId must be a lowercase UUID v4 string, got: ' + utteranceId);
|
|
111
|
+
}
|
|
112
|
+
const payload = Object.assign({ content: partialMessage, startOfSpeech: this.state === TalkMessageStreamState.UNSTARTED, endOfSpeech: endOfSpeech, correlationId: this.correlationId }, (utteranceId != null ? { utteranceId } : {}));
|
|
113
|
+
if (utteranceId != null) {
|
|
114
|
+
this.lastUtteranceId = utteranceId;
|
|
115
|
+
}
|
|
68
116
|
this.state = endOfSpeech
|
|
69
117
|
? TalkMessageStreamState.ENDED
|
|
70
118
|
: TalkMessageStreamState.STREAMING;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TalkMessageStream.js","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAiB,mBAAmB,EAAE,MAAM,GAAG,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAMlE,MAAM,OAAO,iBAAiB;
|
|
1
|
+
{"version":3,"file":"TalkMessageStream.js","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAiB,mBAAmB,EAAE,MAAM,GAAG,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAMlE,kFAAkF;AAClF,mFAAmF;AACnF,MAAM,OAAO,GACX,uEAAuE,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,iBAAiB;IAO5B,YACE,aAAqB,EACrB,oBAA0C,EAC1C,gBAAkC;QAR5B,UAAK,GAAG,sBAAsB,CAAC,SAAS,CAAC;QAU/C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACjD,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QAEzC,IAAI,CAAC,oBAAoB,CAAC,WAAW,CACnC,aAAa,CAAC,uBAAuB,EACrC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAChC,CAAC;IACJ,CAAC;IAEO,YAAY;QAClB,IAAI,CAAC,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O,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAEM,QAAQ;QACb,OAAO,CACL,IAAI,CAAC,KAAK,KAAK,sBAAsB,CAAC,SAAS;YAC/C,IAAI,CAAC,KAAK,KAAK,sBAAsB,CAAC,SAAS,CAChD,CAAC;IACJ,CAAC;IAEM,QAAQ;QACb,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TalkMessageStreamPayload.d.ts","sourceRoot":"","sources":["../../../../src/types/signalling/TalkMessageStreamPayload.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IACvB,WAAW,EAAE,OAAO,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"TalkMessageStreamPayload.d.ts","sourceRoot":"","sources":["../../../../src/types/signalling/TalkMessageStreamPayload.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IACvB,WAAW,EAAE,OAAO,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IAItB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|