@microfox/ai-router 2.1.0 → 2.1.2
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/CHANGELOG.md +12 -0
- package/dist/chunk-BJTO5JO5.mjs +11 -0
- package/dist/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/fs_store.d.mts +5 -0
- package/dist/fs_store.d.ts +5 -0
- package/dist/fs_store.js +122 -0
- package/dist/fs_store.js.map +1 -0
- package/dist/fs_store.mjs +101 -0
- package/dist/fs_store.mjs.map +1 -0
- package/dist/index.d.mts +340 -0
- package/dist/index.d.ts +340 -0
- package/dist/index.js +1164 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1122 -0
- package/dist/index.mjs.map +1 -0
- package/dist/store-BBHh-uTh.d.mts +27 -0
- package/dist/store-BBHh-uTh.d.ts +27 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
__require
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=chunk-BJTO5JO5.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/fs_store.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/fs_store.ts
|
|
21
|
+
var fs_store_exports = {};
|
|
22
|
+
__export(fs_store_exports, {
|
|
23
|
+
FileSystemStore: () => FileSystemStore
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(fs_store_exports);
|
|
26
|
+
var NoOpStore = class {
|
|
27
|
+
constructor() {
|
|
28
|
+
console.warn(
|
|
29
|
+
"FileSystemStore is not available in the browser. Using a mock store."
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
async get(_key) {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
async set(_key, _value) {
|
|
36
|
+
}
|
|
37
|
+
async delete(_key) {
|
|
38
|
+
}
|
|
39
|
+
async has(_key) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var store;
|
|
44
|
+
if (typeof window !== "undefined") {
|
|
45
|
+
store = NoOpStore;
|
|
46
|
+
} else {
|
|
47
|
+
const fs = require("fs/promises");
|
|
48
|
+
const path = require("path");
|
|
49
|
+
const AsyncLock = require("async-lock");
|
|
50
|
+
class FileSystemStore2 {
|
|
51
|
+
constructor(storagePath = ".ai-router.store.json") {
|
|
52
|
+
this.storeFilePath = path.resolve(storagePath);
|
|
53
|
+
this.lock = new AsyncLock();
|
|
54
|
+
this.init();
|
|
55
|
+
}
|
|
56
|
+
async init() {
|
|
57
|
+
try {
|
|
58
|
+
await fs.access(this.storeFilePath);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
61
|
+
await fs.writeFile(this.storeFilePath, "{}", "utf-8");
|
|
62
|
+
} else {
|
|
63
|
+
console.error("Failed to access or create storage file:", error);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async readStore() {
|
|
68
|
+
try {
|
|
69
|
+
const fileContent = await fs.readFile(this.storeFilePath, "utf-8");
|
|
70
|
+
return JSON.parse(fileContent);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error(
|
|
73
|
+
"Failed to read store file, returning empty object:",
|
|
74
|
+
error
|
|
75
|
+
);
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async writeStore(data) {
|
|
80
|
+
const fileContent = JSON.stringify(data, null, 2);
|
|
81
|
+
await fs.writeFile(this.storeFilePath, fileContent, "utf-8");
|
|
82
|
+
}
|
|
83
|
+
async get(key) {
|
|
84
|
+
return this.lock.acquire(this.storeFilePath, async () => {
|
|
85
|
+
const store2 = await this.readStore();
|
|
86
|
+
return store2[key];
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async set(key, value) {
|
|
90
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
91
|
+
const store2 = await this.readStore();
|
|
92
|
+
store2[key] = value;
|
|
93
|
+
await this.writeStore(store2);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async delete(key) {
|
|
97
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
98
|
+
const store2 = await this.readStore();
|
|
99
|
+
delete store2[key];
|
|
100
|
+
await this.writeStore(store2);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async has(key) {
|
|
104
|
+
return this.lock.acquire(this.storeFilePath, async () => {
|
|
105
|
+
const store2 = await this.readStore();
|
|
106
|
+
return key in store2;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
async clear() {
|
|
110
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
111
|
+
await this.writeStore({});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
store = FileSystemStore2;
|
|
116
|
+
}
|
|
117
|
+
var FileSystemStore = store;
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
FileSystemStore
|
|
121
|
+
});
|
|
122
|
+
//# sourceMappingURL=fs_store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fs_store.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { Store } from './store';\n\n// Type definition for AsyncLock since @types/async-lock might not be available\ntype AsyncLock = {\n acquire<T>(key: string, fn: () => Promise<T>): Promise<T>;\n};\n\nclass NoOpStore implements Store {\n constructor() {\n console.warn(\n 'FileSystemStore is not available in the browser. Using a mock store.'\n );\n }\n\n async get<T>(_key: string): Promise<T | undefined> {\n return undefined;\n }\n\n async set<T>(_key: string, _value: T): Promise<void> {\n // No-op\n }\n\n async delete(_key: string): Promise<void> {\n // No-op\n }\n\n async has(_key: string): Promise<boolean> {\n return false;\n }\n}\n\nlet store: new (...args: any[]) => Store;\nif (typeof window !== 'undefined') {\n store = NoOpStore;\n} else {\n const fs = require('fs/promises');\n const path = require('path');\n const AsyncLock = require('async-lock');\n\n class FileSystemStore implements Store {\n private storeFilePath: string;\n private lock: AsyncLock;\n\n constructor(storagePath: string = '.ai-router.store.json') {\n this.storeFilePath = path.resolve(storagePath);\n this.lock = new AsyncLock();\n this.init();\n }\n\n private async init() {\n try {\n // Check if file exists by trying to read it.\n await fs.access(this.storeFilePath);\n } catch (error: unknown) {\n // If not, create it with an empty object.\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n error.code === 'ENOENT'\n ) {\n await fs.writeFile(this.storeFilePath, '{}', 'utf-8');\n } else {\n console.error('Failed to access or create storage file:', error);\n }\n }\n }\n\n private async readStore(): Promise<Record<string, unknown>> {\n try {\n const fileContent = await fs.readFile(this.storeFilePath, 'utf-8');\n return JSON.parse(fileContent);\n } catch (error) {\n console.error(\n 'Failed to read store file, returning empty object:',\n error\n );\n return {};\n }\n }\n\n private async writeStore(data: Record<string, unknown>): Promise<void> {\n const fileContent = JSON.stringify(data, null, 2);\n await fs.writeFile(this.storeFilePath, fileContent, 'utf-8');\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return store[key] as T | undefined;\n });\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n store[key] = value;\n await this.writeStore(store);\n });\n }\n\n async delete(key: string): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n delete store[key];\n await this.writeStore(store);\n });\n }\n\n async has(key: string): Promise<boolean> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return key in store;\n });\n }\n\n async clear(): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n await this.writeStore({});\n });\n }\n }\n store = FileSystemStore;\n}\n\nexport const FileSystemStore = store;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,IAAM,YAAN,MAAiC;AAAA,EAC/B,cAAc;AACZ,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,MAAsC;AACjD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAO,MAAc,QAA0B;AAAA,EAErD;AAAA,EAEA,MAAM,OAAO,MAA6B;AAAA,EAE1C;AAAA,EAEA,MAAM,IAAI,MAAgC;AACxC,WAAO;AAAA,EACT;AACF;AAEA,IAAI;AACJ,IAAI,OAAO,WAAW,aAAa;AACjC,UAAQ;AACV,OAAO;AACL,QAAM,KAAK,QAAQ,aAAa;AAChC,QAAM,OAAO,QAAQ,MAAM;AAC3B,QAAM,YAAY,QAAQ,YAAY;AAAA,EAEtC,MAAMA,iBAAiC;AAAA,IAIrC,YAAY,cAAsB,yBAAyB;AACzD,WAAK,gBAAgB,KAAK,QAAQ,WAAW;AAC7C,WAAK,OAAO,IAAI,UAAU;AAC1B,WAAK,KAAK;AAAA,IACZ;AAAA,IAEA,MAAc,OAAO;AACnB,UAAI;AAEF,cAAM,GAAG,OAAO,KAAK,aAAa;AAAA,MACpC,SAAS,OAAgB;AAEvB,YACE,SACA,OAAO,UAAU,YACjB,UAAU,SACV,MAAM,SAAS,UACf;AACA,gBAAM,GAAG,UAAU,KAAK,eAAe,MAAM,OAAO;AAAA,QACtD,OAAO;AACL,kBAAQ,MAAM,4CAA4C,KAAK;AAAA,QACjE;AAAA,MACF;AAAA,IACF;AAAA,IAEA,MAAc,YAA8C;AAC1D,UAAI;AACF,cAAM,cAAc,MAAM,GAAG,SAAS,KAAK,eAAe,OAAO;AACjE,eAAO,KAAK,MAAM,WAAW;AAAA,MAC/B,SAAS,OAAO;AACd,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,QACF;AACA,eAAO,CAAC;AAAA,MACV;AAAA,IACF;AAAA,IAEA,MAAc,WAAW,MAA8C;AACrE,YAAM,cAAc,KAAK,UAAU,MAAM,MAAM,CAAC;AAChD,YAAM,GAAG,UAAU,KAAK,eAAe,aAAa,OAAO;AAAA,IAC7D;AAAA,IAEA,MAAM,IAAO,KAAqC;AAChD,aAAO,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACvD,cAAMC,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAOA,OAAM,GAAG;AAAA,MAClB,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,IAAO,KAAa,OAAyB;AACjD,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,QAAAA,OAAM,GAAG,IAAI;AACb,cAAM,KAAK,WAAWA,MAAK;AAAA,MAC7B,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,OAAO,KAA4B;AACvC,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAOA,OAAM,GAAG;AAChB,cAAM,KAAK,WAAWA,MAAK;AAAA,MAC7B,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,IAAI,KAA+B;AACvC,aAAO,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACvD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAO,OAAOA;AAAA,MAChB,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,QAAuB;AAC3B,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAM,KAAK,WAAW,CAAC,CAAC;AAAA,MAC1B,CAAC;AAAA,IACH;AAAA,EACF;AACA,UAAQD;AACV;AAEO,IAAM,kBAAkB;","names":["FileSystemStore","store"]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__require
|
|
3
|
+
} from "./chunk-BJTO5JO5.mjs";
|
|
4
|
+
|
|
5
|
+
// src/fs_store.ts
|
|
6
|
+
var NoOpStore = class {
|
|
7
|
+
constructor() {
|
|
8
|
+
console.warn(
|
|
9
|
+
"FileSystemStore is not available in the browser. Using a mock store."
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
async get(_key) {
|
|
13
|
+
return void 0;
|
|
14
|
+
}
|
|
15
|
+
async set(_key, _value) {
|
|
16
|
+
}
|
|
17
|
+
async delete(_key) {
|
|
18
|
+
}
|
|
19
|
+
async has(_key) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var store;
|
|
24
|
+
if (typeof window !== "undefined") {
|
|
25
|
+
store = NoOpStore;
|
|
26
|
+
} else {
|
|
27
|
+
const fs = __require("fs/promises");
|
|
28
|
+
const path = __require("path");
|
|
29
|
+
const AsyncLock = __require("async-lock");
|
|
30
|
+
class FileSystemStore2 {
|
|
31
|
+
constructor(storagePath = ".ai-router.store.json") {
|
|
32
|
+
this.storeFilePath = path.resolve(storagePath);
|
|
33
|
+
this.lock = new AsyncLock();
|
|
34
|
+
this.init();
|
|
35
|
+
}
|
|
36
|
+
async init() {
|
|
37
|
+
try {
|
|
38
|
+
await fs.access(this.storeFilePath);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
41
|
+
await fs.writeFile(this.storeFilePath, "{}", "utf-8");
|
|
42
|
+
} else {
|
|
43
|
+
console.error("Failed to access or create storage file:", error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async readStore() {
|
|
48
|
+
try {
|
|
49
|
+
const fileContent = await fs.readFile(this.storeFilePath, "utf-8");
|
|
50
|
+
return JSON.parse(fileContent);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(
|
|
53
|
+
"Failed to read store file, returning empty object:",
|
|
54
|
+
error
|
|
55
|
+
);
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async writeStore(data) {
|
|
60
|
+
const fileContent = JSON.stringify(data, null, 2);
|
|
61
|
+
await fs.writeFile(this.storeFilePath, fileContent, "utf-8");
|
|
62
|
+
}
|
|
63
|
+
async get(key) {
|
|
64
|
+
return this.lock.acquire(this.storeFilePath, async () => {
|
|
65
|
+
const store2 = await this.readStore();
|
|
66
|
+
return store2[key];
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
async set(key, value) {
|
|
70
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
71
|
+
const store2 = await this.readStore();
|
|
72
|
+
store2[key] = value;
|
|
73
|
+
await this.writeStore(store2);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async delete(key) {
|
|
77
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
78
|
+
const store2 = await this.readStore();
|
|
79
|
+
delete store2[key];
|
|
80
|
+
await this.writeStore(store2);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async has(key) {
|
|
84
|
+
return this.lock.acquire(this.storeFilePath, async () => {
|
|
85
|
+
const store2 = await this.readStore();
|
|
86
|
+
return key in store2;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async clear() {
|
|
90
|
+
await this.lock.acquire(this.storeFilePath, async () => {
|
|
91
|
+
await this.writeStore({});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
store = FileSystemStore2;
|
|
96
|
+
}
|
|
97
|
+
var FileSystemStore = store;
|
|
98
|
+
export {
|
|
99
|
+
FileSystemStore
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=fs_store.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fs_store.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { Store } from './store';\n\n// Type definition for AsyncLock since @types/async-lock might not be available\ntype AsyncLock = {\n acquire<T>(key: string, fn: () => Promise<T>): Promise<T>;\n};\n\nclass NoOpStore implements Store {\n constructor() {\n console.warn(\n 'FileSystemStore is not available in the browser. Using a mock store.'\n );\n }\n\n async get<T>(_key: string): Promise<T | undefined> {\n return undefined;\n }\n\n async set<T>(_key: string, _value: T): Promise<void> {\n // No-op\n }\n\n async delete(_key: string): Promise<void> {\n // No-op\n }\n\n async has(_key: string): Promise<boolean> {\n return false;\n }\n}\n\nlet store: new (...args: any[]) => Store;\nif (typeof window !== 'undefined') {\n store = NoOpStore;\n} else {\n const fs = require('fs/promises');\n const path = require('path');\n const AsyncLock = require('async-lock');\n\n class FileSystemStore implements Store {\n private storeFilePath: string;\n private lock: AsyncLock;\n\n constructor(storagePath: string = '.ai-router.store.json') {\n this.storeFilePath = path.resolve(storagePath);\n this.lock = new AsyncLock();\n this.init();\n }\n\n private async init() {\n try {\n // Check if file exists by trying to read it.\n await fs.access(this.storeFilePath);\n } catch (error: unknown) {\n // If not, create it with an empty object.\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n error.code === 'ENOENT'\n ) {\n await fs.writeFile(this.storeFilePath, '{}', 'utf-8');\n } else {\n console.error('Failed to access or create storage file:', error);\n }\n }\n }\n\n private async readStore(): Promise<Record<string, unknown>> {\n try {\n const fileContent = await fs.readFile(this.storeFilePath, 'utf-8');\n return JSON.parse(fileContent);\n } catch (error) {\n console.error(\n 'Failed to read store file, returning empty object:',\n error\n );\n return {};\n }\n }\n\n private async writeStore(data: Record<string, unknown>): Promise<void> {\n const fileContent = JSON.stringify(data, null, 2);\n await fs.writeFile(this.storeFilePath, fileContent, 'utf-8');\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return store[key] as T | undefined;\n });\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n store[key] = value;\n await this.writeStore(store);\n });\n }\n\n async delete(key: string): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n delete store[key];\n await this.writeStore(store);\n });\n }\n\n async has(key: string): Promise<boolean> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return key in store;\n });\n }\n\n async clear(): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n await this.writeStore({});\n });\n }\n }\n store = FileSystemStore;\n}\n\nexport const FileSystemStore = store;\n"],"mappings":";;;;;AAQA,IAAM,YAAN,MAAiC;AAAA,EAC/B,cAAc;AACZ,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,MAAsC;AACjD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAO,MAAc,QAA0B;AAAA,EAErD;AAAA,EAEA,MAAM,OAAO,MAA6B;AAAA,EAE1C;AAAA,EAEA,MAAM,IAAI,MAAgC;AACxC,WAAO;AAAA,EACT;AACF;AAEA,IAAI;AACJ,IAAI,OAAO,WAAW,aAAa;AACjC,UAAQ;AACV,OAAO;AACL,QAAM,KAAK,UAAQ,aAAa;AAChC,QAAM,OAAO,UAAQ,MAAM;AAC3B,QAAM,YAAY,UAAQ,YAAY;AAAA,EAEtC,MAAMA,iBAAiC;AAAA,IAIrC,YAAY,cAAsB,yBAAyB;AACzD,WAAK,gBAAgB,KAAK,QAAQ,WAAW;AAC7C,WAAK,OAAO,IAAI,UAAU;AAC1B,WAAK,KAAK;AAAA,IACZ;AAAA,IAEA,MAAc,OAAO;AACnB,UAAI;AAEF,cAAM,GAAG,OAAO,KAAK,aAAa;AAAA,MACpC,SAAS,OAAgB;AAEvB,YACE,SACA,OAAO,UAAU,YACjB,UAAU,SACV,MAAM,SAAS,UACf;AACA,gBAAM,GAAG,UAAU,KAAK,eAAe,MAAM,OAAO;AAAA,QACtD,OAAO;AACL,kBAAQ,MAAM,4CAA4C,KAAK;AAAA,QACjE;AAAA,MACF;AAAA,IACF;AAAA,IAEA,MAAc,YAA8C;AAC1D,UAAI;AACF,cAAM,cAAc,MAAM,GAAG,SAAS,KAAK,eAAe,OAAO;AACjE,eAAO,KAAK,MAAM,WAAW;AAAA,MAC/B,SAAS,OAAO;AACd,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,QACF;AACA,eAAO,CAAC;AAAA,MACV;AAAA,IACF;AAAA,IAEA,MAAc,WAAW,MAA8C;AACrE,YAAM,cAAc,KAAK,UAAU,MAAM,MAAM,CAAC;AAChD,YAAM,GAAG,UAAU,KAAK,eAAe,aAAa,OAAO;AAAA,IAC7D;AAAA,IAEA,MAAM,IAAO,KAAqC;AAChD,aAAO,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACvD,cAAMC,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAOA,OAAM,GAAG;AAAA,MAClB,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,IAAO,KAAa,OAAyB;AACjD,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,QAAAA,OAAM,GAAG,IAAI;AACb,cAAM,KAAK,WAAWA,MAAK;AAAA,MAC7B,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,OAAO,KAA4B;AACvC,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAOA,OAAM,GAAG;AAChB,cAAM,KAAK,WAAWA,MAAK;AAAA,MAC7B,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,IAAI,KAA+B;AACvC,aAAO,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACvD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAO,OAAOA;AAAA,MAChB,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,QAAuB;AAC3B,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAM,KAAK,WAAW,CAAC,CAAC;AAAA,MAC1B,CAAC;AAAA,IACH;AAAA,EACF;AACA,UAAQD;AACV;AAEO,IAAM,kBAAkB;","names":["FileSystemStore","store"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
import { UIMessageStreamWriter, UIMessage, GenerateObjectResult, UIDataTypes, generateId, ToolSet, JSONValue, Tool } from 'ai';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { S as Store } from './store-BBHh-uTh.mjs';
|
|
5
|
+
export { M as MemoryStore } from './store-BBHh-uTh.mjs';
|
|
6
|
+
|
|
7
|
+
type UITools = Record<string, {
|
|
8
|
+
input: unknown;
|
|
9
|
+
output: unknown | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
|
|
12
|
+
declare const findLastElement: <T>(array: T[]) => T;
|
|
13
|
+
declare const findFirstElement: <T>(array: T[]) => T;
|
|
14
|
+
declare class StreamWriter<METADATA, TOOLS extends UITools> {
|
|
15
|
+
writer: UIMessageStreamWriter<UIMessage<METADATA, any, TOOLS>>;
|
|
16
|
+
constructor(writer: UIMessageStreamWriter<UIMessage<METADATA, any, TOOLS>>);
|
|
17
|
+
generateId: () => string;
|
|
18
|
+
writeMessageMetadata: <NEW_METADATA extends METADATA>(metadata: NEW_METADATA) => void;
|
|
19
|
+
writeCustomTool: <K extends keyof TOOLS>(tool: {
|
|
20
|
+
toolCallId?: string;
|
|
21
|
+
toolName: K;
|
|
22
|
+
inputTextDelta?: string[];
|
|
23
|
+
input?: any;
|
|
24
|
+
output?: any;
|
|
25
|
+
}) => void;
|
|
26
|
+
writeObjectAsTool: <K extends keyof TOOLS>(tool: {
|
|
27
|
+
toolName: K;
|
|
28
|
+
result?: GenerateObjectResult<TOOLS[K]["output"]>;
|
|
29
|
+
input?: GenerateObjectResult<TOOLS[K]["input"]>;
|
|
30
|
+
}) => void;
|
|
31
|
+
}
|
|
32
|
+
declare const getTextParts: (message: UIMessage | null | undefined) => string[];
|
|
33
|
+
declare const getTextPartsContent: (message: UIMessage | null | undefined) => string;
|
|
34
|
+
declare const findLastMessageWith: <T>(message: UIMessage[] | null | undefined, filters: {
|
|
35
|
+
role?: "user" | "assistant" | "system";
|
|
36
|
+
metadata?: Record<string, any>;
|
|
37
|
+
}) => UIMessage<unknown, UIDataTypes, ai.UITools> | null | undefined;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Sets a global logger that will be used by all router instances when no instance-specific logger is set.
|
|
41
|
+
* This is useful for debugging across multiple router instances.
|
|
42
|
+
* @param logger The logger to use globally, or undefined to disable global logging
|
|
43
|
+
*/
|
|
44
|
+
declare function setGlobalLogger(logger?: AiLogger): void;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the current global logger.
|
|
47
|
+
* @returns The current global logger or undefined if none is set
|
|
48
|
+
*/
|
|
49
|
+
declare function getGlobalLogger(): AiLogger | undefined;
|
|
50
|
+
declare class AiKitError extends Error {
|
|
51
|
+
constructor(message: string);
|
|
52
|
+
}
|
|
53
|
+
declare class AgentNotFoundError extends AiKitError {
|
|
54
|
+
constructor(path: string);
|
|
55
|
+
}
|
|
56
|
+
declare class MaxCallDepthExceededError extends AiKitError {
|
|
57
|
+
constructor(maxDepth: number);
|
|
58
|
+
}
|
|
59
|
+
declare class AgentDefinitionMissingError extends AiKitError {
|
|
60
|
+
constructor(path: string);
|
|
61
|
+
}
|
|
62
|
+
type AiStreamWriter<METADATA = unknown, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = UIMessageStreamWriter<UIMessage<METADATA, PARTS, TOOLS>> & Omit<StreamWriter<METADATA, TOOLS>, 'writer'> & {
|
|
63
|
+
generateId: typeof generateId;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The context object passed to every agent, tool, and middleware. It contains
|
|
67
|
+
* all the necessary information and utilities for a handler to perform its work.
|
|
68
|
+
* @template METADATA - The type for custom metadata in UI messages.
|
|
69
|
+
* @template PARTS - The type for custom parts in UI messages.
|
|
70
|
+
* @template TOOLS - The type for custom tools in UI messages.
|
|
71
|
+
* @template ContextState - The type for the shared state object.
|
|
72
|
+
*/
|
|
73
|
+
type AiContext<METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = Record<string, any>, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = {
|
|
74
|
+
request: {
|
|
75
|
+
/** The message history for the current request. The user can modify this array to change the message history or manipulate the message history, but beware that in the routing, the messages are passed as a reference and not a copy, making the mutatated value available to all the handlers in the request chain. */
|
|
76
|
+
messages: UIMessage<METADATA, PARTS, TOOLS>[];
|
|
77
|
+
/** Parameters passed from an internal tool or agent call. */
|
|
78
|
+
params: PARAMS;
|
|
79
|
+
[key: string]: any;
|
|
80
|
+
} & METADATA;
|
|
81
|
+
/** A shared, mutable state object that persists for the lifetime of a single request. */
|
|
82
|
+
state: ContextState;
|
|
83
|
+
/** A shared, mutable store object that persists for the lifetime of a single request. */
|
|
84
|
+
store: Store;
|
|
85
|
+
/**
|
|
86
|
+
* Internal execution context for the router. Should not be modified by user code.
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
executionContext: {
|
|
90
|
+
handlerPathStack?: string[];
|
|
91
|
+
currentPath?: string;
|
|
92
|
+
callDepth?: number;
|
|
93
|
+
[key: string]: any;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* A unique ID for the top-level request, useful for logging and tracing.
|
|
97
|
+
*/
|
|
98
|
+
requestId: string;
|
|
99
|
+
/**
|
|
100
|
+
* A structured logger that automatically includes the `requestId` and current handler path.
|
|
101
|
+
*/
|
|
102
|
+
logger: AiLogger;
|
|
103
|
+
/**
|
|
104
|
+
* The stream writer to send data back to the end-user's UI.
|
|
105
|
+
* Includes helpers for writing structured data like tool calls and metadata.
|
|
106
|
+
*/
|
|
107
|
+
response: AiStreamWriter<Partial<METADATA>, PARTS, TOOLS>;
|
|
108
|
+
/**
|
|
109
|
+
* Provides functions for an agent to dispatch calls to other agents or tools.
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
next: NextHandler<METADATA, ContextState, PARAMS, PARTS, TOOLS>;
|
|
113
|
+
_onExecutionStart?: () => void;
|
|
114
|
+
_onExecutionEnd?: () => void;
|
|
115
|
+
};
|
|
116
|
+
/** Represents the `next` function in a middleware chain, used to pass control to the next handler. */
|
|
117
|
+
type NextFunction = () => Promise<any>;
|
|
118
|
+
/** A function that handles a request for a specific agent path. */
|
|
119
|
+
type AiHandler<METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = Record<string, any>, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = (ctx: AiContext<METADATA, ContextState, PARAMS, PARTS, TOOLS>) => Promise<any>;
|
|
120
|
+
/** A function that acts as middleware, processing a request and optionally passing control to the next handler. */
|
|
121
|
+
type AiMiddleware<METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = Record<string, any>, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = (ctx: AiContext<METADATA, ContextState, PARAMS, PARTS, TOOLS>, next: NextFunction) => Promise<any>;
|
|
122
|
+
/** A simple structured logger interface. */
|
|
123
|
+
type AiLogger = {
|
|
124
|
+
log: (...args: any[]) => void;
|
|
125
|
+
warn: (...args: any[]) => void;
|
|
126
|
+
error: (...args: any[]) => void;
|
|
127
|
+
};
|
|
128
|
+
/** Internal representation of a registered handler in the router's stack. */
|
|
129
|
+
type Layer<METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = Record<string, any>, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> = {
|
|
130
|
+
path: string | RegExp;
|
|
131
|
+
handler: AiMiddleware<METADATA, ContextState, PARAMS, PARTS, TOOLS>;
|
|
132
|
+
isAgent: boolean;
|
|
133
|
+
timing?: 'before' | 'after';
|
|
134
|
+
hasDynamicParams?: boolean;
|
|
135
|
+
paramNames?: string[];
|
|
136
|
+
};
|
|
137
|
+
type AgentTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = Tool<INPUT, OUTPUT> & {
|
|
138
|
+
name: string;
|
|
139
|
+
id: string;
|
|
140
|
+
metadata?: Record<string, any> & {
|
|
141
|
+
absolutePath?: string;
|
|
142
|
+
name?: string;
|
|
143
|
+
description?: string;
|
|
144
|
+
toolKey?: string;
|
|
145
|
+
icon?: string;
|
|
146
|
+
parentTitle?: string;
|
|
147
|
+
title?: string;
|
|
148
|
+
hideUI?: boolean;
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
type AgentData = {
|
|
152
|
+
metadata?: Record<string, any> & {
|
|
153
|
+
absolutePath?: string;
|
|
154
|
+
name?: string;
|
|
155
|
+
description?: string;
|
|
156
|
+
toolKey?: string;
|
|
157
|
+
icon?: string;
|
|
158
|
+
parentTitle?: string;
|
|
159
|
+
title?: string;
|
|
160
|
+
hideUI?: boolean;
|
|
161
|
+
};
|
|
162
|
+
[key: string]: any;
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* A composable router for building structured, multi-agent AI applications.
|
|
166
|
+
* It allows you to define agents and tools, compose them together, and handle
|
|
167
|
+
* requests in a predictable, middleware-style pattern.
|
|
168
|
+
*
|
|
169
|
+
* @template KIT_METADATA - The base metadata type for all UI messages in this router.
|
|
170
|
+
* @template PARTS - The base custom parts type for all UI messages.
|
|
171
|
+
* @template TOOLS - The base custom tools type for all UI messages.
|
|
172
|
+
* @template ContextState - The base type for the shared state object.
|
|
173
|
+
*/
|
|
174
|
+
declare class AiRouter<KIT_METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = {}, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools, REGISTERED_TOOLS extends ToolSet = {}> {
|
|
175
|
+
private stack;
|
|
176
|
+
actAsToolDefinitions: Map<string | RegExp, AgentTool<any, any>>;
|
|
177
|
+
private logger?;
|
|
178
|
+
private _store;
|
|
179
|
+
/** Configuration options for the router instance. */
|
|
180
|
+
options: {
|
|
181
|
+
/** The maximum number of agent-to-agent calls allowed in a single request to prevent infinite loops. */
|
|
182
|
+
maxCallDepth: number;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Constructs a new AiAgentKit router.
|
|
186
|
+
* @param stack An optional initial stack of layers, used for composing routers.
|
|
187
|
+
* @param options Optional configuration for the router.
|
|
188
|
+
*/
|
|
189
|
+
constructor(stack?: Layer<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS>[], options?: {
|
|
190
|
+
maxCallDepth?: number;
|
|
191
|
+
logger?: AiLogger;
|
|
192
|
+
});
|
|
193
|
+
setStore(store: Store): void;
|
|
194
|
+
/**
|
|
195
|
+
* Sets a logger for this router instance.
|
|
196
|
+
* If no logger is set, the router will fall back to the global logger.
|
|
197
|
+
* @param logger The logger to use for this router instance, or undefined to use global logger
|
|
198
|
+
*/
|
|
199
|
+
setLogger(logger?: AiLogger): void;
|
|
200
|
+
/**
|
|
201
|
+
* Gets the effective logger for this router instance.
|
|
202
|
+
* Returns instance logger if set, otherwise falls back to global logger.
|
|
203
|
+
* @returns The effective logger or undefined if no logging should occur
|
|
204
|
+
*/
|
|
205
|
+
private _getEffectiveLogger;
|
|
206
|
+
/**
|
|
207
|
+
* Registers a middleware-style agent that runs for a specific path prefix, regex pattern, or wildcard.
|
|
208
|
+
* Agents can modify the context and must call `next()` to pass control to the next handler in the chain.
|
|
209
|
+
* This method is primarily for middleware. For terminal agents, see `.agent()` on an instance.
|
|
210
|
+
*
|
|
211
|
+
* @param path The path prefix, regex pattern, or "*" for wildcard matching.
|
|
212
|
+
* @param agents The agent middleware function(s).
|
|
213
|
+
*/
|
|
214
|
+
agent<const TAgents extends (AiMiddleware<any, any, any, any, any> | AiRouter<any, any, any, any, any, any>)[]>(agentPath: string | RegExp | AiMiddleware<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS>, ...agents: TAgents): AiRouter<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS, REGISTERED_TOOLS & (TAgents[number] extends AiRouter<any, any, any, any, any, infer R> ? R : {})>;
|
|
215
|
+
/**
|
|
216
|
+
* Registers middleware that runs BEFORE agent execution for a specific path prefix, regex pattern, or wildcard.
|
|
217
|
+
* The middleware can modify the context and must call `next()` to pass control to the next handler.
|
|
218
|
+
*
|
|
219
|
+
* @param mountPathArg The path prefix, regex pattern, or "*" for wildcard matching.
|
|
220
|
+
* @param handler The middleware function or AiAgentKit router instance to mount.
|
|
221
|
+
*/
|
|
222
|
+
before<THandler extends AiMiddleware<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS> | AiRouter<any, any, any, any, any, any>>(mountPathArg: string | RegExp, handler: THandler): AiRouter<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS, REGISTERED_TOOLS & (THandler extends AiRouter<any, any, any, any, any, infer R> ? R : {})>;
|
|
223
|
+
/**
|
|
224
|
+
* Registers middleware that runs AFTER agent execution for a specific path prefix, regex pattern, or wildcard.
|
|
225
|
+
* The middleware can modify the context and must call `next()` to pass control to the next handler.
|
|
226
|
+
*
|
|
227
|
+
* @param mountPathArg The path prefix, regex pattern, or "*" for wildcard matching.
|
|
228
|
+
* @param handler The middleware function or AiAgentKit router instance to mount.
|
|
229
|
+
*/
|
|
230
|
+
after<THandler extends AiMiddleware<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS> | AiRouter<any, any, any, any, any, any>>(mountPathArg: string | RegExp, handler: THandler): AiRouter<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS, REGISTERED_TOOLS & (THandler extends AiRouter<any, any, any, any, any, infer R> ? R : {})>;
|
|
231
|
+
/**
|
|
232
|
+
* Pre-defines the schema and description for an agent when it is used as a tool by an LLM.
|
|
233
|
+
* This allows `next.agentAsTool()` to create a valid `Tool` object without needing the definition at call time.
|
|
234
|
+
* @param path The path of the agent being defined.
|
|
235
|
+
* @param options The tool definition, including a Zod schema and description.
|
|
236
|
+
*/
|
|
237
|
+
actAsTool<const TPath extends string | RegExp, const TTool extends AgentTool<z.infer<TTool['inputSchema']>, z.infer<TTool['outputSchema']>>>(path: TPath, options: TTool): AiRouter<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS, REGISTERED_TOOLS & {
|
|
238
|
+
[K in TTool['id']]: Tool<z.infer<TTool['inputSchema']>, z.infer<TTool['outputSchema']>> & {
|
|
239
|
+
metadata: TTool['metadata'] & {
|
|
240
|
+
toolKey: TTool['id'];
|
|
241
|
+
name: TTool['name'];
|
|
242
|
+
description: TTool['description'];
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
}>;
|
|
246
|
+
getToolSet(): REGISTERED_TOOLS;
|
|
247
|
+
getToolDefinition(path: string): AgentTool<any, any>;
|
|
248
|
+
/**
|
|
249
|
+
* Outputs all registered paths, and the middlewares and agents registered on each path.
|
|
250
|
+
* @returns A map of paths to their registered handlers.
|
|
251
|
+
*/
|
|
252
|
+
registry(): {
|
|
253
|
+
map: Record<string, {
|
|
254
|
+
before: any[];
|
|
255
|
+
agents: any[];
|
|
256
|
+
after: any[];
|
|
257
|
+
}>;
|
|
258
|
+
tools: REGISTERED_TOOLS;
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Resolves a path based on the parent path and the requested path.
|
|
262
|
+
* - If path starts with `@/`, it's an absolute path from the root.
|
|
263
|
+
* - Otherwise, it's a relative path.
|
|
264
|
+
* @internal
|
|
265
|
+
*/
|
|
266
|
+
private _resolvePath;
|
|
267
|
+
/**
|
|
268
|
+
* Creates a new context for an internal agent or tool call.
|
|
269
|
+
* It inherits from the parent context but gets a new logger and call depth.
|
|
270
|
+
* @internal
|
|
271
|
+
*/
|
|
272
|
+
private _createSubContext;
|
|
273
|
+
/**
|
|
274
|
+
* Creates a new logger instance with a structured prefix.
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
private _createLogger;
|
|
278
|
+
/**
|
|
279
|
+
* Calculates a specificity score for a layer to enable Express-style routing.
|
|
280
|
+
* Higher score means more specific.
|
|
281
|
+
* - Middleware is less specific than an agent/tool.
|
|
282
|
+
* - Deeper paths are more specific.
|
|
283
|
+
* - Static segments are more specific than dynamic segments.
|
|
284
|
+
* @internal
|
|
285
|
+
*/
|
|
286
|
+
private _getSpecificityScore;
|
|
287
|
+
/**
|
|
288
|
+
* The core execution engine. It finds all matching layers for a given path
|
|
289
|
+
* and runs them in a middleware-style chain.
|
|
290
|
+
* @internal
|
|
291
|
+
*/
|
|
292
|
+
private _execute;
|
|
293
|
+
private pendingExecutions;
|
|
294
|
+
/**
|
|
295
|
+
* The main public entry point for the router. It handles an incoming request,
|
|
296
|
+
* sets up the response stream, creates the root context, and starts the execution chain.
|
|
297
|
+
*
|
|
298
|
+
* @param path The path of the agent or tool to execute.
|
|
299
|
+
* @param initialContext The initial context for the request, typically containing messages.
|
|
300
|
+
* @returns A standard `Response` object containing the rich UI stream.
|
|
301
|
+
*/
|
|
302
|
+
handle(path: string, initialContext: Omit<AiContext<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS>, 'state' | 'response' | 'next' | 'requestId' | 'logger' | 'executionContext' | 'store'>): Response;
|
|
303
|
+
handleStream(path: string, initialContext: Omit<AiContext<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS>, 'state' | 'response' | 'next' | 'requestId' | 'logger' | 'executionContext' | 'store'>, executionCompletionPromise: Promise<void>, executionCompletionResolver: (() => void) | null): ReadableStream<ai.InferUIMessageChunk<UIMessage<KIT_METADATA, PARTS, TOOLS>>>;
|
|
304
|
+
/**
|
|
305
|
+
* Handles an incoming request and returns a promise that resolves with the full,
|
|
306
|
+
* non-streamed response. This is useful for environments where streaming is not
|
|
307
|
+
* desired or for testing.
|
|
308
|
+
*
|
|
309
|
+
* @param path The path of the agent or tool to execute.
|
|
310
|
+
* @param initialContext The initial context for the request, typically containing messages.
|
|
311
|
+
* @returns A `Promise<Response>` that resolves with the final JSON response.
|
|
312
|
+
*/
|
|
313
|
+
toAwaitResponse(path: string, initialContext: Omit<AiContext<KIT_METADATA, ContextState, PARAMS, PARTS, TOOLS>, 'state' | 'response' | 'next' | 'requestId' | 'logger' | 'executionContext' | 'store'>): Promise<Response>;
|
|
314
|
+
}
|
|
315
|
+
type AiRouterType = typeof AiRouter;
|
|
316
|
+
declare class NextHandler<METADATA extends Record<string, any> = Record<string, any>, ContextState extends Record<string, any> = Record<string, any>, PARAMS extends Record<string, any> = Record<string, any>, PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> {
|
|
317
|
+
private ctx;
|
|
318
|
+
private router;
|
|
319
|
+
private onExecutionStart;
|
|
320
|
+
private onExecutionEnd;
|
|
321
|
+
maxCallDepth: number;
|
|
322
|
+
constructor(ctx: AiContext<METADATA, ContextState, PARAMS, PARTS, TOOLS>, router: AiRouter<METADATA, ContextState, PARAMS, PARTS, TOOLS>, onExecutionStart: () => void, onExecutionEnd: () => void, parentNext?: NextHandler<METADATA, ContextState, PARAMS, PARTS, TOOLS>);
|
|
323
|
+
callAgent(agentPath: string, params?: Record<string, any>, options?: {
|
|
324
|
+
streamToUI?: boolean;
|
|
325
|
+
}): Promise<{
|
|
326
|
+
ok: true;
|
|
327
|
+
data: any;
|
|
328
|
+
} | {
|
|
329
|
+
ok: false;
|
|
330
|
+
error: Error;
|
|
331
|
+
}>;
|
|
332
|
+
agentAsTool<INPUT extends JSONValue | unknown | never = any, OUTPUT = any>(agentPath: string, schemaControl?: Record<string, any> & {
|
|
333
|
+
disableAllInputs?: boolean;
|
|
334
|
+
}): {
|
|
335
|
+
[x: string]: Tool<INPUT, OUTPUT>;
|
|
336
|
+
};
|
|
337
|
+
getToolDefinition(agentPath: string | RegExp): AgentTool<any, any> | undefined;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export { type AgentData, AgentDefinitionMissingError, AgentNotFoundError, type AgentTool, type AiContext, type AiHandler, AiKitError, type AiLogger, type AiMiddleware, AiRouter, type AiRouterType, type AiStreamWriter, MaxCallDepthExceededError, type NextFunction, Store, StreamWriter, type UITools, findFirstElement, findLastElement, findLastMessageWith, getGlobalLogger, getTextParts, getTextPartsContent, setGlobalLogger };
|