@intelliweave/embedded 1.8.60 → 1.8.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/component/component.d.ts +287 -13
- package/dist/component/component.js +20 -20
- package/dist/intelliweave-wordpress.zip +0 -0
- package/dist/node/node.d.ts +290 -14
- package/dist/node/node.js +11 -11
- package/dist/react/react.d.ts +287 -13
- package/dist/react/react.js +21 -21
- package/dist/script-tag/script-tag.js +58 -51
- package/dist/webpack/index.d.ts +290 -14
- package/dist/webpack/index.js +21 -21
- package/package.json +4 -2
package/dist/node/node.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as onnxruntime_web from 'onnxruntime-web';
|
|
2
2
|
import { InferenceSession, Tensor } from 'onnxruntime-web';
|
|
3
3
|
import { Optional } from 'utility-types';
|
|
4
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* This is a utility class for dealing with the ONNX runtime and model loading.
|
|
@@ -463,6 +464,173 @@ interface TokenWindowGroupItem<CustomDataType> {
|
|
|
463
464
|
disabled?: boolean;
|
|
464
465
|
}
|
|
465
466
|
|
|
467
|
+
// ==================================================================================================
|
|
468
|
+
// JSON Schema Draft 07
|
|
469
|
+
// ==================================================================================================
|
|
470
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
471
|
+
// --------------------------------------------------------------------------------------------------
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Primitive type
|
|
475
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
476
|
+
*/
|
|
477
|
+
type JSONSchema7TypeName =
|
|
478
|
+
| "string" //
|
|
479
|
+
| "number"
|
|
480
|
+
| "integer"
|
|
481
|
+
| "boolean"
|
|
482
|
+
| "object"
|
|
483
|
+
| "array"
|
|
484
|
+
| "null";
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Primitive type
|
|
488
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
489
|
+
*/
|
|
490
|
+
type JSONSchema7Type =
|
|
491
|
+
| string //
|
|
492
|
+
| number
|
|
493
|
+
| boolean
|
|
494
|
+
| JSONSchema7Object
|
|
495
|
+
| JSONSchema7Array
|
|
496
|
+
| null;
|
|
497
|
+
|
|
498
|
+
// Workaround for infinite type recursion
|
|
499
|
+
interface JSONSchema7Object {
|
|
500
|
+
[key: string]: JSONSchema7Type;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Workaround for infinite type recursion
|
|
504
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
505
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Meta schema
|
|
509
|
+
*
|
|
510
|
+
* Recommended values:
|
|
511
|
+
* - 'http://json-schema.org/schema#'
|
|
512
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
513
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
514
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
515
|
+
*
|
|
516
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
517
|
+
*/
|
|
518
|
+
type JSONSchema7Version = string;
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* JSON Schema v7
|
|
522
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
523
|
+
*/
|
|
524
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
525
|
+
interface JSONSchema7 {
|
|
526
|
+
$id?: string | undefined;
|
|
527
|
+
$ref?: string | undefined;
|
|
528
|
+
$schema?: JSONSchema7Version | undefined;
|
|
529
|
+
$comment?: string | undefined;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
533
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
534
|
+
*/
|
|
535
|
+
$defs?: {
|
|
536
|
+
[key: string]: JSONSchema7Definition;
|
|
537
|
+
} | undefined;
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
541
|
+
*/
|
|
542
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
543
|
+
enum?: JSONSchema7Type[] | undefined;
|
|
544
|
+
const?: JSONSchema7Type | undefined;
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
548
|
+
*/
|
|
549
|
+
multipleOf?: number | undefined;
|
|
550
|
+
maximum?: number | undefined;
|
|
551
|
+
exclusiveMaximum?: number | undefined;
|
|
552
|
+
minimum?: number | undefined;
|
|
553
|
+
exclusiveMinimum?: number | undefined;
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
557
|
+
*/
|
|
558
|
+
maxLength?: number | undefined;
|
|
559
|
+
minLength?: number | undefined;
|
|
560
|
+
pattern?: string | undefined;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
564
|
+
*/
|
|
565
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
566
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
|
567
|
+
maxItems?: number | undefined;
|
|
568
|
+
minItems?: number | undefined;
|
|
569
|
+
uniqueItems?: boolean | undefined;
|
|
570
|
+
contains?: JSONSchema7Definition | undefined;
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
574
|
+
*/
|
|
575
|
+
maxProperties?: number | undefined;
|
|
576
|
+
minProperties?: number | undefined;
|
|
577
|
+
required?: string[] | undefined;
|
|
578
|
+
properties?: {
|
|
579
|
+
[key: string]: JSONSchema7Definition;
|
|
580
|
+
} | undefined;
|
|
581
|
+
patternProperties?: {
|
|
582
|
+
[key: string]: JSONSchema7Definition;
|
|
583
|
+
} | undefined;
|
|
584
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
|
585
|
+
dependencies?: {
|
|
586
|
+
[key: string]: JSONSchema7Definition | string[];
|
|
587
|
+
} | undefined;
|
|
588
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
592
|
+
*/
|
|
593
|
+
if?: JSONSchema7Definition | undefined;
|
|
594
|
+
then?: JSONSchema7Definition | undefined;
|
|
595
|
+
else?: JSONSchema7Definition | undefined;
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
599
|
+
*/
|
|
600
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
|
601
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
|
602
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
|
603
|
+
not?: JSONSchema7Definition | undefined;
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
607
|
+
*/
|
|
608
|
+
format?: string | undefined;
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
612
|
+
*/
|
|
613
|
+
contentMediaType?: string | undefined;
|
|
614
|
+
contentEncoding?: string | undefined;
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
618
|
+
*/
|
|
619
|
+
definitions?: {
|
|
620
|
+
[key: string]: JSONSchema7Definition;
|
|
621
|
+
} | undefined;
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
625
|
+
*/
|
|
626
|
+
title?: string | undefined;
|
|
627
|
+
description?: string | undefined;
|
|
628
|
+
default?: JSONSchema7Type | undefined;
|
|
629
|
+
readOnly?: boolean | undefined;
|
|
630
|
+
writeOnly?: boolean | undefined;
|
|
631
|
+
examples?: JSONSchema7Type | undefined;
|
|
632
|
+
}
|
|
633
|
+
|
|
466
634
|
/** ChatGPT config options */
|
|
467
635
|
interface ChatGPTConfig {
|
|
468
636
|
/** API key */
|
|
@@ -495,11 +663,7 @@ interface ChatGPTToolConfig {
|
|
|
495
663
|
/** Description of the tool */
|
|
496
664
|
description: string;
|
|
497
665
|
/** Parameters for the tool */
|
|
498
|
-
params:
|
|
499
|
-
name: string;
|
|
500
|
-
type: string;
|
|
501
|
-
description: string;
|
|
502
|
-
}[];
|
|
666
|
+
params: JSONSchema7;
|
|
503
667
|
/** Callback function to process the tool */
|
|
504
668
|
callback: (params: any) => any;
|
|
505
669
|
/** If true, this tool call will be removed from the message history after it is executed. */
|
|
@@ -860,8 +1024,8 @@ declare class AILogic {
|
|
|
860
1024
|
/**
|
|
861
1025
|
* Generate a Markdown document based on the data from the user.
|
|
862
1026
|
*
|
|
863
|
-
* @param query The query to generate the document from.
|
|
864
1027
|
* @param config Instruct config.
|
|
1028
|
+
* @returns A markdown document.
|
|
865
1029
|
*/
|
|
866
1030
|
generateMarkdown(config: Omit<IntelliWeaveInstructConfig, 'instruction'>): Promise<string>;
|
|
867
1031
|
/**
|
|
@@ -884,6 +1048,102 @@ interface IntelliWeaveInstructConfig {
|
|
|
884
1048
|
callback?: (txt: string) => void;
|
|
885
1049
|
}
|
|
886
1050
|
|
|
1051
|
+
/**
|
|
1052
|
+
* Allows an MCP server to be used as a knowledge source for IntelliWeave.
|
|
1053
|
+
*/
|
|
1054
|
+
declare class MCPKnowledgeClient {
|
|
1055
|
+
/** MCP client */
|
|
1056
|
+
client?: Client;
|
|
1057
|
+
/** All tools discovered on the MCP server. Only available after connect() has completed. */
|
|
1058
|
+
tools: Awaited<ReturnType<Client['listTools']>>['tools'];
|
|
1059
|
+
/** All toold discovered, mapped to IntelliWeave knowledge base actions */
|
|
1060
|
+
iwActions: KnowledgeBaseItem[];
|
|
1061
|
+
/** Statistics */
|
|
1062
|
+
stats: {
|
|
1063
|
+
toolsCalled: number;
|
|
1064
|
+
};
|
|
1065
|
+
/** Configuration */
|
|
1066
|
+
config: {
|
|
1067
|
+
/** Source ID */
|
|
1068
|
+
id?: string;
|
|
1069
|
+
/** URL to the MCP server endpoint */
|
|
1070
|
+
baseURL?: string;
|
|
1071
|
+
/** Custom connection function. If specified, baseURL is optional. */
|
|
1072
|
+
connect?: () => Promise<Client>;
|
|
1073
|
+
/**
|
|
1074
|
+
* The name of the tool which provides knowledge searching. If specified, the search() will exclude this function and instead
|
|
1075
|
+
* call it and show returned results. If not specified, the search() will just return all tools.
|
|
1076
|
+
*/
|
|
1077
|
+
searchToolName?: string;
|
|
1078
|
+
/** Keep search function available for the AI to use. */
|
|
1079
|
+
searchToolVisible?: boolean;
|
|
1080
|
+
};
|
|
1081
|
+
/** Constructor */
|
|
1082
|
+
constructor(config: MCPKnowledgeClient['config']);
|
|
1083
|
+
/** In-progress connection attempt */
|
|
1084
|
+
private connectionPromise?;
|
|
1085
|
+
/** Connect to the client */
|
|
1086
|
+
connect(): Promise<Client<{
|
|
1087
|
+
method: string;
|
|
1088
|
+
params?: {
|
|
1089
|
+
[x: string]: unknown;
|
|
1090
|
+
_meta?: {
|
|
1091
|
+
[x: string]: unknown;
|
|
1092
|
+
progressToken?: string | number | undefined;
|
|
1093
|
+
} | undefined;
|
|
1094
|
+
} | undefined;
|
|
1095
|
+
}, {
|
|
1096
|
+
method: string;
|
|
1097
|
+
params?: {
|
|
1098
|
+
[x: string]: unknown;
|
|
1099
|
+
_meta?: {
|
|
1100
|
+
[x: string]: unknown;
|
|
1101
|
+
} | undefined;
|
|
1102
|
+
} | undefined;
|
|
1103
|
+
}, {
|
|
1104
|
+
[x: string]: unknown;
|
|
1105
|
+
_meta?: {
|
|
1106
|
+
[x: string]: unknown;
|
|
1107
|
+
} | undefined;
|
|
1108
|
+
}>>;
|
|
1109
|
+
connectInternal(): Promise<Client<{
|
|
1110
|
+
method: string;
|
|
1111
|
+
params?: {
|
|
1112
|
+
[x: string]: unknown;
|
|
1113
|
+
_meta?: {
|
|
1114
|
+
[x: string]: unknown;
|
|
1115
|
+
progressToken?: string | number | undefined;
|
|
1116
|
+
} | undefined;
|
|
1117
|
+
} | undefined;
|
|
1118
|
+
}, {
|
|
1119
|
+
method: string;
|
|
1120
|
+
params?: {
|
|
1121
|
+
[x: string]: unknown;
|
|
1122
|
+
_meta?: {
|
|
1123
|
+
[x: string]: unknown;
|
|
1124
|
+
} | undefined;
|
|
1125
|
+
} | undefined;
|
|
1126
|
+
}, {
|
|
1127
|
+
[x: string]: unknown;
|
|
1128
|
+
_meta?: {
|
|
1129
|
+
[x: string]: unknown;
|
|
1130
|
+
} | undefined;
|
|
1131
|
+
}>>;
|
|
1132
|
+
/** Disconnect from server */
|
|
1133
|
+
disconnect(): Promise<void>;
|
|
1134
|
+
/** Fetch list of tools from the MCP server */
|
|
1135
|
+
private fetchTools;
|
|
1136
|
+
/** Cache last search result */
|
|
1137
|
+
lastSearchQuery: string;
|
|
1138
|
+
lastSearchResults: KnowledgeBaseItem[];
|
|
1139
|
+
/** Perform a search query */
|
|
1140
|
+
search(query: string): Promise<KnowledgeBaseItem[]>;
|
|
1141
|
+
/** Perform search using the configured search function */
|
|
1142
|
+
private performSearchCall;
|
|
1143
|
+
/** Perform tool call. */
|
|
1144
|
+
private performToolCall;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
887
1147
|
/** Persona config received from the hub */
|
|
888
1148
|
interface WebWeaverGPTConfig {
|
|
889
1149
|
/** ID */
|
|
@@ -924,6 +1184,8 @@ interface WebWeaverGPTConfig {
|
|
|
924
1184
|
};
|
|
925
1185
|
/** Knowledge base sources */
|
|
926
1186
|
knowledge?: KnowledgeBaseSource[];
|
|
1187
|
+
/** MCP servers */
|
|
1188
|
+
mcpServers?: MCPKnowledgeClient['config'][];
|
|
927
1189
|
}
|
|
928
1190
|
/**
|
|
929
1191
|
* IntelliWeave interface, loads a Persona from the hub and allows you to interact with it. This is the main entry point into the IntelliWeave
|
|
@@ -1058,7 +1320,11 @@ declare class KnowledgeBase {
|
|
|
1058
1320
|
registerSourceFromURL(url: string, id?: string): void;
|
|
1059
1321
|
/** Clone this instance */
|
|
1060
1322
|
clone(): KnowledgeBase;
|
|
1323
|
+
/** Registers an MCP server as a knowledge base source */
|
|
1324
|
+
registerMCPSource(config: MCPKnowledgeClient['config']): MCPKnowledgeClient;
|
|
1061
1325
|
}
|
|
1326
|
+
/** Knowledge fetcher */
|
|
1327
|
+
type KnowledgeFetcher = (query: string) => (KnowledgeBaseItem[] | Promise<KnowledgeBaseItem[]>);
|
|
1062
1328
|
/** Knowledge base source */
|
|
1063
1329
|
interface KnowledgeBaseSource {
|
|
1064
1330
|
/** Source ID */
|
|
@@ -1072,11 +1338,15 @@ interface KnowledgeBaseSource {
|
|
|
1072
1338
|
/** If true, this source will not be queried. */
|
|
1073
1339
|
disabled?: boolean;
|
|
1074
1340
|
/** Source query function. This function should return a list of knowledge base entries that optionally match the query. */
|
|
1075
|
-
query?:
|
|
1341
|
+
query?: KnowledgeFetcher;
|
|
1076
1342
|
/** URL query for remote sources */
|
|
1077
1343
|
url?: string;
|
|
1078
1344
|
/** Pre-packaged knowledge base entries */
|
|
1079
1345
|
entries?: KnowledgeBaseItem[];
|
|
1346
|
+
/** Remote knowledge server type (default is 'iw') */
|
|
1347
|
+
backendType?: 'mcp' | 'iw';
|
|
1348
|
+
/** If using MCP, this is the name of the tool to use to search for knowledge */
|
|
1349
|
+
mcpSearchToolName?: string;
|
|
1080
1350
|
}
|
|
1081
1351
|
/** Knowledge base item */
|
|
1082
1352
|
interface KnowledgeBaseItem {
|
|
@@ -1096,12 +1366,8 @@ interface KnowledgeBaseItem {
|
|
|
1096
1366
|
isContext?: boolean;
|
|
1097
1367
|
/** If true, this item will not be visible to the AI. */
|
|
1098
1368
|
disabled?: boolean;
|
|
1099
|
-
/** List of parameters for an action function. */
|
|
1100
|
-
parameters?:
|
|
1101
|
-
name: string;
|
|
1102
|
-
type: 'string' | 'boolean' | 'number';
|
|
1103
|
-
description: string;
|
|
1104
|
-
})[];
|
|
1369
|
+
/** List of parameters for an action function. Can either use IW's format, or a JSON Schema object. */
|
|
1370
|
+
parameters?: KnowledgeBaseActionParameterSchema;
|
|
1105
1371
|
/**
|
|
1106
1372
|
* Item action. The parameters are defined in `parameters`. The response is stringified and sent to the AI.
|
|
1107
1373
|
* You can return any JSON-serializable object. You can also return a string describing to the AI the action
|
|
@@ -1111,6 +1377,14 @@ interface KnowledgeBaseItem {
|
|
|
1111
1377
|
/** If true, this item will be removed from the AI's message history after it gets called. This is a special case for LLMs that struggle with follow-up function calls and need to use the KB search functino first. */
|
|
1112
1378
|
removeFromMessageHistory?: boolean;
|
|
1113
1379
|
}
|
|
1380
|
+
/** Parameter definition used by IntelliWeave */
|
|
1381
|
+
interface IntelliWeaveParameterDefinition {
|
|
1382
|
+
name: string;
|
|
1383
|
+
type: 'string' | 'boolean' | 'number';
|
|
1384
|
+
description: string;
|
|
1385
|
+
}
|
|
1386
|
+
/** Tool call input schema. Can either use IW's format, or a JSON Schema object */
|
|
1387
|
+
type KnowledgeBaseActionParameterSchema = JSONSchema7 | IntelliWeaveParameterDefinition[];
|
|
1114
1388
|
/** Format for incoming KB webook. Sent from IntelliWeave to your endpoint. */
|
|
1115
1389
|
interface KnowledgeBaseWebhookRequest {
|
|
1116
1390
|
/** Type of the request. */
|
|
@@ -1296,6 +1570,8 @@ declare function intelliweaveConfig(): IntelliWeaveGlobalConfig;
|
|
|
1296
1570
|
declare function sseEvents(stream: ReadableStream<Uint8Array>): AsyncGenerator<any, void, unknown>;
|
|
1297
1571
|
/** Get or create a user ID for this device. */
|
|
1298
1572
|
declare function getDefaultUserID(): string;
|
|
1573
|
+
/** Convert an IntelliWeave parameter list to JSON schema. Does not modify if it's already a JSON schema. */
|
|
1574
|
+
declare function convertParamsToJSONSchema(params: KnowledgeBaseActionParameterSchema): JSONSchema7;
|
|
1299
1575
|
|
|
1300
1576
|
/** Class to help with logging */
|
|
1301
1577
|
declare class Logging {
|
|
@@ -1372,4 +1648,4 @@ interface IntelliWeaveStreamTextOutputEvent extends IntelliWeaveStreamEvent {
|
|
|
1372
1648
|
text: string;
|
|
1373
1649
|
}
|
|
1374
1650
|
|
|
1375
|
-
export { type BufferType, BufferedWebSocket, ChatGPT, type ChatGPTConfig, type ChatGPTMessage, type ChatGPTToolConfig, FixedBufferStream, IntelliWeave, type IntelliWeaveGlobalConfig, IntelliWeaveStream, type IntelliWeaveStreamErrorEvent, type IntelliWeaveStreamEvent, type IntelliWeaveStreamTextOutputEvent, KnowledgeBase, type KnowledgeBaseItem, type KnowledgeBaseSource, type KnowledgeBaseWebhookActionResponse, type KnowledgeBaseWebhookRequest, type KnowledgeBaseWebhookSearchResponse, Logging, ONNXModel, type ONNXTensors, Resampler, type SupportedArrayBuffers, TokenWindow, TokenWindowGroup, type TokenWindowGroupItem, type WebWeaverGPTConfig, audioToWav, floatTo16BitPCM, floatTo64BitPCM, getDefaultUserID, int16ToFloat32BitPCM, intelliweaveConfig, intelliweaveGlobalThis, sseEvents, trimWhitespaceInText };
|
|
1651
|
+
export { type BufferType, BufferedWebSocket, ChatGPT, type ChatGPTConfig, type ChatGPTMessage, type ChatGPTToolConfig, FixedBufferStream, IntelliWeave, type IntelliWeaveGlobalConfig, type IntelliWeaveParameterDefinition, IntelliWeaveStream, type IntelliWeaveStreamErrorEvent, type IntelliWeaveStreamEvent, type IntelliWeaveStreamTextOutputEvent, KnowledgeBase, type KnowledgeBaseActionParameterSchema, type KnowledgeBaseItem, type KnowledgeBaseSource, type KnowledgeBaseWebhookActionResponse, type KnowledgeBaseWebhookRequest, type KnowledgeBaseWebhookSearchResponse, type KnowledgeFetcher, Logging, MCPKnowledgeClient, ONNXModel, type ONNXTensors, Resampler, type SupportedArrayBuffers, TokenWindow, TokenWindowGroup, type TokenWindowGroupItem, type WebWeaverGPTConfig, audioToWav, convertParamsToJSONSchema, floatTo16BitPCM, floatTo64BitPCM, getDefaultUserID, int16ToFloat32BitPCM, intelliweaveConfig, intelliweaveGlobalThis, sseEvents, trimWhitespaceInText };
|
package/dist/node/node.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import{v4 as
|
|
1
|
+
import{v4 as _}from"uuid";function W(u){let t=(u||"").split(`
|
|
2
2
|
`);for(;t.length>0&&t[0].trim()=="";)t.shift();for(;t.length>0&&t[t.length-1].trim()=="";)t.pop();let e=1/0;for(let s of t){let n=s.match(/^\s*/)?.[0].length||0;e=Math.min(e,n)}return e>0&&e<1/0&&(t=t.map(s=>s.substring(e))),t.join(`
|
|
3
|
-
`)}var
|
|
3
|
+
`)}var V={};function I(){return typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:V}function f(){return I().intelliweave=I().intelliweave||I().webWeaver||{},I().intelliweave}async function*R(u){let t="",e=function*(){for(;;){let i=t.indexOf(`
|
|
4
4
|
|
|
5
|
-
`);if(i==-1)break;let
|
|
6
|
-
`);for(let
|
|
7
|
-
`+d:
|
|
5
|
+
`);if(i==-1)break;let l=t.slice(0,i);t=t.slice(i+2);let c={},o=l.split(`
|
|
6
|
+
`);for(let r of o){i=r.indexOf(": ");let a=r.slice(0,i),d=r.slice(i+2);i==-1&&(a=r,d=""),a&&(c[a]!==void 0?c[a]+=`
|
|
7
|
+
`+d:c[a]=d)}yield c}},s=new TextDecoder,n=u.getReader();for(;;){let{done:i,value:l}=await n.read();if(i)break;t+=s.decode(l,{stream:!0}),yield*e()}t+=s.decode()+`
|
|
8
8
|
|
|
9
|
-
`,yield*e()}function
|
|
10
|
-
`;this.itemPadding=0;this.sortFunction=(t,e)=>t.sortOrder!==e.sortOrder?t.sortOrder-e.sortOrder:t.dateAdded!==e.dateAdded?t.dateAdded-e.dateAdded:t.content.localeCompare(e.content)}setItemPadding(t){return this.itemPadding=t,this.recalculateTokens(),this}sortBy(t){return this.sortFunction=t,this.items.sort(this.sortFunction),this}setSeparator(t){return this.separator==t?this:(this.separator=t,this.recalculateTokens(),this)}setWeight(t){return this.weight=t,this}recalculateTokens(){this.tokenCount=this.items.reduce((t,e)=>(e.tokenCount=
|
|
9
|
+
`,yield*e()}function U(){if(f().userID)return f().userID||"";if(typeof localStorage<"u"){let u=localStorage.getItem("intelliweave.uid")||"";return u||(u=_(),localStorage.setItem("intelliweave.uid",u),u)}else return _()}function j(u){let t=u;if(t?.type=="object"&&t.properties)return u;let e={type:"object",properties:{},required:[]};if(u&&Array.isArray(u)){let s=u;for(let n of s)e.properties[n.name]={type:n.type||"string",description:n.description||""}}return e}var C=class C{constructor(t){this.module="IntelliWeave";this.module=t}get debugEnabled(){return C.debug?!0:typeof window<"u"&&f().debug}log(...t){this.debugEnabled&&console.log(`[IntelliWeave > ${this.module}]`,...t)}debug(...t){this.debugEnabled&&console.debug(`[IntelliWeave > ${this.module}]`,...t)}info(...t){this.debugEnabled&&console.info(`[IntelliWeave > ${this.module}]`,...t)}warn(...t){console.warn(`[IntelliWeave > ${this.module}]`,...t)}error(...t){console.error(`[IntelliWeave > ${this.module}]`,...t)}timer(t,...e){let s=Date.now();return this.debug(`[${t} 0ms] Started`,...e),(...n)=>this.debug(`[${t} ${Math.floor(Date.now()-s)}ms]`,...n)}};C.debug=!1;var g=C;var b=new g("ONNXModel"),J=class u{constructor(t){this.stateTensors={};this.constantTensors={};this._runActive=!1;this.ignoreIfBusy=!1;this.session=t,b.debug(`Model input parameters: ${t.inputNames.join(", ")}`),b.debug(`Model output parameters: ${t.outputNames.join(", ")}`)}static isSupported(){return!!u.lib}static async load(t){if(!u.lib)throw new Error("ONNX runtime not loaded, please set the runtime loader. Example: ONNXModel.lib = () => import('onnxruntime-web')");this.onnx||(b.debug("Loading ONNX runtime"),this.onnx=await u.lib()),b.debug(`Loading model: ${t}`);let e=await this.onnx.InferenceSession.create(t);return new u(e)}makeTensor(t,e,s=0){let n=1;for(let l of e)n*=l;let i;if(t=="float32")i=new u.onnx.Tensor(new Float32Array(n),e);else if(t=="int8")i=new u.onnx.Tensor(new Int8Array(n),e);else if(t=="int16")i=new u.onnx.Tensor(new Int16Array(n),e);else if(t=="int32")i=new u.onnx.Tensor(new Int32Array(n),e);else if(t=="int64")i=new u.onnx.Tensor(new BigInt64Array(n),e);else if(t=="uint8")i=new u.onnx.Tensor(new Uint8Array(n),e);else if(t=="uint16")i=new u.onnx.Tensor(new Uint16Array(n),e);else if(t=="uint32")i=new u.onnx.Tensor(new Uint32Array(n),e);else if(t=="uint64")i=new u.onnx.Tensor(new BigUint64Array(n),e);else throw new Error(`Invalid type: ${t}`);return s!==0&&(t=="int64"||t=="uint64")?i.data.fill(BigInt(s)):s!==0&&i.data.fill(s),i}registerConstant(t,e){if(!this.session.inputNames.includes(t))throw new Error(`Model does not have an input named: ${t}`);return this.constantTensors[t]=e,e}makeConstant(t,e,s,n=0){return this.registerConstant(t,this.makeTensor(e,s,n))}registerState(t,e,s){if(e||(e=t),!this.session.inputNames.includes(t))throw new Error(`Model does not have an input named: ${t}`);if(!this.session.outputNames.includes(e))throw new Error(`Model does not have an output named: ${e}`);return this.stateTensors[t]={outputName:e,tensor:s},s}makeState(t,e,s,n,i=0){return this.registerState(t,e,this.makeTensor(s,n,i))}async run(t={}){if(this._runActive&&this.ignoreIfBusy)return b.debug("Ignoring run request because a previous run is still active");if(this._runActive)throw new Error("A previous run is still active");this._runActive=!0;for(let e in this.stateTensors)t[e]=this.stateTensors[e].tensor;for(let e in this.constantTensors)t[e]=this.constantTensors[e];try{let e=await this.session.run(t);for(let s in this.stateTensors){let n=e[this.stateTensors[s].outputName];this.stateTensors[s].tensor=n}return e}finally{this._runActive=!1}}resetState(){b.debug("Resetting state tensors");for(let t in this.stateTensors)this.stateTensors[t].tensor.data.fill(0)}};function ue(u,t){let e=t.reduce((o,r)=>o+r.byteLength,0),s=new DataView(new ArrayBuffer(44));s.setUint8(0,82),s.setUint8(1,73),s.setUint8(2,70),s.setUint8(3,70),s.setUint32(4,44+e,!0),s.setUint8(8,87),s.setUint8(9,65),s.setUint8(10,86),s.setUint8(11,69);let n=1,i=32,l=n*i/8,c=u*l;return s.setUint8(12,102),s.setUint8(13,109),s.setUint8(14,116),s.setUint8(15,32),s.setUint32(16,16,!0),s.setUint16(20,3,!0),s.setUint16(22,n,!0),s.setUint32(24,u,!0),s.setUint32(28,c,!0),s.setUint16(32,l,!0),s.setUint16(34,i,!0),s.setUint8(36,100),s.setUint8(37,97),s.setUint8(38,116),s.setUint8(39,97),s.setUint32(40,e,!0),new File([s,...t],"audio.wav",{type:"audio/wav"})}var F=class extends WebSocket{constructor(e){super(e);this.pendingData=[];this.addEventListener("open",()=>this._onOpen())}send(e){this.readyState==WebSocket.OPEN?super.send(e):this.pendingData.push(e)}_onOpen(){for(let e of this.pendingData)super.send(e);this.pendingData=[]}};var L=class{constructor(t,e){this.outputBufferSize=0;this.partialBuffers=[];this.partialBufferOffset=0;if(!t)throw new Error(`Invalid array class: ${t}`);if(!e||e<=0)throw new Error(`Invalid output buffer size: ${e}`);this.ArrayClass=t,this.outputBufferSize=e}get queuedSize(){return this.partialBuffers.reduce((t,e)=>t+e.length,0)}feed(t){this.partialBuffers.push(t)}get canDrain(){return this.partialBuffers.reduce((e,s)=>e+s.length,0)-this.partialBufferOffset>=this.outputBufferSize}drain(){if(!this.canDrain)return null;let t=this.ArrayClass,e=new t(this.outputBufferSize),s=0;for(;s!=e.length;){if(s>e.length)throw new Error(`Buffer overflow: ${s} > ${e.length}`);let n=e.length-s,i=this.partialBuffers[0],l=i.length-this.partialBufferOffset;l<n?(e.set(i.subarray(this.partialBufferOffset),s),s+=l,this.partialBuffers.shift(),this.partialBufferOffset=0):(e.set(i.subarray(this.partialBufferOffset,this.partialBufferOffset+n),s),s+=n,this.partialBufferOffset+=n)}return e}pad(){let t=this.queuedSize%this.outputBufferSize;if(t==0)return;let e=this.ArrayClass,s=new e(t);this.feed(s)}};var z=class{constructor(t,e,s,n){if(!t||!e||!s)throw new Error("Invalid settings specified for the resampler.");this.resampler=null,this.fromSampleRate=t,this.toSampleRate=e,this.channels=s||0,this.inputBufferSize=n,this.initialize()}initialize(){this.fromSampleRate==this.toSampleRate?(this.resampler=t=>t,this.ratioWeight=1):(this.fromSampleRate<this.toSampleRate?(this.linearInterpolation(),this.lastWeight=1):(this.multiTap(),this.tailExists=!1,this.lastWeight=0),this.initializeBuffers(),this.ratioWeight=this.fromSampleRate/this.toSampleRate)}bufferSlice(t){try{return this.outputBuffer.subarray(0,t)}catch{try{return this.outputBuffer.length=t,this.outputBuffer}catch{return this.outputBuffer.slice(0,t)}}}initializeBuffers(){this.outputBufferSize=Math.ceil(this.inputBufferSize*this.toSampleRate/this.fromSampleRate/this.channels*1.0000004768371582)+this.channels+this.channels;try{this.outputBuffer=new Float32Array(this.outputBufferSize),this.lastOutput=new Float32Array(this.channels)}catch{this.outputBuffer=[],this.lastOutput=[]}}linearInterpolation(){this.resampler=t=>{let e=t.length,s=this.channels,n,i,l,c,o,r,a,d,h;if(e%s!==0)throw new Error("Buffer was of incorrect sample length.");if(e<=0)return[];for(n=this.outputBufferSize,i=this.ratioWeight,l=this.lastWeight,c=0,o=0,r=0,a=0,d=this.outputBuffer;l<1;l+=i)for(o=l%1,c=1-o,this.lastWeight=l%1,h=0;h<this.channels;++h)d[a++]=this.lastOutput[h]*c+t[h]*o;for(l-=1,e-=s,r=Math.floor(l)*s;a<n&&r<e;){for(o=l%1,c=1-o,h=0;h<this.channels;++h)d[a++]=t[r+(h>0?h:0)]*c+t[r+(s+h)]*o;l+=i,r=Math.floor(l)*s}for(h=0;h<s;++h)this.lastOutput[h]=t[r++];return this.bufferSlice(a)}}multiTap(){this.resampler=t=>{let e=t.length,s,n,i=this.channels,l,c,o,r,a,d,h,S,T;if(e%i!==0)throw new Error("Buffer was of incorrect sample length.");if(e<=0)return[];for(s=this.outputBufferSize,n=[],l=this.ratioWeight,c=0,r=0,a=0,d=!this.tailExists,this.tailExists=!1,h=this.outputBuffer,S=0,T=0,o=0;o<i;++o)n[o]=0;do{if(d)for(c=l,o=0;o<i;++o)n[o]=0;else{for(c=this.lastWeight,o=0;o<i;++o)n[o]=this.lastOutput[o];d=!0}for(;c>0&&r<e;)if(a=1+r-T,c>=a){for(o=0;o<i;++o)n[o]+=t[r++]*a;T=r,c-=a}else{for(o=0;o<i;++o)n[o]+=t[r+(o>0?o:0)]*c;T+=c,c=0;break}if(c===0)for(o=0;o<i;++o)h[S++]=n[o]/l;else{for(this.lastWeight=c,o=0;o<i;++o)this.lastOutput[o]=n[o];this.tailExists=!0;break}}while(r<e&&S<s);return this.bufferSlice(S)}}resample(t){return this.fromSampleRate==this.toSampleRate?this.ratioWeight=1:(this.fromSampleRate<this.toSampleRate?this.lastWeight=1:(this.tailExists=!1,this.lastWeight=0),this.initializeBuffers(),this.ratioWeight=this.fromSampleRate/this.toSampleRate),this.resampler(t)}};function ge(u){let t=u.length,e=new Int16Array(t);for(;t--;){let s=Math.max(-1,Math.min(1,u[t]));e[t]=s<0?s*32768:s*32767}return e}function fe(u){let t=u.length,e=new BigInt64Array(t);for(;t--;){let s=Math.max(-1,Math.min(1,u[t]));e[t]=BigInt(Math.floor(s<0?s*32768:s*32767))*0x100000000000n}return e}function pe(u){let t=u.length,e=new Float32Array(t);for(;t--;){let s=u[t];e[t]=s>=32768?-(65536-s)/32768:s/32767}return e}import{countTokens as k}from"gpt-tokenizer";import{v4 as X}from"uuid";var B=class{constructor(){this.size=4096;this.groups=[]}createGroup(t){let e=new G;return e.id=t,this.groups.push(e),e}group(t){return this.groups.find(e=>e.id===t)}countTokens(){return this.groups.reduce((t,e)=>t+e.tokenCount,0)}removeOverflow(){let t=this.countTokens(),e=this.groups.reduce((s,n)=>s+n.weight,0);for(;t>this.size;){let s=this.groups.slice().sort((i,l)=>{let c=Math.floor(i.weight/e*this.size),o=Math.floor(l.weight/e*this.size),r=i.tokenCount-c;return l.tokenCount-o-r}),n=this.removeOneItem(s);if(!n)throw new Error("Too many items in the token window that cannot be removed.");t-=n.tokenCount}}removeOneItem(t){for(let e of t){let s=e.items.findIndex(i=>!i.cannotRemove);if(s===-1)continue;let n=e.items[s];return e.items.splice(s,1),e.tokenCount-=n.tokenCount,n}return null}},G=class{constructor(){this.id="";this.items=[];this.weight=1;this.tokenCount=0;this.separator=`
|
|
10
|
+
`;this.itemPadding=0;this.sortFunction=(t,e)=>t.sortOrder!==e.sortOrder?t.sortOrder-e.sortOrder:t.dateAdded!==e.dateAdded?t.dateAdded-e.dateAdded:t.content.localeCompare(e.content)}setItemPadding(t){return this.itemPadding=t,this.recalculateTokens(),this}sortBy(t){return this.sortFunction=t,this.items.sort(this.sortFunction),this}setSeparator(t){return this.separator==t?this:(this.separator=t,this.recalculateTokens(),this)}setWeight(t){return this.weight=t,this}recalculateTokens(){this.tokenCount=this.items.reduce((t,e)=>(e.tokenCount=k(e.content)+k(this.separator)+this.itemPadding,t+e.tokenCount),0)}add(t){typeof t=="string"&&(t={content:t});let e=t;e.id===void 0&&(e.id=X());let s=this.items.find(n=>n.id===e.id);return s?(this.tokenCount-=s.tokenCount,Object.assign(s,e),e=s):this.items.push(e),e.dateAdded===void 0&&(e.dateAdded=Date.now()),e.sortOrder===void 0&&(e.sortOrder=0),e.disabled===void 0&&(e.disabled=!1),e.tokenCount=e.disabled?0:k(t.content)+k(this.separator)+this.itemPadding,this.tokenCount+=e.tokenCount,this.items.sort(this.sortFunction),e}getAllAsString(){return this.getAll().map(t=>t.content).join(this.separator)}getAll(){return this.items.filter(t=>!t.disabled)}empty(){this.items=[],this.tokenCount=0}};var x=1,w=new g("ChatGPT"),E=class{constructor(t){this.id="";this.metadata={};this.config={apiKey:"",endpoint:"",model:"gpt-4-turbo",systemMessage:"",userID:"",stream:!0,maxTokens:4096};this.maxToolCallsPerMessage=10;this.stats={tokensUsed:0};this.tokenWindow=(()=>{let t=new B;return t.createGroup("context").setSeparator(`
|
|
11
11
|
|
|
12
|
-
`),t.createGroup("actions").setItemPadding(25),t.createGroup("messages").setItemPadding(10),t})();this.config={...this.config,...t},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(e),customData:e}),await this.processMessages();let n=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return n?.role=="assistant"&&n.content||""}addAssistantMessage(t){let e={role:"assistant",content:t};this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(e),customData:e})}addUserMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(t=>t.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),m.debugEnabled&&m.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(s=>s.customData),messages:this.messageGroup.items.map(s=>s.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let s of e.tool_calls)await this.processToolCall(s);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(t){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let r=this.messageGroup.items.length-1;r>=0;r--){let o=this.messageGroup.items[r].customData;if(o.role=="assistant"&&o.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(o.role=="user")break}m.debugEnabled&&m.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(r=>r.customData),messages:this.messageGroup.items.map(r=>r.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let s={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,max_tokens:1024,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(r=>r.customData)]};for(let r of this.toolGroup.getAll()){let o=r.customData;o.description&&o.description.length>1024&&m.warn(`Tool description for "${o.name}" is too long, it will be truncated.`);let a={type:"function",function:{name:o.name,description:(o.description||"").substring(0,1024),parameters:{type:"object",properties:{}}}};if(Array.isArray(o.params))for(let d of o.params)a.function.parameters.properties[d.name]={type:d.type,description:d.description};else if(o.params){let d=o.params;for(let h in d)a.function.parameters.properties[h]={type:"string",description:d[h]}}s.tools.push(a)}s.user||delete s.user,s.tools.length||delete s.tools;let n={};if(n["Content-Type"]="application/json",this.config.apiKey&&(n.Authorization=`Bearer ${this.config.apiKey}`),t)return s;let i=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:n,body:JSON.stringify(s)});if(!i.ok){let r=`${i.status} ${i.statusText}`;try{let a=await i.json();r=a.errorText||a.error?.message||a.error||r}catch{}let o=new Error(r);throw o.code=i.status,o}let u=null,l=null;if(this.config.stream)for await(let r of _(i.body)){if(r.data=="[DONE]")break;if(!r.data)continue;let o=JSON.parse(r.data);if(l){for(let a in o.choices[0].delta)if(typeof o.choices[0].delta[a]=="string"){if(a=="role"&&l[a]==o.choices[0].delta[a])continue;l[a]=(l[a]||"")+o.choices[0].delta[a]}for(let a of o.choices[0].delta.tool_calls||[]){if(l.tool_calls||(l.tool_calls=[]),!l.tool_calls[a.index]){l.tool_calls[a.index]=a;continue}let d=l.tool_calls[a.index];d.function=d.function||{},d.function.name=(d.function.name||"")+(a.function?.name||""),d.function.arguments=(d.function.arguments||"")+(a.function?.arguments||"")}}else l={chunkID:o.id,...o.choices[0].delta};l?.content&&!l.content.startsWith("{")&&this.config.onAIMessage?.(l.content,!0)}else u=await i.json(),this.stats.tokensUsed+=u.usage?.total_tokens||0,m.debug(`Used ${u.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),l=u.choices?.[0]?.message;if(l||(m.warn("No response block in API response."),l={role:"assistant",content:""}),l.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(l),this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(l),customData:l}),l.content&&this.config.onAIMessage?.(l.content,!1)}processIncomingMessage(t){}registerTool(t){this.toolGroup.add({id:t.name,cannotRemove:!t.canRemove,content:JSON.stringify(t),customData:t})}async processToolCall(t){try{let s=this.toolGroup.items.find(l=>l.id==t.function.name)?.customData;if(!s)throw new Error(`Tool "${t.function.name}" not found.`);let n=JSON.parse(t.function.arguments);this.config.onAIToolStart?.(t.function.name,n);let i=await s.callback(n);typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success");let u={role:"tool",content:i,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(u),customData:u})}catch(e){m.warn(`Unable to process tool call for "${t?.function?.name}"`,e);let s={role:"tool",content:`Error: ${e.message}`,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+v++,content:JSON.stringify(s),customData:s})}}resetConversation(){this.messageGroup.empty()}};import H from"minisearch";var j=[{id:"search",type:"action",name:"Search the knowledge base.",content:"Search the knowledge base for information, actions, tools, tours, UI elements, etc. Use this on EVERY request if you don't have information. If the user asks for personal information, use this. If the user asks you to do something, use this to find the tool you need.",isContext:!0,removeFromMessageHistory:!0,parameters:[{name:"query",type:"string",description:"The search query"}],action:async(c,t)=>{let e=await t.knowledgeBase.search(c.query),s=t;s._lastKBsearch=c.query,s._nextRequestUseKBitems=e,t.submitAnalyticsEvent({type:"kb-search",query:c.query,results:e});let n=e.filter(u=>u.type!="action").map(u=>"id="+u.id).join(", ")||"(none)",i=e.filter(u=>u.type=="action").map(u=>"name="+u._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${n}. New tools available: ${i}.`}}];var w=new p("KnowledgeBase"),F=1,E=class c{constructor(t){this._sources=[{id:"core.internal",query:async()=>j}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=t}registerSource(t,e){let s=t;return typeof t=="function"&&(e=t,s=`source.${F++}`),this._sources.push({id:s,query:e}),s}removeSource(t){this._sources=this.sources.filter(e=>e.id!==t&&e.query!==t)}addEntry(t){this.manualEntries.push(t)}removeEntry(t){this.manualEntries=this.manualEntries.filter(e=>e.id!==t)}get sources(){let t=this._sources;return g().knowledgeBaseSources&&(t=t.concat(g().knowledgeBaseSources)),t=t.concat(this._windowSources),t=t.filter(e=>!e.disabled),t}async search(t){w.debug(`Searching knowledge base for: ${t}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=t,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let n=(await Promise.all(this.sources.map(async r=>{try{let o=Date.now(),a=await r.query(t);return w.debug(`Source '${r.id}' took ${Date.now()-o}ms`),a||[]}catch(o){return w.warn(`Knowledge source '${r.id}' failed:`,o),[]}}))).flat();n=n.concat(e.entries),n=n.concat(this.manualEntries),g().knowledgeBase&&(n=n.concat(g().knowledgeBase)),n=n.filter(r=>r&&!r.disabled);for(let r=0;r<n.length;r++){let o=n[r];if(o.id=o.id||`temp.${r}`,o._functionID=o.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!o.action&&o.type=="tour"&&(o.action=a=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!o.action&&o.type=="info"&&(o.action=a=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")}),o.parameters&&!Array.isArray(o.parameters)){let a=o.parameters,d=[];for(let h in a)d.push({name:h,type:"string",description:o.parameters[h]});o.parameters=d}}let i=new H({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});i.addAll(n);let l=i.search(t).map(r=>n.find(o=>o.id==r.id));for(let r of n)r.isContext&&(l.find(o=>o.id===r.id)||l.push(r));return this.lastResults=l,w.debug("Found results:",l),l}getCachedEntry(t){return this.lastResults.find(e=>e.id==t||e._functionID==t)}registerSourceFromURL(t,e){e||(e=`external.${F++}`),w.debug(`Registering remote knowledge base source: ${t}`);let s=[],n=[],i=!0,u=async(r,o)=>{w.debug(`Calling remote knowledge base action: ${r.id}`);let a={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:r.id,parameters:o},d=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let h=await d.json();return l(h.updateItems||[]),h.response},l=r=>{for(let o of r){if(!o.id){w.warn("KB item skipped since it has no ID.",o);continue}let a=s.find(d=>d.id==o.id);if(a){a.name=o.name||a.name||"",a.content=o.content||a.content||"",a.disabled=o.disabled??a.disabled,a.isContext=o.isContext??a.isContext,a.parameters=o.parameters||a.parameters||[],a.tags=o.tags||a.tags,a.type=o.type||a.type;continue}s.push({...o,action:d=>u(o,d)})}};this.registerSource(e,async r=>{if(i&&n.includes(r))return s;let o={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:r},a=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});if(!a.ok)throw new Error(`HTTP Error ${a.status} ${a.statusText}`);let d=await a.json();return i=!d.noCache,n.includes(r)||n.push(r),l(d.items),s})}clone(){let t=new c(this.ai);return t._sources=this._sources,t._windowSources=this._windowSources,t.manualEntries=this.manualEntries,t}};import{v4 as M}from"uuid";var z={name:"@intelliweave/embedded",version:"1.8.60",description:"Integrate IntelliWeave into your app or website.",main:"./dist/webpack/index.js",types:"./dist/webpack/index.d.ts",type:"module",exports:{".":"./dist/webpack/index.js","./component":"./dist/component/component.js","./node":"./dist/node/node.js","./react":"./dist/react/react.js","./webpack":"./dist/webpack/index.js"},scripts:{build:"npm run build:lib && npm run build:docs","build:lib":"tsx build.ts","build:dev":"cross-env DEVELOPMENT=1 npm run build","build:docs":"typedoc src/index.mts --out dist/docs/",deploy:'npm run build && gsutil cp ./dist/web-weaver.min.js gs://metapress-cdn/web-weaver.min.js && gcloud compute url-maps invalidate-cdn-cache mp-cdn-loadbalancer --project="mp-backend-api" --path "/web-weaver.min.js" --async',"start:server":"cd server && npm run start","deploy:server":"cd server && npm run deploy","llm:build":"cd llm-server && docker build -t web-weaver-llm .","llm:start":"npm run llm:build && docker run -it --rm -p 8000:80 --gpus=all web-weaver-llm","llm:deploy.docker":"npm run llm:build && gcloud auth configure-docker us-central1-docker.pkg.dev && docker tag web-weaver-llm us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm && docker push us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm","llm:deploy":'npm run llm:deploy.docker && gcloud run deploy web-weaver-llm --project=ydangle-web-companion --image=us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm --allow-unauthenticated --region=us-central1 --description="Web Weaver LLM" --concurrency=2 --min-instances=0 --timeout=5m --memory=16Gi --cpu=8',prepack:"npm run build",test:"npm run build && tsx --test"},keywords:["web","weaver","ai","assistant","chat"],author:"jjv360",license:"UNLICENSED",devDependencies:{"@types/audioworklet":"^0.0.64","@types/lodash":"^4.17.13","@types/react":"^18.3.12","@types/uuid":"^10.0.0",bestzip:"^2.2.1","cross-env":"^7.0.3","find-cache-dir":"^5.0.0",lodash:"^4.17.21","onnxruntime-web":"^1.20.0",react:"^18.3.1","replace-in-file":"^8.2.0",tsup:"^8.3.5",tsx:"^4.19.2",typedoc:"^0.27.6"},peerDependencies:{"onnxruntime-web":"^1.20.0",react:"^18 || ^19"},dependencies:{"gpt-tokenizer":"^2.9.0",minisearch:"^6.3.0","rehype-document":"^7.0.3","rehype-external-links":"^3.0.0","rehype-format":"^5.0.0","rehype-stringify":"^10.0.0","remark-parse":"^11.0.0","remark-rehype":"^11.1.0",unified:"^11.0.4","utility-types":"^3.11.0",uuid:"^10.0.0"}};var B=class{constructor(t){this.ai=t}async boolean(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
12
|
+
`),t.createGroup("actions").setItemPadding(25),t.createGroup("messages").setItemPadding(10),t})();this.config={...this.config,...t},this.tokenWindow.size=this.config.maxTokens||4096}get contextGroup(){return this.tokenWindow.group("context")}get toolGroup(){return this.tokenWindow.group("actions")}get messageGroup(){return this.tokenWindow.group("messages")}async sendMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e}),await this.processMessages();let n=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;return n?.role=="assistant"&&n.content||""}addAssistantMessage(t){let e={role:"assistant",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}addUserMessage(t){let e={role:"user",content:t};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(e),customData:e})}getMessages(){return this.messageGroup.items.map(t=>t.customData)}getLatestMessage(){return this.messageGroup.items.length?this.messageGroup.items[this.messageGroup.items.length-1]?.customData:void 0}async processMessages(){await this.config.onBeforeMessageProcessing?.(),w.debugEnabled&&w.debug("Process message state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(s=>s.customData),messages:this.messageGroup.items.map(s=>s.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let e=this.messageGroup.items[this.messageGroup.items.length-1]?.customData;if(e?.role=="user"||e?.role=="tool")await this.sendToAPI(),await this.processMessages();else if(e?.role=="assistant"&&e?.tool_calls?.length){for(let s of e.tool_calls)await this.processToolCall(s);await this.processMessages()}}async trimMessages(){for(this.tokenWindow.removeOverflow();this.messageGroup.items.length&&this.messageGroup.items[0].customData?.role=="tool";)this.messageGroup.items.shift()}async sendToAPI(t){this.config.systemMessage&&this.contextGroup.add({id:"_gpt_context",cannotRemove:!0,sortOrder:0,content:this.config.systemMessage}),await this.trimMessages();let e=0;for(let o=this.messageGroup.items.length-1;o>=0;o--){let r=this.messageGroup.items[o].customData;if(r.role=="assistant"&&r.tool_calls){if(e+=1,e>=this.maxToolCallsPerMessage)throw new Error(`Exceeded the maximum tool calls per message limit of ${this.maxToolCallsPerMessage}.`)}else if(r.role=="user")break}w.debugEnabled&&w.debug("Before LLM state:",{context:this.contextGroup.getAllAsString(),tools:this.toolGroup.getAll().map(o=>o.customData),messages:this.messageGroup.items.map(o=>o.customData),tokens:{used:this.tokenWindow.countTokens(),max:this.tokenWindow.size}});let s={model:this.config.model,temperature:.2,user:this.config.userID,tools:[],stream:!!this.config.stream,max_tokens:1024,messages:[{role:"system",content:this.contextGroup.getAllAsString()},...this.messageGroup.getAll().map(o=>o.customData)]};for(let o of this.toolGroup.getAll()){let r=o.customData;r.description&&r.description.length>1024&&w.warn(`Tool description for "${r.name}" is too long, it will be truncated.`);let a={type:"function",function:{name:r.name,description:(r.description||"").substring(0,1024),parameters:r.params}};s.tools.push(a)}s.user||delete s.user,s.tools.length||delete s.tools;let n={};if(n["Content-Type"]="application/json",this.config.apiKey&&(n.Authorization=`Bearer ${this.config.apiKey}`),t)return s;let i=await fetch(this.config.endpoint||"https://api.openai.com/v1/chat/completions",{method:"POST",headers:n,body:JSON.stringify(s)});if(!i.ok){let o=`${i.status} ${i.statusText}`;try{let a=await i.json();o=a.errorText||a.error?.message||a.error||o}catch{}let r=new Error(o);throw r.code=i.status,r}let l=null,c=null;if(this.config.stream)for await(let o of R(i.body)){if(o.data=="[DONE]")break;if(!o.data)continue;let r=JSON.parse(o.data);if(c){for(let a in r.choices[0].delta)if(typeof r.choices[0].delta[a]=="string"){if(a=="role"&&c[a]==r.choices[0].delta[a])continue;c[a]=(c[a]||"")+r.choices[0].delta[a]}for(let a of r.choices[0].delta.tool_calls||[]){if(c.tool_calls||(c.tool_calls=[]),!c.tool_calls[a.index]){c.tool_calls[a.index]=a;continue}let d=c.tool_calls[a.index];d.function=d.function||{},d.function.name=(d.function.name||"")+(a.function?.name||""),d.function.arguments=(d.function.arguments||"")+(a.function?.arguments||"")}}else c={chunkID:r.id,...r.choices[0].delta};c?.content&&!c.content.startsWith("{")&&this.config.onAIMessage?.(c.content,!0)}else l=await i.json(),this.stats.tokensUsed+=l.usage?.total_tokens||0,w.debug(`Used ${l.usage?.total_tokens||0} tokens, total used: ${this.stats.tokensUsed}`),c=l.choices?.[0]?.message;if(c||(w.warn("No response block in API response."),c={role:"assistant",content:""}),c.role=="user")throw new Error("API returned a user message, which is not allowed.");this.processIncomingMessage(c),this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(c),customData:c}),c.content&&this.config.onAIMessage?.(c.content,!1)}processIncomingMessage(t){}registerTool(t){this.toolGroup.add({id:t.name,cannotRemove:!t.canRemove,content:JSON.stringify(t),customData:t})}async processToolCall(t){try{let s=this.toolGroup.items.find(c=>c.id==t.function.name)?.customData;if(!s)throw new Error(`Tool "${t.function.name}" not found.`);let n=JSON.parse(t.function.arguments);this.config.onAIToolStart?.(t.function.name,n);let i=await s.callback(n);typeof i!="string"&&(i=JSON.stringify(i)||""),(i===""||i==="undefined")&&(i="success");let l={role:"tool",content:i,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(l),customData:l})}catch(e){w.warn(`Unable to process tool call for "${t?.function?.name}"`,e);let s={role:"tool",content:`Error: ${e.message}`,tool_call_id:t.id};this.messageGroup.add({id:"msg-"+x++,content:JSON.stringify(s),customData:s})}}resetConversation(){this.messageGroup.empty(),this.toolGroup.empty(),this.contextGroup.empty()}};import ne from"minisearch";var q=[{id:"search",type:"action",name:"Search the knowledge base.",content:"Search the knowledge base for information, actions, tools, tours, UI elements, etc. Use this on EVERY request if you don't have information. If the user asks for personal information, use this. If the user asks you to do something, use this to find the tool you need.",isContext:!0,removeFromMessageHistory:!0,parameters:[{name:"query",type:"string",description:"The search query"}],action:async(u,t)=>{let e=await t.knowledgeBase.search(u.query),s=t;s._lastKBsearch=u.query,s._nextRequestUseKBitems=e,t.submitAnalyticsEvent({type:"kb-search",query:u.query,results:e});let n=e.filter(l=>l.type!="action").map(l=>"id="+l.id).join(", ")||"(none)",i=e.filter(l=>l.type=="action").map(l=>"name="+l._functionID).join(", ")||"(none)";return`Search complete, context has been updated. New info entries found: ${n}. New tools available: ${i}.`}}];import{Client as H}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as Q}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as ee}from"@modelcontextprotocol/sdk/client/sse.js";var y={name:"@intelliweave/embedded",version:"1.8.61",description:"Integrate IntelliWeave into your app or website.",main:"./dist/webpack/index.js",types:"./dist/webpack/index.d.ts",type:"module",exports:{".":"./dist/webpack/index.js","./component":"./dist/component/component.js","./node":"./dist/node/node.js","./react":"./dist/react/react.js","./webpack":"./dist/webpack/index.js"},scripts:{build:"npm run build:lib && npm run build:docs","build:lib":"tsx build.ts","build:dev":"cross-env DEVELOPMENT=1 npm run build","build:docs":"typedoc src/index.mts --out dist/docs/",deploy:'npm run build && gsutil cp ./dist/web-weaver.min.js gs://metapress-cdn/web-weaver.min.js && gcloud compute url-maps invalidate-cdn-cache mp-cdn-loadbalancer --project="mp-backend-api" --path "/web-weaver.min.js" --async',"start:server":"cd server && npm run start","deploy:server":"cd server && npm run deploy","llm:build":"cd llm-server && docker build -t web-weaver-llm .","llm:start":"npm run llm:build && docker run -it --rm -p 8000:80 --gpus=all web-weaver-llm","llm:deploy.docker":"npm run llm:build && gcloud auth configure-docker us-central1-docker.pkg.dev && docker tag web-weaver-llm us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm && docker push us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm","llm:deploy":'npm run llm:deploy.docker && gcloud run deploy web-weaver-llm --project=ydangle-web-companion --image=us-central1-docker.pkg.dev/ydangle-web-companion/docker-artifacts/web-weaver-llm --allow-unauthenticated --region=us-central1 --description="Web Weaver LLM" --concurrency=2 --min-instances=0 --timeout=5m --memory=16Gi --cpu=8',prepack:"npm run build",test:"npm run build && IW_TESTER_API_KEY=97150cfc-5b97-413e-8da3-e86d01f70057 tsx --test"},keywords:["web","weaver","ai","assistant","chat"],author:"jjv360",license:"UNLICENSED",devDependencies:{"@types/audioworklet":"^0.0.64","@types/lodash":"^4.17.13","@types/react":"^18.3.12","@types/uuid":"^10.0.0",bestzip:"^2.2.1","cross-env":"^7.0.3","find-cache-dir":"^5.0.0",lodash:"^4.17.21","onnxruntime-web":"^1.20.0",react:"^18.3.1","replace-in-file":"^8.2.0",tsup:"^8.3.5",tsx:"^4.19.2",typedoc:"^0.27.6"},peerDependencies:{"onnxruntime-web":"^1.20.0",react:"^18 || ^19"},dependencies:{"@modelcontextprotocol/sdk":"^1.12.1","@types/json-schema":"^7.0.15","gpt-tokenizer":"^2.9.0",minisearch:"^6.3.0","rehype-document":"^7.0.3","rehype-external-links":"^3.0.0","rehype-format":"^5.0.0","rehype-stringify":"^10.0.0","remark-parse":"^11.0.0","remark-rehype":"^11.1.0",unified:"^11.0.4","utility-types":"^3.11.0",uuid:"^10.0.0"}};import{LoggingMessageNotificationSchema as te,ToolListChangedNotificationSchema as se}from"@modelcontextprotocol/sdk/types.js";var m=new g("MCPKnowledgeClient"),M=class{constructor(t){this.tools=[];this.iwActions=[];this.stats={toolsCalled:0};this.lastSearchQuery="";this.lastSearchResults=[];this.config=t}async connect(){return this.client?this.client:this.connectionPromise?await this.connectionPromise:(this.connectionPromise=this.connectInternal(),this.connectionPromise.finally(()=>{this.connectionPromise=void 0}),await this.connectionPromise)}async connectInternal(){m.debug("Connecting to MCP client");let t=this.config.connect?await this.config.connect():await Promise.resolve().then(async()=>{let e=new H({name:y.name,version:y.version}),s=new Q(new URL(this.config.baseURL));return await e.connect(s),m.debug("Connected with HTTP streaming mode"),e}).catch(async e=>{let s=new H({name:y.name,version:y.version}),n=new ee(new URL(this.config.baseURL));return await s.connect(n),m.debug("Connected with SSE mode"),s});return await this.disconnect(),this.client=t,t.onerror=e=>{m.error(`MCP client error: ${e.message}`)},t.onclose=()=>{m.debug("MCP client connection closed"),this.client=void 0},t.setNotificationHandler(te,e=>{e.params.level=="critical"?m.error(`[Server] ${e.params.data}`):e.params.level=="emergency"?m.error(`[Server] ${e.params.data}`):e.params.level=="error"?m.error(`[Server] ${e.params.data}`):e.params.level=="warning"?m.warn(`[Server] ${e.params.data}`):e.params.level=="info"?m.info(`[Server] ${e.params.data}`):e.params.level=="debug"?m.debug(`[Server] ${e.params.data}`):m.log(`[Server] ${e.params.data}`)}),t.setNotificationHandler(se,e=>{m.debug("Tool list changed",e),this.fetchTools()}),m.debug("Fetching tools from MCP server..."),await this.fetchTools(),t}async disconnect(){await this.client?.close(),this.client=void 0,this.tools=[],this.iwActions=[]}async fetchTools(){let t=[],e;for(;;){let n=await this.client.listTools({cursor:e});e=n.nextCursor;for(let i of n.tools||[])t.push(i);if(!n?.tools?.length||!e)break}let s=[];for(let n of t){let i=!!(this.config.searchToolName&&n.name==this.config.searchToolName&&!this.config.searchToolVisible);s.push({id:n.name,name:n.name,content:n.description||"",type:"action",isContext:!0,parameters:n.inputSchema,action:l=>this.performToolCall(n.name,l),disabled:i})}m.debug(`Fetched ${t.length} tools from MCP server.`),this.tools=t,this.iwActions=s}async search(t){if(t==this.lastSearchQuery)return this.lastSearchResults;await this.connect();let e=this.iwActions.slice(),s=await this.performSearchCall(t);return e=e.concat(s),this.lastSearchQuery=t,this.lastSearchResults=e,m.debug(`Search completed, found ${e.length} items.`),e}async performSearchCall(t){let e=[];if(!this.config.searchToolName)return e;let s=this.tools.find(i=>i.name==this.config.searchToolName);if(!s)return m.warn(`Search function ${this.config.searchToolName} not found on the MCP server.`),e;let n;if(s.inputSchema.required?.length==1){let i=s.inputSchema.required[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else if(s.inputSchema.properties&&Object.keys(s.inputSchema.properties).length==1){let i=Object.keys(s.inputSchema.properties)[0];n=await this.performToolCall(this.config.searchToolName,{[i]:t})}else n=await this.performToolCall(this.config.searchToolName,{});if(Array.isArray(n))for(let i=0;i<n.length;i++){let l=n[i];e.push({id:this.config.id+":"+this.config.searchToolName+":"+i,name:`Search result ${i+1} in ${this.config.searchToolName}`,type:"info",content:typeof l=="string"?l:JSON.stringify(l)})}else e.push({id:this.config.id+":"+this.config.searchToolName+":result",name:`Search result in ${this.config.searchToolName}`,type:"info",content:typeof n=="string"?n:JSON.stringify(n)});return e}async performToolCall(t,e){await this.connect(),m.debug(`Performing tool call for ${t} with params:`,e),this.stats.toolsCalled+=1;let s=await this.client.callTool({name:t,arguments:e});if(s.isError){let n=s.content?.[0]?.text||"Unknown error";throw m.error(`Error calling tool ${t}: ${n}`),new Error(`Error calling tool ${t}: ${n}`)}if(Array.isArray(s.content)&&s.content.length==1&&s.content[0].type=="text")try{return JSON.parse(s.content[0].text)}catch{}return s.content||[]}};var v=new g("KnowledgeBase"),K=1,P=class u{constructor(t){this._sources=[{id:"core.internal",query:async()=>q}];this._windowSources=[];this.lastResults=[];this.manualEntries=[];this.ai=t}registerSource(t,e){let s=t;return typeof t=="function"&&(e=t,s=`source.${K++}`),this._sources.push({id:s,query:e}),s}removeSource(t){this._sources=this.sources.filter(e=>e.id!==t&&e.query!==t)}addEntry(t){this.manualEntries.push(t)}removeEntry(t){this.manualEntries=this.manualEntries.filter(e=>e.id!==t)}get sources(){let t=this._sources;return f().knowledgeBaseSources&&(t=t.concat(f().knowledgeBaseSources)),t=t.concat(this._windowSources),t=t.filter(e=>!e.disabled),t}async search(t){v.debug(`Searching knowledge base for: ${t}`);let e=new Event("webweaver_kb_search",{bubbles:!0,cancelable:!0});e.query=t,e.entries=[],e.sources=[],typeof document<"u"&&document.dispatchEvent(e),this._windowSources=e.sources;let n=(await Promise.all(this.sources.map(async o=>{try{let r=Date.now(),a=await o.query(t);return v.debug(`Source '${o.id}' took ${Date.now()-r}ms`),a||[]}catch(r){return v.warn(`Knowledge source '${o.id}' failed:`,r),[]}}))).flat();n=n.concat(e.entries),n=n.concat(this.manualEntries),f().knowledgeBase&&(n=n.concat(f().knowledgeBase)),n=n.filter(o=>o&&!o.disabled);for(let o=0;o<n.length;o++){let r=n[o];r.id=r.id||`temp.${o}`,r._functionID=r.id.replaceAll(/[^a-zA-Z0-9_]/g,"_"),!r.action&&r.type=="tour"&&(r.action=a=>{throw new Error("This tour does not have an action. You must perform the tour content manually.")}),!r.action&&r.type=="info"&&(r.action=a=>{throw new Error("This item does not have an action. Use the content to provide information to the user instead.")})}let i=new ne({fields:["id","type","name","content","tags"],storeFields:[],searchOptions:{boost:{name:3,tags:2},fuzzy:.2}});i.addAll(n);let c=i.search(t).map(o=>n.find(r=>r.id==o.id));for(let o of n)o.isContext&&(c.find(r=>r.id===o.id)||c.push(o));return this.lastResults=c,v.debug("Found results:",c),c}getCachedEntry(t){return this.lastResults.find(e=>e.id==t||e._functionID==t)}registerSourceFromURL(t,e){e||(e=`external.${K++}`),v.debug(`Registering remote knowledge base source: ${t}`);let s=[],n=[],i=!0,l=async(o,r)=>{v.debug(`Calling remote knowledge base action: ${o.id}`);let a={type:"action",userID:this.ai.userID,extra:this.ai.extra,actionID:o.id,parameters:r},d=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!d.ok)throw new Error(`HTTP Error ${d.status} ${d.statusText}`);let h=await d.json();return c(h.updateItems||[]),h.response},c=o=>{for(let r of o){if(!r.id){v.warn("KB item skipped since it has no ID.",r);continue}let a=s.find(d=>d.id==r.id);if(a){a.name=r.name||a.name||"",a.content=r.content||a.content||"",a.disabled=r.disabled??a.disabled,a.isContext=r.isContext??a.isContext,a.parameters=r.parameters||a.parameters||[],a.tags=r.tags||a.tags,a.type=r.type||a.type;continue}s.push({...r,action:d=>l(r,d)})}};this.registerSource(e,async o=>{if(i&&n.includes(o))return s;let r={type:"search",userID:this.ai?.userID||"",extra:this.ai?.extra||{},query:o},a=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});if(!a.ok)throw new Error(`HTTP Error ${a.status} ${a.statusText}`);let d=await a.json();return i=!d.noCache,n.includes(o)||n.push(o),c(d.items),s})}clone(){let t=new u(this.ai);return t._sources=this._sources,t._windowSources=this._windowSources,t.manualEntries=this.manualEntries,t}registerMCPSource(t){t.id||(t.id=`external.${K++}`);let e=new M(t);return this.registerSource(t.id,s=>e.search(s)),e}};import{v4 as D}from"uuid";var A=class{constructor(t){this.ai=t}async boolean(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
13
13
|
|
|
14
14
|
Return only the text "true" or "false" to indicate the answer to the question. Do not include any additional text or explanations.`});if(e.toLowerCase().includes("true"))return!0;if(e.toLowerCase().includes("false"))return!1;throw new Error("The AI did not give a boolean answer: "+e)}async choose(t){let e=await this.instruct({...t,instruction:`${t.instruction}
|
|
15
15
|
|
|
@@ -26,7 +26,7 @@ Objects should have the following keys:
|
|
|
26
26
|
${t.extractions.map(n=>`- ${n.name} (${n.type}): ${n.description||"No description provided."}`).join(`
|
|
27
27
|
`)}
|
|
28
28
|
`,s=await this.instruct({...t,instruction:e});if(s=s.replace(/```jsonl/g,"").replace(/```json/g,"").replace(/```/g,""),t.allowMultiple){let n=s.split(`
|
|
29
|
-
`).map(
|
|
30
|
-
`});else if(i.type=="action"){let
|
|
29
|
+
`).map(l=>l.trim()).filter(l=>!!l.trim()),i=[];for(let l of n)try{i.push(JSON.parse(l))}catch(c){console.error("Failed to parse line as JSON:",l,c.message)}return i}else return[JSON.parse(s.trim())]}async generateMarkdown(t){return this.instruct({...t,instruction:"Generate a Markdown document based on the input text. Always include a header on every response. Give long detailed answers with many paragraphs, and explain concepts step by step."})}async instruct(t){let e=this.ai.clone();return e.resetConversation(),e.getContextPrefix=async()=>t.instruction,t.callback&&e.addEventListener("output",s=>{t.callback(s.detail.message)}),t.allowKB||(e.knowledgeBase._sources=[]),await e.sendMessage(typeof t.data=="string"?t.data:JSON.stringify(t.data))||""}};var p=new g("Main"),N=class N extends EventTarget{constructor(){super(...arguments);this.conversationID=D();this.knowledgeBase=new P(this);this._lastKBsearch="";this.models=[];this.audio=null;this.apiKey="";this.logic=new A(this);this.userID=U();this.extra=f().extra||{};this.hubAPI="https://intelliweave.ai/api";this._lastSystemMsg="";this.isProcessing=!1}get loaded(){return!!(this.config&&this.currentModel)}async load(e){if(this.apiKey=e,!e)throw new Error("API key is required to load the AI.");try{await Promise.all([(async()=>{p.debug("Loading configuration...");let s=await fetch(this.hubAPI+"/config/get",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:e})});if(!s.ok)throw new Error(`Failed to load configuration: ${s.status} ${s.statusText}`);try{this.config=await s.json(),p.debug("Config loaded successfully:",this.config),this.config&&p.debug("UI Properties - logo:",this.config.logo,"background:",this.config.background,"textColor:",this.config.textColor)}catch(n){p.error("Failed to parse JSON response:",n);try{let i=await s.clone().text();p.error("Raw response that failed parsing:",i)}catch{p.error("Could not get raw response text either")}throw new Error(`Failed to parse configuration: ${n}`)}})(),(async()=>{try{let s=await fetch("https://cdn.intelliweave.ai/models/silero_vad_3.onnx");if(!s.ok)throw new Error(`Failed to load VAD model: ${s.status} ${s.statusText}`);this.vadModel=await s.blob()}catch(s){p.warn(`Failed to load VAD model, some features will be unavailable. ${s.message}`)}})()]),this.models=[{id:this.config.id,config:this.config.model}],this.setModel(this.config.id);for(let s of this.config.knowledge||[])s.url&&s.backendType=="mcp"?this.knowledgeBase.registerMCPSource({id:s.id,searchToolName:s.mcpSearchToolName,baseURL:s.url}):s.url&&this.knowledgeBase.registerSourceFromURL(s.url);for(let s of this.config.knowledge||[])s.entries?.length&&this.knowledgeBase.registerSource(s.id,()=>s.entries||[]);if(this.config.mcpServers)for(let s of this.config.mcpServers)this.knowledgeBase.registerMCPSource(s);return this.resetConversation(),this.dispatchEvent(new CustomEvent("load",{detail:{ai:this}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_loaded",{detail:{ai:this}})),this.config}catch(s){throw p.warn("Failed to load:",s),this.error=s,this.dispatchEvent(new CustomEvent("error",{detail:{ai:this,error:s}})),typeof window<"u"&&window.dispatchEvent(new CustomEvent("webweaver_error",{detail:{ai:this,error:s}})),s}}setModel(e){let s=this.models.find(n=>n.id==e);if(!s)throw new Error(`Model with ID "${e}" not found.`);this.currentModel=new E({...s.config,systemMessage:"",onBeforeMessageProcessing:this.onBeforeMessageProcessing.bind(this),onAIMessage:this.processIncomingMessage.bind(this),onAIToolStart:(n,i)=>{let l=this.knowledgeBase.getCachedEntry(n);this.onAIToolStart?.(l?.id||n,i)}}),this.currentModel.id=s.id,this.currentModel.metadata=s}async getContextPrefix(){let e=f().pageSummary||`You are ${this.config?.name||"IntelliWeave"}. ${this.config?.instructions||"Speak in short sentences."}`;return typeof e=="function"&&(e=await e()),e}async onBeforeMessageProcessing(){this._lastKBsearch||(this._lastKBsearch="__intelliweaveblanksearchforcontextitems__");let e=await this.getContextPrefix();this.currentModel.contextGroup.add({id:"_iw_main",sortOrder:1,cannotRemove:!0,content:e}),this.currentModel.contextGroup.add({id:"_iw_kb_description",sortOrder:100,cannotRemove:!0,content:'You have access to a database of knowledge base items. These include "info" type items which provide information and "tour" type items which contain instructions you should follow (only do one item at a time). Current info and tour items:'});let s=this._nextRequestUseKBitems||await this.knowledgeBase.search(this._lastKBsearch);this._nextRequestUseKBitems=void 0;for(let i of s)if(i.type=="info"||i.type=="tour"||i.type=="input-event")this.currentModel.contextGroup.add({id:i.id,customData:i,cannotRemove:i.isContext,sortOrder:101,disabled:i.disabled,content:JSON.stringify({id:i.id,type:i.type,name:i.name,content:W(typeof i.content=="function"?i.content():i.content)})+`
|
|
30
|
+
`});else if(i.type=="action"){let l={name:i._functionID,description:W(typeof i.content=="function"?i.content():i.content),params:j(i.parameters||[{name:"value",type:"string",description:"Input"}]),removeFromMessageHistory:!!i.removeFromMessageHistory,callback:c=>this.toolRunKBAction(i,c),kb:i};this.currentModel.toolGroup.add({id:i._functionID,customData:l,cannotRemove:i.isContext,sortOrder:101,disabled:i.disabled,content:JSON.stringify(l)})}else continue;let n=this.currentModel.contextGroup.getAllAsString();this._lastSystemMsg!=n&&(this._lastSystemMsg=n,this.submitAnalyticsEvent({type:"system-msg",txt:n}))}processIncomingMessage(e,s){s||this.submitAnalyticsEvent({type:"message",role:"assistant",message:e});let n="";this._lastOutput===void 0?(n=e,this._lastOutput=e):e.startsWith(this._lastOutput)?(n=e.substring(this._lastOutput.length),this._lastOutput=e):(p.warn("Message was changed during chunking. This should not happen.",e,this._lastOutput),n=e,this._lastOutput=e),s||(this._lastOutput=void 0),this.dispatchEvent(new CustomEvent("output",{detail:{ai:this,isChunk:s,message:e,messageChunk:n}})),this.onAIMessage?.(e,!!s)}async sendMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(this.isProcessing)return p.warn("Cannot send message while another message is being processed."),null;this.isProcessing=!0;try{return this.submitAnalyticsEvent({type:"message",role:"user",message:e}),this.dispatchEvent(new CustomEvent("input",{detail:{ai:this,message:e}})),await this.currentModel.sendMessage(e)}finally{this.isProcessing=!1}}async toolRunKBAction(e,s){try{this.dispatchEvent(new CustomEvent("toolstart",{detail:{knowledgeBaseEntry:e,input:s,ai:this}}));let n=await e.action(s,this);return this.submitAnalyticsEvent({type:"action",action:e.id,value:s,result:n}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:s,ai:this,result:n}})),n}catch(n){throw this.submitAnalyticsEvent({type:"action",action:e.id,value:s,error:n.message}),this.dispatchEvent(new CustomEvent("tool",{detail:{knowledgeBaseEntry:e,input:s,ai:this,error:n}})),n}}submitAnalyticsEvent(e){f().analytics===!1||this.config?.analytics===!1||fetch(this.hubAPI+"/analytics/post",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({...e,apiKey:this.apiKey,time:Date.now(),conversationID:this.conversationID,personaID:this.config?.id})}).catch(s=>{p.debug("Failed to submit analytics event:",s)})}resetConversation(){this.currentModel&&(this.currentModel.resetConversation(),this._lastKBsearch="",this._nextRequestUseKBitems=void 0,this.conversationID=D(),this._lastSystemMsg="")}insertAssistantMessage(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");let s={role:"assistant",content:e};this.currentModel.messageGroup.add({id:D(),content:JSON.stringify(s),customData:s})}exportState(){return{type:"intelliweave/state/v1",conversationID:this.conversationID,messages:this.currentModel?.messageGroup.getAll().map(e=>e.customData)}}importState(e){if(!this.currentModel)throw new Error("No model selected. Please call load() first.");if(e?.type!="intelliweave/state/v1")throw new Error(`Invalid state type: ${e.type}`);this.conversationID=e.conversationID;for(let s of e.messages)this.currentModel.messageGroup.add({id:D(),content:JSON.stringify(s),customData:s})}clone(){let e=new N;return e.apiKey=this.apiKey,e.config=this.config,e.models=this.models,this.config?.id&&e.setModel(this.config.id),e.audio=this.audio,e.vadModel=this.vadModel,e.userID=this.userID,e.extra=this.extra,e.knowledgeBase=this.knowledgeBase.clone(),e}};N.version=y.version;var O=N;var $=new g("Stream"),Y=class extends O{constructor(){super(...arguments);this.pendingEvents=[];this._isProcessingEvents=!1}async getContextPrefix(){let e=`You will receive an event stream, a list of JSON objects, one on each line. Respond with tool calls only and no text output.
|
|
31
31
|
|
|
32
|
-
`;return e+=await super.getContextPrefix(),e}postEvent(e){if(!e.eventName)throw new Error("Missing event name");if(e.direction||(e.direction="input"),e.direction!="input")throw new Error('Invalid event direction, must be "input".');e.timestamp||(e.timestamp=Date.now()),e.timestampDate=new Date(e.timestamp).toString()
|
|
32
|
+
`;return e+=await super.getContextPrefix(),e}postEvent(e){if(!e.eventName)throw new Error("Missing event name");if(e.direction||(e.direction="input"),e.direction!="input")throw new Error('Invalid event direction, must be "input".');e.timestamp||(e.timestamp=Date.now()),e.timestampDate=new Date(e.timestamp).toString(),$.log("Event in",JSON.stringify(e)),this.pendingEvents.push(e),this.processEvents()}async toolRunKBAction(e,s){if(e.type!="output-event")return await super.toolRunKBAction(e,s);this.emitEvent({...s,eventName:e.id,timestamp:Date.now(),timestampDate:new Date().toString(),direction:"output"})}emitEvent(e){$.log("Event out",JSON.stringify(e)),this.dispatchEvent(new CustomEvent("event",{detail:{ai:this,event:e}}))}async processEvents(){if(!this._isProcessingEvents){this._isProcessingEvents=!0;try{let e=this.pendingEvents.shift();if(!e)return;let s=this.knowledgeBase.getCachedEntry(e.eventName);s&&(e.assistantHint=typeof s.content=="function"?s.content():s.content);let n=JSON.stringify(e),i=await this.sendMessage(n)||"";if(!i.trim())return;this.emitEvent({eventName:"text",text:i,timestamp:Date.now(),timestampDate:new Date().toString(),direction:"output"})}catch(e){$.error("Error processing events:",e),this.emitEvent({eventName:"error",timestamp:Date.now(),timestampDate:new Date().toString(),errorMessage:e.message,direction:"output"})}finally{this._isProcessingEvents=!1,this.pendingEvents.length>0&&this.processEvents()}}}};export{F as BufferedWebSocket,E as ChatGPT,L as FixedBufferStream,O as IntelliWeave,Y as IntelliWeaveStream,P as KnowledgeBase,g as Logging,M as MCPKnowledgeClient,J as ONNXModel,z as Resampler,B as TokenWindow,G as TokenWindowGroup,ue as audioToWav,j as convertParamsToJSONSchema,ge as floatTo16BitPCM,fe as floatTo64BitPCM,U as getDefaultUserID,pe as int16ToFloat32BitPCM,f as intelliweaveConfig,I as intelliweaveGlobalThis,R as sseEvents,W as trimWhitespaceInText};
|