@intelliweave/embedded 1.8.63 → 1.9.65-beta.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.
@@ -1,773 +1,756 @@
1
1
  import React from 'react';
2
- import { Optional } from 'utility-types';
3
2
  import { Client } from '@modelcontextprotocol/sdk/client/index.js';
3
+ import { Optional } from 'utility-types';
4
4
 
5
5
  /**
6
- * This class helps organize groups of tokenized text along with removing items when the window is full.
6
+ * Speech output
7
+ *
8
+ * - event `speechstart` - When the speech starts
9
+ * - event `speechend` - When the speech ends
7
10
  */
8
- declare class TokenWindow {
9
- /** Token window size */
10
- size: number;
11
- /** Token groups */
12
- groups: TokenWindowGroup<any>[];
13
- /** Create a new group */
14
- createGroup(id: string): TokenWindowGroup<unknown>;
15
- /** Get a group */
16
- group<CustomDataType>(id: string): TokenWindowGroup<CustomDataType> | undefined;
17
- /** Calculate current tokens in all groups */
18
- countTokens(): number;
19
- /** Remove overflow from all groups. */
20
- removeOverflow(): void;
21
- /** Remove one overflow item. Returns null if no items were able to be removed. */
22
- private removeOneItem;
11
+ declare class WebWeaverSpeechOutput extends EventTarget {
12
+ /** Reference to the AI */
13
+ private ai?;
14
+ /** Constructor */
15
+ constructor(ai: IntelliWeave);
16
+ /** Called when the AI speaks */
17
+ onTextOutputFromAI(e: CustomEvent): void;
18
+ /** Current player vars */
19
+ private currentPlayerVolume?;
20
+ private currentPlayer?;
21
+ /** The audio analyser node */
22
+ private analyserNode?;
23
+ /** The audio analyser buffer */
24
+ private analyserBuffer?;
25
+ /** @private Maximum volume heard this session */
26
+ private maxVolumeHeard;
27
+ /** Get current (realtime) audio output volume level, from 0 to 1 */
28
+ get volumeLevel(): number;
29
+ /** Speak the text */
30
+ speak(text: string): Promise<void>;
31
+ private _speakWithLock;
32
+ /** True if currently playing audio */
33
+ get isSpeaking(): boolean;
34
+ /** Interrupt the previously playing audio */
35
+ interrupt(): Promise<void>;
36
+ /** Called when the speech output ends */
37
+ onSpeechEnd(): void;
23
38
  }
24
- /** A token group. */
25
- declare class TokenWindowGroup<CustomDataType> {
26
- /** Group ID */
27
- id: string;
28
- /** List of items */
29
- items: TokenWindowGroupItem<CustomDataType>[];
39
+
40
+ /**
41
+ * An AudioWorklet module that records data from input and sends it to the host.
42
+ *
43
+ * - event `data` - Fired when data is available to be read.
44
+ */
45
+ declare class PCMReceiverNode extends AudioWorkletNode {
46
+ /** @type {'int16' | 'float32'} The output data format */
47
+ format: string;
30
48
  /**
31
- * Weight controls how many items from this group should be kept in relation to the entire window. For example if all
32
- * groups have a weight of 1, each group remove items equally if full. If one has a weight of 2 while the rest are 1,
33
- * that group will be allowed to keep double the amount of items.
49
+ * Creates a new PCMRecorderNode ready to receive PCM data.
50
+ *
51
+ * @param context - The audio context to use.
52
+ * @param sampleRate - The sample rate of the output data stream.
53
+ * @param format - The format of the output data stream.
54
+ * @param bufferSize - The size of the output buffer in elements (Int16Array or Float32Array items, depending on `format`).
34
55
  */
35
- weight: number;
36
- /** Current total token count, computed automatically. Don't update this value manually. */
37
- tokenCount: number;
38
- /** Group item separator */
39
- separator: string;
40
- /** Token count padding added to each item. */
41
- private itemPadding;
42
- /** Sets the token count padding added to each item. Useful if you don't know exactly what will be added by the LLM host. */
43
- setItemPadding(padding: number): this;
44
- /** Sort function */
45
- private sortFunction;
46
- /** Set sort function */
47
- sortBy(sortFunction: (a: TokenWindowGroupItem<CustomDataType>, b: TokenWindowGroupItem<CustomDataType>) => number): this;
48
- /** Set separator */
49
- setSeparator(separator: string): this;
50
- /** Set weight */
51
- setWeight(weight: number): this;
52
- /** Recalculate all tokens. Note this may take a while. */
53
- recalculateTokens(): void;
54
- /** Add an item to the group */
55
- add(item: string | Omit<Optional<TokenWindowGroupItem<CustomDataType>, 'id' | 'dateAdded' | 'sortOrder'>, 'tokenCount'>): TokenWindowGroupItem<CustomDataType>;
56
- /** Get all items as a string */
57
- getAllAsString(): string;
58
- /** Get all items. Doesn't return disabled items. */
59
- getAll(): TokenWindowGroupItem<CustomDataType>[];
60
- /** Remove all items from this group */
61
- empty(): void;
62
- }
63
- /** Token group item */
64
- interface TokenWindowGroupItem<CustomDataType> {
65
- /** Each item must have a unique ID. */
66
- id: string;
67
- /** The string content of the item */
68
- content: string;
69
- /** True if this item should never be removed */
70
- cannotRemove?: boolean;
71
- /** Sorting order. If not specified, uses dateAdded instead. */
72
- sortOrder: number;
73
- /** Date this item was added */
74
- dateAdded: number;
75
- /** Token count in the content */
76
- tokenCount: number;
77
- /** Anything to attach to this item */
78
- customData?: CustomDataType;
79
- /** If disabled, this item will not be included and will not add tot he token count. */
80
- disabled?: boolean;
56
+ constructor(context: AudioContext, sampleRate: number, format: 'int16' | 'int64' | 'float32', bufferSize: number);
57
+ /** @private Called when a message is received from the worklet */
58
+ onWorkletMessage(e: MessageEvent): void;
59
+ /** Called when data is received */
60
+ onData(buffer: Float32Array | Int16Array | BigInt64Array): void;
81
61
  }
82
62
 
83
- // ==================================================================================================
84
- // JSON Schema Draft 07
85
- // ==================================================================================================
86
- // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
87
- // --------------------------------------------------------------------------------------------------
88
-
89
63
  /**
90
- * Primitive type
91
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
64
+ * An AudioNode which sends events for when speech is detected
65
+ *
66
+ * - event `speechstart` - Fired when speech is detected
67
+ * - event `speechend` - Fired when speech ends
92
68
  */
93
- type JSONSchema7TypeName =
94
- | "string" //
95
- | "number"
96
- | "integer"
97
- | "boolean"
98
- | "object"
99
- | "array"
100
- | "null";
69
+ declare class VoiceDetectionNode extends PCMReceiverNode {
70
+ /** True if voice is currently being detected */
71
+ isVoiceActive: boolean;
72
+ /** True if voice is active but may be ending soon */
73
+ get isVoicePossiblyEnding(): boolean;
74
+ /** Last date that voice was detected */
75
+ lastVoiceActiveDate: number;
76
+ /** Amount of time to wait after voice detection to detect that it has ended */
77
+ voiceEndTimeout: number;
78
+ /** Detection sensitivity, if the detection model outputs a number bigger than this it will be considered voice */
79
+ sensitivity: number;
80
+ /** Sensitivity threshold to end speaking */
81
+ sentivityEnd: number;
82
+ /** VAD model */
83
+ static vadModelURL: string;
84
+ /** Loaded VAD model */
85
+ private vad?;
86
+ /** Sample rate */
87
+ get sampleRate(): 16000 | 8000;
88
+ /** Number of samples */
89
+ get numberOfSamples(): number;
90
+ /** Number of sample chunks */
91
+ get numberOfSampleChunks(): number;
92
+ /** Output buffer size */
93
+ get outputBufferSize(): number;
94
+ /** True if the VAD model has been loaded */
95
+ get isModelLoaded(): boolean;
96
+ /** The time when to next reset the VAD model */
97
+ nextVadReset: number;
98
+ /** The current probability of active voice */
99
+ currentProbability: number;
100
+ /** Constructor */
101
+ constructor(audioContext: AudioContext);
102
+ /** Start loading */
103
+ loadModel(): Promise<void>;
104
+ private _lastVoiceActive;
105
+ /** Called when data is received */
106
+ onData(buffer: Float32Array): Promise<void>;
107
+ /** Called when speech is detected */
108
+ onSpeechStart(): void;
109
+ /** Called when speech ends */
110
+ onSpeechEnd(): void;
111
+ }
101
112
 
102
113
  /**
103
- * Primitive type
104
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
114
+ * An AudioNode which isolates speech and outputs the audio data. Since we are reusing the VAD model node,
115
+ * output data is in 8000Hz Float32 format.
116
+ *
117
+ * - event `voicedata` - Fired when a chunk of voice is detected. `data` contains the recorded chunk of voice in a Float32Array.
118
+ * - event `voicedataend` - Fired when this chunk of voice ends. `data` contains an array of Float32Array containing the entirety of the recorded voice.
105
119
  */
106
- type JSONSchema7Type =
107
- | string //
108
- | number
109
- | boolean
110
- | JSONSchema7Object
111
- | JSONSchema7Array
112
- | null;
113
-
114
- // Workaround for infinite type recursion
115
- interface JSONSchema7Object {
116
- [key: string]: JSONSchema7Type;
120
+ declare class VoiceChunkOutputNode extends VoiceDetectionNode {
121
+ /** Stored buffers */
122
+ buffers: Float32Array[];
123
+ /** Recorded audio chunks with voice in it */
124
+ recordedBuffers: Float32Array[];
125
+ /** Last active state */
126
+ _voiceRecording: boolean;
127
+ /** Amount of audio data in the buffer, in seconds */
128
+ get bufferDuration(): number;
129
+ /** Amount of data to keep from before the user started speaking */
130
+ backBufferDurationSeconds: number;
131
+ /** Called when data is received */
132
+ onData(buffer: Float32Array): Promise<void>;
133
+ /** Called when a chunk of voice is recorded */
134
+ onVoiceChunk(buffer: Float32Array): void;
135
+ /** Called when the voice recording ends */
136
+ onVoiceEnd(buffers: Float32Array[]): void;
117
137
  }
118
138
 
119
- // Workaround for infinite type recursion
120
- // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
121
- interface JSONSchema7Array extends Array<JSONSchema7Type> {}
122
-
123
139
  /**
124
- * Meta schema
125
- *
126
- * Recommended values:
127
- * - 'http://json-schema.org/schema#'
128
- * - 'http://json-schema.org/hyper-schema#'
129
- * - 'http://json-schema.org/draft-07/schema#'
130
- * - 'http://json-schema.org/draft-07/hyper-schema#'
140
+ * This AudioNode uses OpenAI's Whisper model to transcribe spoken speech to text.
131
141
  *
132
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
142
+ * - event `transcription` - Fired when a transcription is ready. `text` contains the transcribed text.
133
143
  */
134
- type JSONSchema7Version = string;
144
+ declare class OpenAITranscriptionNode extends VoiceChunkOutputNode {
145
+ /** OpenAI API key */
146
+ apiKey: string;
147
+ /** Pending buffers */
148
+ private pendingBuffers;
149
+ /** Last request */
150
+ private lastRequestAbortController?;
151
+ /** True if currently transcribing */
152
+ isTranscribing: boolean;
153
+ /** Constructor */
154
+ constructor(audioContext: AudioContext, apiKey: string);
155
+ /** Called when the voice recording ends */
156
+ onVoiceEnd(buffers: Float32Array[]): Promise<void>;
157
+ /** Called when a transcription is ready */
158
+ onVoiceTranscription(text: string): void;
159
+ }
135
160
 
136
161
  /**
137
- * JSON Schema v7
138
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
139
- */
140
- type JSONSchema7Definition = JSONSchema7 | boolean;
141
- interface JSONSchema7 {
142
- $id?: string | undefined;
143
- $ref?: string | undefined;
144
- $schema?: JSONSchema7Version | undefined;
145
- $comment?: string | undefined;
146
-
147
- /**
148
- * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
149
- * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
150
- */
151
- $defs?: {
152
- [key: string]: JSONSchema7Definition;
153
- } | undefined;
154
-
155
- /**
156
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
157
- */
158
- type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
159
- enum?: JSONSchema7Type[] | undefined;
160
- const?: JSONSchema7Type | undefined;
161
-
162
- /**
163
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
164
- */
165
- multipleOf?: number | undefined;
166
- maximum?: number | undefined;
167
- exclusiveMaximum?: number | undefined;
168
- minimum?: number | undefined;
169
- exclusiveMinimum?: number | undefined;
170
-
171
- /**
172
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
173
- */
174
- maxLength?: number | undefined;
175
- minLength?: number | undefined;
176
- pattern?: string | undefined;
177
-
178
- /**
179
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
180
- */
181
- items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
182
- additionalItems?: JSONSchema7Definition | undefined;
183
- maxItems?: number | undefined;
184
- minItems?: number | undefined;
185
- uniqueItems?: boolean | undefined;
186
- contains?: JSONSchema7Definition | undefined;
187
-
188
- /**
189
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
190
- */
191
- maxProperties?: number | undefined;
192
- minProperties?: number | undefined;
193
- required?: string[] | undefined;
194
- properties?: {
195
- [key: string]: JSONSchema7Definition;
196
- } | undefined;
197
- patternProperties?: {
198
- [key: string]: JSONSchema7Definition;
199
- } | undefined;
200
- additionalProperties?: JSONSchema7Definition | undefined;
201
- dependencies?: {
202
- [key: string]: JSONSchema7Definition | string[];
203
- } | undefined;
204
- propertyNames?: JSONSchema7Definition | undefined;
205
-
206
- /**
207
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
208
- */
209
- if?: JSONSchema7Definition | undefined;
210
- then?: JSONSchema7Definition | undefined;
211
- else?: JSONSchema7Definition | undefined;
162
+ * This AudioNode uses IntelliWeave's servers to transcribe spoken speech to text.
163
+ *
164
+ * - event `transcription` - Fired when a transcription is ready. `text` contains the transcribed text.
165
+ */
166
+ declare class IntelliWeaveTranscriptionNode extends VoiceChunkOutputNode {
167
+ /** Debug: Export each recording as a wav file for download */
168
+ static debugExportWav: boolean;
169
+ /** Server address for transcription */
170
+ apiAddress: string;
171
+ /** OpenAI API key */
172
+ apiKey: string;
173
+ /** WebSocket connection */
174
+ private ws?;
175
+ /** True if currently transcribing */
176
+ isTranscribing: boolean;
177
+ /** WebSocket shutdown timer */
178
+ private shutdownTimer?;
179
+ /** Constructor */
180
+ constructor(audioContext: AudioContext, apiKey: string);
181
+ /** Called when a voice chunk is received */
182
+ onVoiceChunk(buffer: Float32Array): Promise<void>;
183
+ /** Called when the voice recording ends */
184
+ onVoiceEnd(buffers: Float32Array[]): Promise<void>;
185
+ /** Called when a transcription is ready */
186
+ onVoiceTranscription(text: string): void;
187
+ /** Called when the WebSocket is closed */
188
+ onSocketClose(): void;
189
+ }
212
190
 
213
- /**
214
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
215
- */
216
- allOf?: JSONSchema7Definition[] | undefined;
217
- anyOf?: JSONSchema7Definition[] | undefined;
218
- oneOf?: JSONSchema7Definition[] | undefined;
219
- not?: JSONSchema7Definition | undefined;
191
+ /**
192
+ * Handles speech recognition from the microphone
193
+ *
194
+ * - event `speechstart` - We have detected the user started speaking
195
+ * - event `speechend` - We have detected the user stopped speaking
196
+ * - event `speech` - Speech recognition result
197
+ * - event `start` - Speech recognition started
198
+ * - event `end` - Speech recognition ended
199
+ */
200
+ declare class WebWeaverSpeechRecognition extends EventTarget {
201
+ /** Reference to the AI */
202
+ ai?: IntelliWeave;
203
+ /** True if recognition is running */
204
+ isRunning: boolean;
205
+ /** The audio analyser node */
206
+ private analyserNode?;
207
+ /** The audio analyser buffer */
208
+ private analyserBuffer?;
209
+ /** The microphone stream */
210
+ micStream?: MediaStream;
211
+ /** Recording start time for tracking duration */
212
+ private recordingStartTime?;
213
+ /** Returns true if speech recognition is supported by this persona and browser */
214
+ get isSupported(): boolean;
215
+ /** Currently active voice detection node */
216
+ voiceDetection?: IntelliWeaveTranscriptionNode | OpenAITranscriptionNode;
217
+ /** Constructor */
218
+ constructor(ai: IntelliWeave);
219
+ private _skipEvents;
220
+ /** Start recognition */
221
+ start(): Promise<void>;
222
+ /** Stop recognition */
223
+ stop(): void;
224
+ /** @private Maximum volume heard this session */
225
+ maxVolumeHeard: number;
226
+ /** Get current (realtime) microphone volume level, from 0 to 1 */
227
+ get volumeLevel(): number;
228
+ /** True if currently detecting words being spoken */
229
+ get wordsCurrentlyBeingSpoken(): boolean;
230
+ /** True if currently transcribing voice to text */
231
+ get isTranscribing(): boolean;
232
+ /** Called when speech has been recorded */
233
+ onTranscription(e: CustomEvent): void;
234
+ /** Called to reset the speech recognizer */
235
+ reset(): Promise<void>;
236
+ }
220
237
 
221
- /**
222
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
223
- */
224
- format?: string | undefined;
238
+ /** Handles creating and managing the AudioContext */
239
+ declare class AudioSystem {
240
+ /** Reference to the AI */
241
+ private ai?;
242
+ /** The speech recognition module. */
243
+ speechRecognition: WebWeaverSpeechRecognition;
244
+ /** The speech output module. */
245
+ speechOutput: WebWeaverSpeechOutput;
246
+ /** The audio context */
247
+ context?: AudioContext;
248
+ /** List of active named locks */
249
+ locks: string[];
250
+ /** Returns true if speech recognition and output is supported by this persona and browser */
251
+ static get isSupported(): boolean;
252
+ /** Constructor */
253
+ constructor(ai: IntelliWeave);
254
+ /** Create a named lock to enable the audio system */
255
+ beginAccess(namedLock: string): Promise<void>;
256
+ /** Stop accessing the audio system */
257
+ endAccess(namedLock: string): void;
258
+ }
225
259
 
260
+ /**
261
+ * This class allows you to use the AI as a logic engine, data extractor, etc.
262
+ */
263
+ declare class AILogic {
264
+ /** Reference to the AI */
265
+ private ai?;
266
+ /** Constructor */
267
+ constructor(ai: IntelliWeave);
268
+ /** Ask the AI a yes/no question associated with the specified data. Data must be JSON-serializable, or a string of any kind of data. */
269
+ boolean(config: IntelliWeaveInstructConfig): Promise<boolean>;
270
+ /** Ask the AI to select a choice from a list of options. */
271
+ choose(config: IntelliWeaveInstructConfig & {
272
+ /** List of choices the AI can pick from. */
273
+ options: string[];
274
+ }): Promise<string | undefined>;
226
275
  /**
227
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
276
+ * Ask the AI to extract data from input data. The AI will return the extracted data. Possibly an array of multiple extractions.
228
277
  */
229
- contentMediaType?: string | undefined;
230
- contentEncoding?: string | undefined;
231
-
278
+ extract<T = any>(config: IntelliWeaveInstructConfig & {
279
+ /** Allow multiple items to be returned. If true, returns an array instead of an object. */
280
+ allowMultiple?: boolean;
281
+ /** Fields to extract in each object. */
282
+ extractions: {
283
+ /** Field name */
284
+ name: string;
285
+ /** Field data type */
286
+ type: 'string' | 'number' | 'boolean' | 'date' | 'email' | 'phone' | 'address';
287
+ /** Describe to the AI what data to put in this field. */
288
+ description?: string;
289
+ }[];
290
+ }): Promise<T[]>;
232
291
  /**
233
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
292
+ * Generate a Markdown document based on the data from the user.
293
+ *
294
+ * @param config Instruct config.
295
+ * @returns A markdown document.
234
296
  */
235
- definitions?: {
236
- [key: string]: JSONSchema7Definition;
237
- } | undefined;
238
-
297
+ generateMarkdown(config: Omit<IntelliWeaveInstructConfig, 'instruction'>): Promise<string>;
239
298
  /**
240
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
299
+ * Perform an instruction.
300
+ *
301
+ * @param config Instruct config.
302
+ * @returns The final response from the AI.
241
303
  */
242
- title?: string | undefined;
243
- description?: string | undefined;
244
- default?: JSONSchema7Type | undefined;
245
- readOnly?: boolean | undefined;
246
- writeOnly?: boolean | undefined;
247
- examples?: JSONSchema7Type | undefined;
248
- }
249
-
250
- /** ChatGPT config options */
251
- interface ChatGPTConfig {
252
- /** API key */
253
- apiKey: string;
254
- /** Provider ID */
255
- providerID?: string;
256
- /** Endpoint URL if using a custom URL */
257
- endpoint: string;
258
- /** LLM model to use */
259
- model: string;
260
- /** System message to describe to the AI how to behave. */
261
- systemMessage: string;
262
- /** User ID used to uniquely identify users in ChatGPT's API */
263
- userID: string;
264
- /** If true, streams the text responses from the API */
265
- stream: boolean;
266
- /** Amount of estimated tokens to keep when trimming */
267
- maxTokens: number;
268
- /** Callback before the AI sends info to the LLM */
269
- onBeforeMessageProcessing?: () => void;
270
- /** Callback when a message from the AI is returned. If isChunk is true, it may be incomplete and be called again with more updates. */
271
- onAIMessage?: (text: string, isChunk: boolean) => void;
272
- /** Callback when the AI starts performing an action */
273
- onAIToolStart?: (toolName: string, input: any) => void;
274
- }
275
- /** ChatGPT tool config */
276
- interface ChatGPTToolConfig {
277
- /** Name of the tool */
278
- name: string;
279
- /** Description of the tool */
280
- description: string;
281
- /** Parameters for the tool */
282
- params: JSONSchema7;
283
- /** Callback function to process the tool */
284
- callback: (params: any) => any;
285
- /** If true, this tool call will be removed from the message history after it is executed. */
286
- removeFromMessageHistory?: boolean;
287
- /** If true, this item can be removed if there's not enough context available. */
288
- canRemove?: boolean;
289
- /** Misc app context */
290
- [key: string]: any;
304
+ instruct(config: IntelliWeaveInstructConfig): Promise<string>;
291
305
  }
292
- /** ChatGPT message */
293
- interface ChatGPTMessage {
294
- /** Role of the message */
295
- role: 'user' | 'assistant' | 'system' | 'tool';
296
- /** Content of the message */
297
- content: string;
298
- /** Tool call ID */
299
- tool_call_id?: string;
300
- /** Tool calls made by the AI */
301
- tool_calls?: any[];
306
+ /** Config for any instruct call */
307
+ interface IntelliWeaveInstructConfig {
308
+ /** Instruction */
309
+ instruction: string;
310
+ /** Input data or query to process */
311
+ data: any;
312
+ /** Whether to allow the AI to use the knowledge base or not. If false, the AI will not use the knowledge base. */
313
+ allowKB?: boolean;
314
+ /** Callback that will be called when streaming the response. Each call will contain the full text that has been generated so far. */
315
+ callback?: (txt: string) => void;
302
316
  }
317
+
303
318
  /**
304
- * API for interacting with ChatGPT APIs.
319
+ * Allows an MCP server to be used as a knowledge source for IntelliWeave.
305
320
  */
306
- declare class ChatGPT {
307
- /** ID */
308
- id: string;
309
- /** Metadata */
310
- metadata: any;
311
- /** Config */
312
- config: ChatGPTConfig;
313
- /** The maximum tool calls in sequence the AI can make before an error is thrown. */
314
- maxToolCallsPerMessage: number;
321
+ declare class MCPKnowledgeClient {
322
+ /** MCP client */
323
+ client?: Client;
324
+ /** All tools discovered on the MCP server. Only available after connect() has completed. */
325
+ tools: Awaited<ReturnType<Client['listTools']>>['tools'];
326
+ /** All toold discovered, mapped to IntelliWeave knowledge base actions */
327
+ iwActions: KnowledgeBaseItem[];
315
328
  /** Statistics */
316
329
  stats: {
317
- /** Total tokens used this session */
318
- tokensUsed: number;
330
+ toolsCalled: number;
331
+ };
332
+ /** Configuration */
333
+ config: {
334
+ /** Source ID */
335
+ id?: string;
336
+ /** URL to the MCP server endpoint */
337
+ baseURL?: string;
338
+ /** Custom connection function. If specified, baseURL is optional. */
339
+ connect?: () => Promise<Client>;
340
+ /**
341
+ * The name of the tool which provides knowledge searching. If specified, the search() will exclude this function and instead
342
+ * call it and show returned results. If not specified, the search() will just return all tools.
343
+ */
344
+ searchToolName?: string;
345
+ /** Keep search function available for the AI to use. */
346
+ searchToolVisible?: boolean;
319
347
  };
320
- /** Token window management */
321
- tokenWindow: TokenWindow;
322
- /** Token window group used for the context message */
323
- get contextGroup(): TokenWindowGroup<any>;
324
- /** Token window group used for tools / actions */
325
- get toolGroup(): TokenWindowGroup<ChatGPTToolConfig>;
326
- /** Token window group used for messages */
327
- get messageGroup(): TokenWindowGroup<ChatGPTMessage>;
328
348
  /** Constructor */
329
- constructor(config: ChatGPTConfig);
330
- /** Send a message, and get the response */
331
- sendMessage(message: string): Promise<string>;
332
- /** Insert a message as if the assistant has written it */
333
- addAssistantMessage(message: string): void;
334
- /** Insert a message sent from a user. Note that doing this instead of using `sendMessage()` means you'll need to manually call `await processMessages()` and then `getLatestMessage()` to get the response. */
335
- addUserMessage(message: string): void;
336
- /** Get all messages */
337
- getMessages(): ChatGPTMessage[];
338
- /** Get latest message */
339
- getLatestMessage(): ChatGPTMessage | undefined;
340
- /** @private Process messages in the chat history */
341
- processMessages(): Promise<void>;
342
- /** Trim message list */
343
- trimMessages(): Promise<void>;
344
- /** @private Send message list to the API and store the response */
345
- sendToAPI(generatePayloadOnly?: boolean): Promise<any>;
346
- /** Process incoming message from the AI. Can be used to respond to encoded actions in the text response. */
347
- processIncomingMessage(message: string): void;
348
- /** Register a tool. */
349
- registerTool(tool: ChatGPTToolConfig): void;
350
- /** @private Process a tool call request from the AI */
351
- processToolCall(toolCall: any): Promise<void>;
352
- /** Reset the conversation */
353
- resetConversation(): void;
349
+ constructor(config: MCPKnowledgeClient['config']);
350
+ /** In-progress connection attempt */
351
+ private connectionPromise?;
352
+ /** Connect to the client */
353
+ connect(): Promise<Client<{
354
+ method: string;
355
+ params?: {
356
+ [x: string]: unknown;
357
+ _meta?: {
358
+ [x: string]: unknown;
359
+ progressToken?: string | number | undefined;
360
+ } | undefined;
361
+ } | undefined;
362
+ }, {
363
+ method: string;
364
+ params?: {
365
+ [x: string]: unknown;
366
+ _meta?: {
367
+ [x: string]: unknown;
368
+ } | undefined;
369
+ } | undefined;
370
+ }, {
371
+ [x: string]: unknown;
372
+ _meta?: {
373
+ [x: string]: unknown;
374
+ } | undefined;
375
+ }>>;
376
+ connectInternal(): Promise<Client<{
377
+ method: string;
378
+ params?: {
379
+ [x: string]: unknown;
380
+ _meta?: {
381
+ [x: string]: unknown;
382
+ progressToken?: string | number | undefined;
383
+ } | undefined;
384
+ } | undefined;
385
+ }, {
386
+ method: string;
387
+ params?: {
388
+ [x: string]: unknown;
389
+ _meta?: {
390
+ [x: string]: unknown;
391
+ } | undefined;
392
+ } | undefined;
393
+ }, {
394
+ [x: string]: unknown;
395
+ _meta?: {
396
+ [x: string]: unknown;
397
+ } | undefined;
398
+ }>>;
399
+ /** Disconnect from server */
400
+ disconnect(): Promise<void>;
401
+ /** Fetch list of tools from the MCP server */
402
+ private fetchTools;
403
+ /** Cache last search result */
404
+ lastSearchQuery: string;
405
+ lastSearchResults: KnowledgeBaseItem[];
406
+ /** Perform a search query */
407
+ search(query: string): Promise<KnowledgeBaseItem[]>;
408
+ /** Perform search using the configured search function */
409
+ private performSearchCall;
410
+ /** Perform tool call. */
411
+ private performToolCall;
354
412
  }
355
413
 
356
414
  /**
357
- * Speech output
358
- *
359
- * - event `speechstart` - When the speech starts
360
- * - event `speechend` - When the speech ends
415
+ * This class helps organize groups of tokenized text along with removing items when the window is full.
361
416
  */
362
- declare class WebWeaverSpeechOutput extends EventTarget {
363
- /** Reference to the AI */
364
- private ai?;
365
- /** Constructor */
366
- constructor(ai: IntelliWeave);
367
- /** Called when the AI speaks */
368
- onTextOutputFromAI(e: CustomEvent): void;
369
- /** Current player vars */
370
- private currentPlayerVolume?;
371
- private currentPlayer?;
372
- /** The audio analyser node */
373
- private analyserNode?;
374
- /** The audio analyser buffer */
375
- private analyserBuffer?;
376
- /** @private Maximum volume heard this session */
377
- private maxVolumeHeard;
378
- /** Get current (realtime) audio output volume level, from 0 to 1 */
379
- get volumeLevel(): number;
380
- /** Speak the text */
381
- speak(text: string): Promise<void>;
382
- private _speakWithLock;
383
- /** True if currently playing audio */
384
- get isSpeaking(): boolean;
385
- /** Interrupt the previously playing audio */
386
- interrupt(): Promise<void>;
387
- /** Called when the speech output ends */
388
- onSpeechEnd(): void;
417
+ declare class TokenWindow {
418
+ /** Token window size */
419
+ size: number;
420
+ /** Token groups */
421
+ groups: TokenWindowGroup<any>[];
422
+ /** Create a new group */
423
+ createGroup(id: string): TokenWindowGroup<unknown>;
424
+ /** Get a group */
425
+ group<CustomDataType>(id: string): TokenWindowGroup<CustomDataType> | undefined;
426
+ /** Calculate current tokens in all groups */
427
+ countTokens(): number;
428
+ /** Remove overflow from all groups. */
429
+ removeOverflow(): void;
430
+ /** Remove one overflow item. Returns null if no items were able to be removed. */
431
+ private removeOneItem;
389
432
  }
390
-
391
- /**
392
- * An AudioWorklet module that records data from input and sends it to the host.
393
- *
394
- * - event `data` - Fired when data is available to be read.
395
- */
396
- declare class PCMReceiverNode extends AudioWorkletNode {
397
- /** @type {'int16' | 'float32'} The output data format */
398
- format: string;
433
+ /** A token group. */
434
+ declare class TokenWindowGroup<CustomDataType> {
435
+ /** Group ID */
436
+ id: string;
437
+ /** List of items */
438
+ items: TokenWindowGroupItem<CustomDataType>[];
399
439
  /**
400
- * Creates a new PCMRecorderNode ready to receive PCM data.
401
- *
402
- * @param context - The audio context to use.
403
- * @param sampleRate - The sample rate of the output data stream.
404
- * @param format - The format of the output data stream.
405
- * @param bufferSize - The size of the output buffer in elements (Int16Array or Float32Array items, depending on `format`).
440
+ * Weight controls how many items from this group should be kept in relation to the entire window. For example if all
441
+ * groups have a weight of 1, each group remove items equally if full. If one has a weight of 2 while the rest are 1,
442
+ * that group will be allowed to keep double the amount of items.
406
443
  */
407
- constructor(context: AudioContext, sampleRate: number, format: 'int16' | 'int64' | 'float32', bufferSize: number);
408
- /** @private Called when a message is received from the worklet */
409
- onWorkletMessage(e: MessageEvent): void;
410
- /** Called when data is received */
411
- onData(buffer: Float32Array | Int16Array | BigInt64Array): void;
412
- }
413
-
414
- /**
415
- * An AudioNode which sends events for when speech is detected
416
- *
417
- * - event `speechstart` - Fired when speech is detected
418
- * - event `speechend` - Fired when speech ends
419
- */
420
- declare class VoiceDetectionNode extends PCMReceiverNode {
421
- /** True if voice is currently being detected */
422
- isVoiceActive: boolean;
423
- /** True if voice is active but may be ending soon */
424
- get isVoicePossiblyEnding(): boolean;
425
- /** Last date that voice was detected */
426
- lastVoiceActiveDate: number;
427
- /** Amount of time to wait after voice detection to detect that it has ended */
428
- voiceEndTimeout: number;
429
- /** Detection sensitivity, if the detection model outputs a number bigger than this it will be considered voice */
430
- sensitivity: number;
431
- /** Sensitivity threshold to end speaking */
432
- sentivityEnd: number;
433
- /** VAD model */
434
- static vadModelURL: string;
435
- /** Loaded VAD model */
436
- private vad?;
437
- /** Sample rate */
438
- get sampleRate(): 8000 | 16000;
439
- /** Number of samples */
440
- get numberOfSamples(): number;
441
- /** Number of sample chunks */
442
- get numberOfSampleChunks(): number;
443
- /** Output buffer size */
444
- get outputBufferSize(): number;
445
- /** True if the VAD model has been loaded */
446
- get isModelLoaded(): boolean;
447
- /** The time when to next reset the VAD model */
448
- nextVadReset: number;
449
- /** The current probability of active voice */
450
- currentProbability: number;
451
- /** Constructor */
452
- constructor(audioContext: AudioContext);
453
- /** Start loading */
454
- loadModel(): Promise<void>;
455
- private _lastVoiceActive;
456
- /** Called when data is received */
457
- onData(buffer: Float32Array): Promise<void>;
458
- /** Called when speech is detected */
459
- onSpeechStart(): void;
460
- /** Called when speech ends */
461
- onSpeechEnd(): void;
444
+ weight: number;
445
+ /** Current total token count, computed automatically. Don't update this value manually. */
446
+ tokenCount: number;
447
+ /** Group item separator */
448
+ separator: string;
449
+ /** Token count padding added to each item. */
450
+ private itemPadding;
451
+ /** Sets the token count padding added to each item. Useful if you don't know exactly what will be added by the LLM host. */
452
+ setItemPadding(padding: number): this;
453
+ /** Sort function */
454
+ private sortFunction;
455
+ /** Set sort function */
456
+ sortBy(sortFunction: (a: TokenWindowGroupItem<CustomDataType>, b: TokenWindowGroupItem<CustomDataType>) => number): this;
457
+ /** Set separator */
458
+ setSeparator(separator: string): this;
459
+ /** Set weight */
460
+ setWeight(weight: number): this;
461
+ /** Recalculate all tokens. Note this may take a while. */
462
+ recalculateTokens(): void;
463
+ /** Add an item to the group */
464
+ add(item: string | Omit<Optional<TokenWindowGroupItem<CustomDataType>, 'id' | 'dateAdded' | 'sortOrder'>, 'tokenCount'>): TokenWindowGroupItem<CustomDataType>;
465
+ /** Get all items as a string */
466
+ getAllAsString(): string;
467
+ /** Get all items. Doesn't return disabled items. */
468
+ getAll(): TokenWindowGroupItem<CustomDataType>[];
469
+ /** Remove all items from this group */
470
+ empty(): void;
471
+ }
472
+ /** Token group item */
473
+ interface TokenWindowGroupItem<CustomDataType> {
474
+ /** Each item must have a unique ID. */
475
+ id: string;
476
+ /** The string content of the item */
477
+ content: string;
478
+ /** True if this item should never be removed */
479
+ cannotRemove?: boolean;
480
+ /** Sorting order. If not specified, uses dateAdded instead. */
481
+ sortOrder: number;
482
+ /** Date this item was added */
483
+ dateAdded: number;
484
+ /** Token count in the content */
485
+ tokenCount: number;
486
+ /** Anything to attach to this item */
487
+ customData?: CustomDataType;
488
+ /** If disabled, this item will not be included and will not add tot he token count. */
489
+ disabled?: boolean;
462
490
  }
463
491
 
492
+ // ==================================================================================================
493
+ // JSON Schema Draft 07
494
+ // ==================================================================================================
495
+ // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
496
+ // --------------------------------------------------------------------------------------------------
497
+
464
498
  /**
465
- * An AudioNode which isolates speech and outputs the audio data. Since we are reusing the VAD model node,
466
- * output data is in 8000Hz Float32 format.
467
- *
468
- * - event `voicedata` - Fired when a chunk of voice is detected. `data` contains the recorded chunk of voice in a Float32Array.
469
- * - event `voicedataend` - Fired when this chunk of voice ends. `data` contains an array of Float32Array containing the entirety of the recorded voice.
499
+ * Primitive type
500
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
470
501
  */
471
- declare class VoiceChunkOutputNode extends VoiceDetectionNode {
472
- /** Stored buffers */
473
- buffers: Float32Array[];
474
- /** Recorded audio chunks with voice in it */
475
- recordedBuffers: Float32Array[];
476
- /** Last active state */
477
- _voiceRecording: boolean;
478
- /** Amount of audio data in the buffer, in seconds */
479
- get bufferDuration(): number;
480
- /** Amount of data to keep from before the user started speaking */
481
- backBufferDurationSeconds: number;
482
- /** Called when data is received */
483
- onData(buffer: Float32Array): Promise<void>;
484
- /** Called when a chunk of voice is recorded */
485
- onVoiceChunk(buffer: Float32Array): void;
486
- /** Called when the voice recording ends */
487
- onVoiceEnd(buffers: Float32Array[]): void;
488
- }
502
+ type JSONSchema7TypeName =
503
+ | "string" //
504
+ | "number"
505
+ | "integer"
506
+ | "boolean"
507
+ | "object"
508
+ | "array"
509
+ | "null";
489
510
 
490
511
  /**
491
- * This AudioNode uses OpenAI's Whisper model to transcribe spoken speech to text.
492
- *
493
- * - event `transcription` - Fired when a transcription is ready. `text` contains the transcribed text.
512
+ * Primitive type
513
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
494
514
  */
495
- declare class OpenAITranscriptionNode extends VoiceChunkOutputNode {
496
- /** OpenAI API key */
497
- apiKey: string;
498
- /** Pending buffers */
499
- private pendingBuffers;
500
- /** Last request */
501
- private lastRequestAbortController?;
502
- /** True if currently transcribing */
503
- isTranscribing: boolean;
504
- /** Constructor */
505
- constructor(audioContext: AudioContext, apiKey: string);
506
- /** Called when the voice recording ends */
507
- onVoiceEnd(buffers: Float32Array[]): Promise<void>;
508
- /** Called when a transcription is ready */
509
- onVoiceTranscription(text: string): void;
515
+ type JSONSchema7Type =
516
+ | string //
517
+ | number
518
+ | boolean
519
+ | JSONSchema7Object
520
+ | JSONSchema7Array
521
+ | null;
522
+
523
+ // Workaround for infinite type recursion
524
+ interface JSONSchema7Object {
525
+ [key: string]: JSONSchema7Type;
510
526
  }
511
527
 
528
+ // Workaround for infinite type recursion
529
+ // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
530
+ interface JSONSchema7Array extends Array<JSONSchema7Type> {}
531
+
512
532
  /**
513
- * This AudioNode uses IntelliWeave's servers to transcribe spoken speech to text.
533
+ * Meta schema
514
534
  *
515
- * - event `transcription` - Fired when a transcription is ready. `text` contains the transcribed text.
535
+ * Recommended values:
536
+ * - 'http://json-schema.org/schema#'
537
+ * - 'http://json-schema.org/hyper-schema#'
538
+ * - 'http://json-schema.org/draft-07/schema#'
539
+ * - 'http://json-schema.org/draft-07/hyper-schema#'
540
+ *
541
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
516
542
  */
517
- declare class IntelliWeaveTranscriptionNode extends VoiceChunkOutputNode {
518
- /** Debug: Export each recording as a wav file for download */
519
- static debugExportWav: boolean;
520
- /** Server address for transcription */
521
- apiAddress: string;
522
- /** OpenAI API key */
523
- apiKey: string;
524
- /** WebSocket connection */
525
- private ws?;
526
- /** True if currently transcribing */
527
- isTranscribing: boolean;
528
- /** WebSocket shutdown timer */
529
- private shutdownTimer?;
530
- /** Constructor */
531
- constructor(audioContext: AudioContext, apiKey: string);
532
- /** Called when a voice chunk is received */
533
- onVoiceChunk(buffer: Float32Array): Promise<void>;
534
- /** Called when the voice recording ends */
535
- onVoiceEnd(buffers: Float32Array[]): Promise<void>;
536
- /** Called when a transcription is ready */
537
- onVoiceTranscription(text: string): void;
538
- /** Called when the WebSocket is closed */
539
- onSocketClose(): void;
540
- }
543
+ type JSONSchema7Version = string;
541
544
 
542
545
  /**
543
- * Handles speech recognition from the microphone
544
- *
545
- * - event `speechstart` - We have detected the user started speaking
546
- * - event `speechend` - We have detected the user stopped speaking
547
- * - event `speech` - Speech recognition result
548
- * - event `start` - Speech recognition started
549
- * - event `end` - Speech recognition ended
546
+ * JSON Schema v7
547
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
550
548
  */
551
- declare class WebWeaverSpeechRecognition extends EventTarget {
552
- /** Reference to the AI */
553
- ai?: IntelliWeave;
554
- /** True if recognition is running */
555
- isRunning: boolean;
556
- /** The audio analyser node */
557
- private analyserNode?;
558
- /** The audio analyser buffer */
559
- private analyserBuffer?;
560
- /** The microphone stream */
561
- micStream?: MediaStream;
562
- /** Recording start time for tracking duration */
563
- private recordingStartTime?;
564
- /** Returns true if speech recognition is supported by this persona and browser */
565
- get isSupported(): boolean;
566
- /** Currently active voice detection node */
567
- voiceDetection?: IntelliWeaveTranscriptionNode | OpenAITranscriptionNode;
568
- /** Constructor */
569
- constructor(ai: IntelliWeave);
570
- private _skipEvents;
571
- /** Start recognition */
572
- start(): Promise<void>;
573
- /** Stop recognition */
574
- stop(): void;
575
- /** @private Maximum volume heard this session */
576
- maxVolumeHeard: number;
577
- /** Get current (realtime) microphone volume level, from 0 to 1 */
578
- get volumeLevel(): number;
579
- /** True if currently detecting words being spoken */
580
- get wordsCurrentlyBeingSpoken(): boolean;
581
- /** True if currently transcribing voice to text */
582
- get isTranscribing(): boolean;
583
- /** Called when speech has been recorded */
584
- onTranscription(e: CustomEvent): void;
585
- /** Called to reset the speech recognizer */
586
- reset(): Promise<void>;
587
- }
549
+ type JSONSchema7Definition = JSONSchema7 | boolean;
550
+ interface JSONSchema7 {
551
+ $id?: string | undefined;
552
+ $ref?: string | undefined;
553
+ $schema?: JSONSchema7Version | undefined;
554
+ $comment?: string | undefined;
555
+
556
+ /**
557
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
558
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
559
+ */
560
+ $defs?: {
561
+ [key: string]: JSONSchema7Definition;
562
+ } | undefined;
563
+
564
+ /**
565
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
566
+ */
567
+ type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
568
+ enum?: JSONSchema7Type[] | undefined;
569
+ const?: JSONSchema7Type | undefined;
570
+
571
+ /**
572
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
573
+ */
574
+ multipleOf?: number | undefined;
575
+ maximum?: number | undefined;
576
+ exclusiveMaximum?: number | undefined;
577
+ minimum?: number | undefined;
578
+ exclusiveMinimum?: number | undefined;
579
+
580
+ /**
581
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
582
+ */
583
+ maxLength?: number | undefined;
584
+ minLength?: number | undefined;
585
+ pattern?: string | undefined;
586
+
587
+ /**
588
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
589
+ */
590
+ items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
591
+ additionalItems?: JSONSchema7Definition | undefined;
592
+ maxItems?: number | undefined;
593
+ minItems?: number | undefined;
594
+ uniqueItems?: boolean | undefined;
595
+ contains?: JSONSchema7Definition | undefined;
596
+
597
+ /**
598
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
599
+ */
600
+ maxProperties?: number | undefined;
601
+ minProperties?: number | undefined;
602
+ required?: string[] | undefined;
603
+ properties?: {
604
+ [key: string]: JSONSchema7Definition;
605
+ } | undefined;
606
+ patternProperties?: {
607
+ [key: string]: JSONSchema7Definition;
608
+ } | undefined;
609
+ additionalProperties?: JSONSchema7Definition | undefined;
610
+ dependencies?: {
611
+ [key: string]: JSONSchema7Definition | string[];
612
+ } | undefined;
613
+ propertyNames?: JSONSchema7Definition | undefined;
588
614
 
589
- /** Handles creating and managing the AudioContext */
590
- declare class AudioSystem {
591
- /** Reference to the AI */
592
- private ai?;
593
- /** The speech recognition module. */
594
- speechRecognition: WebWeaverSpeechRecognition;
595
- /** The speech output module. */
596
- speechOutput: WebWeaverSpeechOutput;
597
- /** The audio context */
598
- context?: AudioContext;
599
- /** List of active named locks */
600
- locks: string[];
601
- /** Returns true if speech recognition and output is supported by this persona and browser */
602
- static get isSupported(): boolean;
603
- /** Constructor */
604
- constructor(ai: IntelliWeave);
605
- /** Create a named lock to enable the audio system */
606
- beginAccess(namedLock: string): Promise<void>;
607
- /** Stop accessing the audio system */
608
- endAccess(namedLock: string): void;
609
- }
615
+ /**
616
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
617
+ */
618
+ if?: JSONSchema7Definition | undefined;
619
+ then?: JSONSchema7Definition | undefined;
620
+ else?: JSONSchema7Definition | undefined;
610
621
 
611
- /**
612
- * This class allows you to use the AI as a logic engine, data extractor, etc.
613
- */
614
- declare class AILogic {
615
- /** Reference to the AI */
616
- private ai?;
617
- /** Constructor */
618
- constructor(ai: IntelliWeave);
619
- /** Ask the AI a yes/no question associated with the specified data. Data must be JSON-serializable, or a string of any kind of data. */
620
- boolean(config: IntelliWeaveInstructConfig): Promise<boolean>;
621
- /** Ask the AI to select a choice from a list of options. */
622
- choose(config: IntelliWeaveInstructConfig & {
623
- /** List of choices the AI can pick from. */
624
- options: string[];
625
- }): Promise<string | undefined>;
626
622
  /**
627
- * Ask the AI to extract data from input data. The AI will return the extracted data. Possibly an array of multiple extractions.
623
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
628
624
  */
629
- extract<T = any>(config: IntelliWeaveInstructConfig & {
630
- /** Allow multiple items to be returned. If true, returns an array instead of an object. */
631
- allowMultiple?: boolean;
632
- /** Fields to extract in each object. */
633
- extractions: {
634
- /** Field name */
635
- name: string;
636
- /** Field data type */
637
- type: 'string' | 'number' | 'boolean' | 'date' | 'email' | 'phone' | 'address';
638
- /** Describe to the AI what data to put in this field. */
639
- description?: string;
640
- }[];
641
- }): Promise<T[]>;
625
+ allOf?: JSONSchema7Definition[] | undefined;
626
+ anyOf?: JSONSchema7Definition[] | undefined;
627
+ oneOf?: JSONSchema7Definition[] | undefined;
628
+ not?: JSONSchema7Definition | undefined;
629
+
642
630
  /**
643
- * Generate a Markdown document based on the data from the user.
644
- *
645
- * @param config Instruct config.
646
- * @returns A markdown document.
631
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
647
632
  */
648
- generateMarkdown(config: Omit<IntelliWeaveInstructConfig, 'instruction'>): Promise<string>;
633
+ format?: string | undefined;
634
+
649
635
  /**
650
- * Perform an instruction.
651
- *
652
- * @param config Instruct config.
653
- * @returns The final response from the AI.
636
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
654
637
  */
655
- instruct(config: IntelliWeaveInstructConfig): Promise<string>;
656
- }
657
- /** Config for any instruct call */
658
- interface IntelliWeaveInstructConfig {
659
- /** Instruction */
660
- instruction: string;
661
- /** Input data or query to process */
662
- data: any;
663
- /** Whether to allow the AI to use the knowledge base or not. If false, the AI will not use the knowledge base. */
664
- allowKB?: boolean;
665
- /** Callback that will be called when streaming the response. Each call will contain the full text that has been generated so far. */
666
- callback?: (txt: string) => void;
638
+ contentMediaType?: string | undefined;
639
+ contentEncoding?: string | undefined;
640
+
641
+ /**
642
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
643
+ */
644
+ definitions?: {
645
+ [key: string]: JSONSchema7Definition;
646
+ } | undefined;
647
+
648
+ /**
649
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
650
+ */
651
+ title?: string | undefined;
652
+ description?: string | undefined;
653
+ default?: JSONSchema7Type | undefined;
654
+ readOnly?: boolean | undefined;
655
+ writeOnly?: boolean | undefined;
656
+ examples?: JSONSchema7Type | undefined;
667
657
  }
668
658
 
659
+ /** Chat config options */
660
+ interface ChatBaseConfig {
661
+ /** API key */
662
+ apiKey: string;
663
+ /** Provider ID */
664
+ providerID?: string;
665
+ /** Endpoint URL if using a custom URL */
666
+ endpoint: string;
667
+ /** LLM model to use */
668
+ model: string;
669
+ /** System message to describe to the AI how to behave. */
670
+ systemMessage: string;
671
+ /** User ID used to uniquely identify users in ChatGPT's API */
672
+ userID: string;
673
+ /** If true, streams the text responses from the API */
674
+ stream: boolean;
675
+ /** Amount of estimated tokens to keep when trimming */
676
+ maxTokens: number;
677
+ /** Callback before the AI sends info to the LLM */
678
+ onBeforeMessageProcessing?: () => void;
679
+ /** Callback when a message from the AI is returned. If isChunk is true, it may be incomplete and be called again with more updates. */
680
+ onAIMessage?: (text: string, isChunk: boolean) => void;
681
+ /** Callback when the AI starts performing an action */
682
+ onAIToolStart?: (toolName: string, input: any) => void;
683
+ }
684
+ /** Chat tool config */
685
+ interface ChatBaseToolConfig {
686
+ /** Name of the tool, eg "perform_search" */
687
+ name: string;
688
+ /** Description of the tool */
689
+ description: string;
690
+ /** Parameters for the tool */
691
+ params: JSONSchema7;
692
+ /** Callback function to process the tool */
693
+ callback: (params: any) => any;
694
+ /** If true, this tool call will be removed from the message history after it is executed. */
695
+ removeFromMessageHistory?: boolean;
696
+ /** If true, this item can be removed if there's not enough context available. */
697
+ canRemove?: boolean;
698
+ /** Misc app context */
699
+ [key: string]: any;
700
+ }
669
701
  /**
670
- * Allows an MCP server to be used as a knowledge source for IntelliWeave.
702
+ * API for interacting with chat APIs.
671
703
  */
672
- declare class MCPKnowledgeClient {
673
- /** MCP client */
674
- client?: Client;
675
- /** All tools discovered on the MCP server. Only available after connect() has completed. */
676
- tools: Awaited<ReturnType<Client['listTools']>>['tools'];
677
- /** All toold discovered, mapped to IntelliWeave knowledge base actions */
678
- iwActions: KnowledgeBaseItem[];
704
+ declare class ChatBase<
705
+ /** Format for messages in the token window */
706
+ MessageFormat = any,
707
+ /** Optional extended config */
708
+ ConfigFormat extends ChatBaseConfig = ChatBaseConfig> {
709
+ /** ID */
710
+ id: string;
711
+ /** Metadata */
712
+ metadata: any;
713
+ /** Config */
714
+ config: ConfigFormat;
715
+ /** The maximum tool calls in sequence the AI can make before an error is thrown. */
716
+ maxToolCallsPerMessage: number;
679
717
  /** Statistics */
680
718
  stats: {
681
- toolsCalled: number;
682
- };
683
- /** Configuration */
684
- config: {
685
- /** Source ID */
686
- id?: string;
687
- /** URL to the MCP server endpoint */
688
- baseURL?: string;
689
- /** Custom connection function. If specified, baseURL is optional. */
690
- connect?: () => Promise<Client>;
691
- /**
692
- * The name of the tool which provides knowledge searching. If specified, the search() will exclude this function and instead
693
- * call it and show returned results. If not specified, the search() will just return all tools.
694
- */
695
- searchToolName?: string;
696
- /** Keep search function available for the AI to use. */
697
- searchToolVisible?: boolean;
719
+ /** Total tokens used this session */
720
+ tokensUsed: number;
698
721
  };
722
+ /** Token window management */
723
+ tokenWindow: TokenWindow;
724
+ /** Token window group used for the context message */
725
+ get contextGroup(): TokenWindowGroup<any>;
726
+ /** Token window group used for tools / actions */
727
+ get toolGroup(): TokenWindowGroup<ChatBaseToolConfig>;
728
+ /** Token window group used for messages */
729
+ get messageGroup(): TokenWindowGroup<MessageFormat>;
699
730
  /** Constructor */
700
- constructor(config: MCPKnowledgeClient['config']);
701
- /** In-progress connection attempt */
702
- private connectionPromise?;
703
- /** Connect to the client */
704
- connect(): Promise<Client<{
705
- method: string;
706
- params?: {
707
- [x: string]: unknown;
708
- _meta?: {
709
- [x: string]: unknown;
710
- progressToken?: string | number | undefined;
711
- } | undefined;
712
- } | undefined;
713
- }, {
714
- method: string;
715
- params?: {
716
- [x: string]: unknown;
717
- _meta?: {
718
- [x: string]: unknown;
719
- } | undefined;
720
- } | undefined;
721
- }, {
722
- [x: string]: unknown;
723
- _meta?: {
724
- [x: string]: unknown;
725
- } | undefined;
726
- }>>;
727
- connectInternal(): Promise<Client<{
728
- method: string;
729
- params?: {
730
- [x: string]: unknown;
731
- _meta?: {
732
- [x: string]: unknown;
733
- progressToken?: string | number | undefined;
734
- } | undefined;
735
- } | undefined;
736
- }, {
737
- method: string;
738
- params?: {
739
- [x: string]: unknown;
740
- _meta?: {
741
- [x: string]: unknown;
742
- } | undefined;
743
- } | undefined;
744
- }, {
745
- [x: string]: unknown;
746
- _meta?: {
747
- [x: string]: unknown;
748
- } | undefined;
749
- }>>;
750
- /** Disconnect from server */
751
- disconnect(): Promise<void>;
752
- /** Fetch list of tools from the MCP server */
753
- private fetchTools;
754
- /** Cache last search result */
755
- lastSearchQuery: string;
756
- lastSearchResults: KnowledgeBaseItem[];
757
- /** Perform a search query */
758
- search(query: string): Promise<KnowledgeBaseItem[]>;
759
- /** Perform search using the configured search function */
760
- private performSearchCall;
761
- /** Perform tool call. */
762
- private performToolCall;
731
+ constructor(config: ConfigFormat);
732
+ /** Send a message, and get the response */
733
+ sendMessage(message: string): Promise<string>;
734
+ /** Add a user message to the message history */
735
+ addUserMessage(message: string): void;
736
+ /** Add an assistant message to the message history */
737
+ addAssistantMessage(message: string): void;
738
+ /** Process incoming message from the AI. Can be used to respond to encoded actions in the text response. */
739
+ onBeforeIncomingMessage(message: MessageFormat): void;
740
+ /** Reset the conversation */
741
+ resetConversation(): void;
742
+ /** Trim message list */
743
+ trimMessages(): Promise<void>;
744
+ /** Register a tool. */
745
+ registerTool(tool: ChatBaseToolConfig): TokenWindowGroupItem<ChatBaseToolConfig>;
763
746
  }
764
747
 
765
748
  /** Persona config received from the hub */
766
749
  interface WebWeaverGPTConfig {
767
750
  /** ID */
768
751
  id: string;
769
- /** ChatGPT config */
770
- model: ChatGPTConfig;
752
+ /** Chat API config */
753
+ model: ChatBaseConfig;
771
754
  /** If true, message history will be sent to the IntelliWeave hub for analysis */
772
755
  analytics?: boolean;
773
756
  /** Persona name */
@@ -814,7 +797,7 @@ interface WebWeaverGPTConfig {
814
797
  * - event `webweaver_loaded` - Fired when the AI is loaded with a new configuration. This is a global event that is fired on the window object.
815
798
  * - event `webweaver_error` - Fired when an error occurs during loading. This is a global event that is fired on the window object.
816
799
  * - event `input` - Fired when the user sends a message to the AI.
817
- * - event `output` - Fired when the AI sends a message back to the user. If `event.detail.isChunk` is true, the message is incomplete and will be followed by more chunks.
800
+ * - event `output` - Fired when the AI sends a message back to the user. If `event.detail.isChunk` is true, the message is incomplete and will be followed by more events.
818
801
  * - event `toolstart` - Fired when the AI starts performing an action.
819
802
  * - event `tool` - Fired when the AI finishes performing an action.
820
803
  */
@@ -824,7 +807,7 @@ declare class IntelliWeave extends EventTarget {
824
807
  /** Callback when a message from the AI is returned. If isChunk is true, it may be incomplete and be called again with more updates. */
825
808
  onAIMessage?: (text: string, isChunk: boolean) => void;
826
809
  /** Callback when the AI starts performing an action */
827
- onAIToolStart?: ChatGPTConfig['onAIToolStart'];
810
+ onAIToolStart?: ChatBaseConfig['onAIToolStart'];
828
811
  /** Current conversation ID */
829
812
  conversationID: string;
830
813
  /** Knowledge database interface */
@@ -840,11 +823,11 @@ declare class IntelliWeave extends EventTarget {
840
823
  /** Available LLMs */
841
824
  models: {
842
825
  id: string;
843
- config: ChatGPTConfig;
826
+ config: ChatBaseConfig;
844
827
  priority?: number;
845
828
  }[];
846
829
  /** Current LLM */
847
- currentModel?: ChatGPT;
830
+ currentModel?: ChatBase;
848
831
  /** The audio system. Set this to a new instance of AudioSystem to enable audio support */
849
832
  audio: AudioSystem | null;
850
833
  /** Silero VAD model blob */
@@ -874,7 +857,6 @@ declare class IntelliWeave extends EventTarget {
874
857
  getContextPrefix(): Promise<string>;
875
858
  /** Get system message to send to the AI */
876
859
  onBeforeMessageProcessing(): Promise<void>;
877
- private _lastOutput?;
878
860
  /** @private Process incoming message from the AI. Can be used to respond to encoded actions in the text response. */
879
861
  processIncomingMessage(message: string, isChunk?: boolean): void;
880
862
  /** True if currently processing a message */
@@ -895,7 +877,7 @@ declare class IntelliWeave extends EventTarget {
895
877
  exportState(): {
896
878
  type: string;
897
879
  conversationID: string;
898
- messages: (ChatGPTMessage | undefined)[] | undefined;
880
+ messages: any[] | undefined;
899
881
  };
900
882
  /** Import conversation state from JSON */
901
883
  importState(state: any): void;