@appweaver/ai 0.0.0-stage → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ModelRegistry = void 0;
37
+ exports.registerAiDriver = registerAiDriver;
38
+ const common_1 = require("@appweaver/common");
39
+ const internal_1 = require("@appweaver/core/internal");
40
+ const config_1 = require("../config");
41
+ const presets_1 = require("./presets");
42
+ const logger = (0, common_1.createLogger)('ai');
43
+ // Matched by name, like the other contracts, since a module may load its own copy of the class
44
+ const AI_DRIVER_CONTRACT = 'AiDriver';
45
+ /** The packages of the preset drivers, named when one is not installed. */
46
+ const DRIVER_PACKAGES = {
47
+ openai: '@appweaver/openai'
48
+ };
49
+ const AI_DRIVERS = Symbol.for('appweaver.AiDrivers');
50
+ const registered = (globalThis[AI_DRIVERS] ??=
51
+ new Map());
52
+ /**
53
+ * Registers a driver class under a name without a module providing it, i.e. a stub in a test. It takes precedence
54
+ * over the driver of a module with the same name.
55
+ *
56
+ * @param {string} name - The name of the driver.
57
+ * @param {AiDriverClass | undefined} driver - The driver class, or `undefined` to remove it.
58
+ */
59
+ function registerAiDriver(name, driver) {
60
+ if (driver) {
61
+ registered.set(name, driver);
62
+ }
63
+ else {
64
+ registered.delete(name);
65
+ }
66
+ }
67
+ /** Resolves the model references and the aliases to their endpoints, and creates each endpoint's driver once. */
68
+ class ModelRegistry {
69
+ /** @internal */
70
+ _drivers = new Map();
71
+ /**
72
+ * Resolves a model reference or an alias into the models to call in turn, the default model when none is given.
73
+ *
74
+ * @param {string} [model] - A model reference (`local:qwen3:8b`), an alias (`fast`), or none.
75
+ * @param {string} [fallback] - The default reference or alias, i.e. `ai.defaultModel`.
76
+ * @return {ResolvedModel[]} The models, more than one for an alias listing fallbacks.
77
+ * @throws {AiError} `AI_MODEL_UNKNOWN` for an unknown alias or endpoint, or no model at all.
78
+ */
79
+ resolve(model, fallback = config_1.aiConfig.DEFAULT_MODEL) {
80
+ const name = model ?? fallback;
81
+ if (!name) {
82
+ throw new common_1.AiError(common_1.ErrorCode.AiModelUnknown, 'No model given and no default model configured (ai.defaultModel, AI_DEFAULT_MODEL)', { model: '' });
83
+ }
84
+ return this.references(name).map((reference) => this.parse(reference));
85
+ }
86
+ /**
87
+ * Returns the endpoint of a name, completed with the defaults of its preset and the `ai` config.
88
+ *
89
+ * @param {string} name - The name of the endpoint.
90
+ * @return {AiEndpoint} The endpoint.
91
+ * @throws {AiError} `AI_MODEL_UNKNOWN` for an endpoint that is not configured.
92
+ */
93
+ endpoint(name) {
94
+ const config = config_1.aiConfig.ENDPOINTS[name] ?? (name === 'mock' ? {} : undefined);
95
+ if (!config) {
96
+ throw new common_1.AiError(common_1.ErrorCode.AiModelUnknown, `No AI endpoint '${name}' configured, the endpoints are: ${[
97
+ 'mock',
98
+ ...Object.keys(config_1.aiConfig.ENDPOINTS)
99
+ ].join(', ')}`, { model: name });
100
+ }
101
+ const presetName = config.preset ?? (name === 'mock' ? 'mock' : undefined);
102
+ const preset = presetName ? presets_1.aiPresets[presetName] : undefined;
103
+ if (presetName && !preset) {
104
+ throw new common_1.ConfigurationError(common_1.ErrorCode.ConfigurationInvalid, `Unknown preset '${presetName}' of the AI endpoint '${name}', the presets are: ${Object.keys(presets_1.aiPresets).join(', ')}`);
105
+ }
106
+ const apiKeyVariable = preset?.apiKeyVariable;
107
+ return {
108
+ name,
109
+ driver: config.driver ?? preset?.driver ?? 'openai',
110
+ preset: presetName,
111
+ baseUrl: config.baseUrl ?? preset?.baseUrl,
112
+ apiKey: config.apiKey ??
113
+ (apiKeyVariable ? process.env[apiKeyVariable] || undefined : undefined),
114
+ headers: parseHeaders(config.headers ?? []),
115
+ capabilities: (config.capabilities ??
116
+ preset?.capabilities ??
117
+ presets_1.ALL_CAPABILITIES),
118
+ timeout: config.timeout ?? config_1.aiConfig.TIMEOUT,
119
+ maxRetries: config.maxRetries ?? config_1.aiConfig.MAX_RETRIES,
120
+ options: preset?.options ?? {}
121
+ };
122
+ }
123
+ /**
124
+ * Returns the driver of an endpoint, created on its first call.
125
+ *
126
+ * @param {string} name - The name of the endpoint.
127
+ * @return {Promise<AiDriver>} The driver.
128
+ * @throws {ConfigurationError} `CONFIGURATION_INVALID` when no enabled module provides its driver, or the endpoint
129
+ * requires an API key it has not.
130
+ */
131
+ driver(name) {
132
+ let driver = this._drivers.get(name);
133
+ if (!driver) {
134
+ driver = this.createDriver(name);
135
+ this._drivers.set(name, driver);
136
+ driver.catch(() => this._drivers.delete(name));
137
+ }
138
+ return driver;
139
+ }
140
+ /**
141
+ * Checks that the default models and every alias name configured endpoints.
142
+ *
143
+ * @return {string[]} The errors, empty when the config is valid.
144
+ */
145
+ validate() {
146
+ const errors = [];
147
+ const named = [
148
+ ...[config_1.aiConfig.DEFAULT_MODEL, config_1.aiConfig.DEFAULT_EMBEDDING_MODEL].filter((model) => !!model),
149
+ ...Object.keys(config_1.aiConfig.ALIASES)
150
+ ];
151
+ for (const name of named) {
152
+ try {
153
+ for (const { endpoint } of this.resolve(name)) {
154
+ this.endpoint(endpoint);
155
+ }
156
+ }
157
+ catch (error) {
158
+ errors.push(error.message);
159
+ }
160
+ }
161
+ return errors;
162
+ }
163
+ /** @internal */
164
+ references(name) {
165
+ if (name.includes(':')) {
166
+ return [name];
167
+ }
168
+ const alias = config_1.aiConfig.ALIASES[name];
169
+ if (alias === undefined) {
170
+ throw new common_1.AiError(common_1.ErrorCode.AiModelUnknown, `Unknown AI model '${name}': not a model reference (<endpoint>:<model>) nor an alias of ai.aliases`, { model: name });
171
+ }
172
+ const references = (Array.isArray(alias) ? alias : alias.split(','))
173
+ .map((reference) => reference.trim())
174
+ .filter(Boolean);
175
+ const invalid = references.find((reference) => !reference.includes(':'));
176
+ if (invalid !== undefined || !references.length) {
177
+ throw new common_1.AiError(common_1.ErrorCode.AiModelUnknown, `The AI alias '${name}' must list model references (<endpoint>:<model>), not '${invalid ?? ''}'`, { model: name });
178
+ }
179
+ return references;
180
+ }
181
+ /** @internal */
182
+ parse(reference) {
183
+ const separator = reference.indexOf(':');
184
+ const endpoint = reference.slice(0, separator);
185
+ const model = reference.slice(separator + 1);
186
+ if (!endpoint || !model) {
187
+ throw new common_1.AiError(common_1.ErrorCode.AiModelUnknown, `Invalid AI model reference '${reference}', expected <endpoint>:<model>`, { model: reference });
188
+ }
189
+ return { reference, endpoint, model };
190
+ }
191
+ /** @internal */
192
+ async createDriver(name) {
193
+ const endpoint = this.endpoint(name);
194
+ const preset = endpoint.preset ? presets_1.aiPresets[endpoint.preset] : undefined;
195
+ if (preset?.apiKeyRequired && !endpoint.apiKey) {
196
+ throw new common_1.ConfigurationError(common_1.ErrorCode.ConfigurationInvalid, `The AI endpoint '${name}' needs an API key: set ai.endpoints.${name}.apiKey (AI_ENDPOINTS_${(0, common_1.camelToSnakeCase)(name).toUpperCase()}_API_KEY)${preset.apiKeyVariable ? ` or ${preset.apiKeyVariable}` : ''}`);
197
+ }
198
+ const Driver = await findDriver(endpoint.driver);
199
+ if (!Driver) {
200
+ const pkg = DRIVER_PACKAGES[endpoint.driver];
201
+ throw new common_1.ConfigurationError(common_1.ErrorCode.ConfigurationInvalid, `No enabled module provides the AI driver '${endpoint.driver}' of the endpoint '${name}'${pkg ? `, install ${pkg}` : ''}`);
202
+ }
203
+ logger.debug({ endpoint: name, driver: endpoint.driver, baseUrl: endpoint.baseUrl }, 'AI endpoint connected');
204
+ return new Driver(endpoint);
205
+ }
206
+ }
207
+ exports.ModelRegistry = ModelRegistry;
208
+ /** Finds the class of a driver: a registered one, else the provider of an enabled module. */
209
+ async function findDriver(name) {
210
+ const driver = registered.get(name);
211
+ if (driver) {
212
+ return driver;
213
+ }
214
+ const provider = internal_1.context.modules.enabled
215
+ .flatMap((module) => module.providers ?? [])
216
+ .find((entry) => entry.contract.name === AI_DRIVER_CONTRACT && entry.name === name);
217
+ if (provider) {
218
+ return driverClass(await provider.load());
219
+ }
220
+ // The mock is built in, also without the module discovered, i.e. in a unit test
221
+ if (name === 'mock') {
222
+ return driverClass(await Promise.resolve().then(() => __importStar(require('./mock-driver'))));
223
+ }
224
+ return undefined;
225
+ }
226
+ /** The driver class a module exports, the first export with a `generate` method. */
227
+ function driverClass(exports) {
228
+ return Object.values(exports).find((value) => typeof value === 'function' &&
229
+ typeof value.prototype
230
+ ?.generate === 'function');
231
+ }
232
+ /** Parses the `Name: value` headers of an endpoint. */
233
+ function parseHeaders(headers) {
234
+ const parsed = {};
235
+ for (const header of headers) {
236
+ const separator = header.indexOf(':');
237
+ if (separator > 0) {
238
+ parsed[header.slice(0, separator).trim()] = header
239
+ .slice(separator + 1)
240
+ .trim();
241
+ }
242
+ }
243
+ return parsed;
244
+ }
@@ -0,0 +1,23 @@
1
+ import { AiCapability } from '../types';
2
+ /** The defaults an endpoint of the `ai.endpoints` config takes from its preset. */
3
+ export type AiPreset = {
4
+ /** The driver talking to the endpoint */
5
+ driver: string;
6
+ /** The base URL of the API */
7
+ baseUrl?: string;
8
+ /** The environment variable the API key is read from when the endpoint sets none */
9
+ apiKeyVariable?: string;
10
+ /** Whether the endpoint rejects a request without an API key, checked when its driver is created */
11
+ apiKeyRequired?: boolean;
12
+ /** The capabilities of its models, all of them when missing */
13
+ capabilities?: AiCapability[];
14
+ /** Options of the driver, i.e. the parameter of the maximum tokens */
15
+ options?: Record<string, unknown>;
16
+ };
17
+ /** Every capability, the default of an endpoint without a preset limiting them. */
18
+ export declare const ALL_CAPABILITIES: AiCapability[];
19
+ /**
20
+ * The presets of the endpoints, by name. Every OpenAI-compatible server is reached by the `openai` driver of
21
+ * `@appweaver/openai`, an unknown server by setting its `baseUrl` without a preset.
22
+ */
23
+ export declare const aiPresets: Readonly<Record<string, AiPreset>>;
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.aiPresets = exports.ALL_CAPABILITIES = void 0;
4
+ /** Every capability, the default of an endpoint without a preset limiting them. */
5
+ exports.ALL_CAPABILITIES = [
6
+ 'tools',
7
+ 'json',
8
+ 'json-schema',
9
+ 'images',
10
+ 'files',
11
+ 'reasoning',
12
+ 'embeddings'
13
+ ];
14
+ /** The capabilities of the local servers, which read no files. */
15
+ const LOCAL_CAPABILITIES = [
16
+ 'tools',
17
+ 'json',
18
+ 'json-schema',
19
+ 'images',
20
+ 'reasoning',
21
+ 'embeddings'
22
+ ];
23
+ /**
24
+ * The presets of the endpoints, by name. Every OpenAI-compatible server is reached by the `openai` driver of
25
+ * `@appweaver/openai`, an unknown server by setting its `baseUrl` without a preset.
26
+ */
27
+ exports.aiPresets = Object.freeze({
28
+ openai: {
29
+ driver: 'openai',
30
+ baseUrl: 'https://api.openai.com/v1',
31
+ apiKeyVariable: 'OPENAI_API_KEY',
32
+ apiKeyRequired: true,
33
+ options: { maxTokensParam: 'max_completion_tokens' }
34
+ },
35
+ openrouter: {
36
+ driver: 'openai',
37
+ baseUrl: 'https://openrouter.ai/api/v1',
38
+ apiKeyVariable: 'OPENROUTER_API_KEY',
39
+ apiKeyRequired: true,
40
+ options: { reasoningParam: 'reasoning' }
41
+ },
42
+ ollama: {
43
+ driver: 'openai',
44
+ baseUrl: 'http://localhost:11434/v1',
45
+ apiKeyVariable: 'OLLAMA_API_KEY',
46
+ capabilities: LOCAL_CAPABILITIES
47
+ },
48
+ lmstudio: {
49
+ driver: 'openai',
50
+ baseUrl: 'http://localhost:1234/v1',
51
+ capabilities: LOCAL_CAPABILITIES
52
+ },
53
+ vllm: {
54
+ driver: 'openai',
55
+ baseUrl: 'http://localhost:8000/v1',
56
+ apiKeyVariable: 'VLLM_API_KEY',
57
+ capabilities: LOCAL_CAPABILITIES
58
+ },
59
+ llamacpp: {
60
+ driver: 'openai',
61
+ baseUrl: 'http://localhost:8080/v1',
62
+ capabilities: LOCAL_CAPABILITIES
63
+ },
64
+ litellm: {
65
+ driver: 'openai',
66
+ baseUrl: 'http://localhost:4000/v1',
67
+ apiKeyVariable: 'LITELLM_API_KEY'
68
+ },
69
+ groq: {
70
+ driver: 'openai',
71
+ baseUrl: 'https://api.groq.com/openai/v1',
72
+ apiKeyVariable: 'GROQ_API_KEY',
73
+ apiKeyRequired: true,
74
+ capabilities: ['tools', 'json', 'json-schema', 'images', 'reasoning']
75
+ },
76
+ together: {
77
+ driver: 'openai',
78
+ baseUrl: 'https://api.together.xyz/v1',
79
+ apiKeyVariable: 'TOGETHER_API_KEY',
80
+ apiKeyRequired: true,
81
+ capabilities: LOCAL_CAPABILITIES
82
+ },
83
+ fireworks: {
84
+ driver: 'openai',
85
+ baseUrl: 'https://api.fireworks.ai/inference/v1',
86
+ apiKeyVariable: 'FIREWORKS_API_KEY',
87
+ apiKeyRequired: true,
88
+ capabilities: LOCAL_CAPABILITIES
89
+ },
90
+ deepseek: {
91
+ driver: 'openai',
92
+ baseUrl: 'https://api.deepseek.com/v1',
93
+ apiKeyVariable: 'DEEPSEEK_API_KEY',
94
+ apiKeyRequired: true,
95
+ capabilities: ['tools', 'json', 'reasoning']
96
+ },
97
+ mistral: {
98
+ driver: 'openai',
99
+ baseUrl: 'https://api.mistral.ai/v1',
100
+ apiKeyVariable: 'MISTRAL_API_KEY',
101
+ apiKeyRequired: true,
102
+ capabilities: LOCAL_CAPABILITIES
103
+ },
104
+ xai: {
105
+ driver: 'openai',
106
+ baseUrl: 'https://api.x.ai/v1',
107
+ apiKeyVariable: 'XAI_API_KEY',
108
+ apiKeyRequired: true,
109
+ capabilities: ['tools', 'json', 'json-schema', 'images', 'reasoning']
110
+ },
111
+ gemini: {
112
+ driver: 'openai',
113
+ baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
114
+ apiKeyVariable: 'GEMINI_API_KEY',
115
+ apiKeyRequired: true,
116
+ capabilities: LOCAL_CAPABILITIES
117
+ },
118
+ huggingface: {
119
+ driver: 'openai',
120
+ baseUrl: 'https://router.huggingface.co/v1',
121
+ apiKeyVariable: 'HF_TOKEN',
122
+ apiKeyRequired: true,
123
+ capabilities: ['tools', 'json', 'json-schema', 'images', 'reasoning']
124
+ },
125
+ mock: { driver: 'mock' }
126
+ });
package/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export * from './config';
2
+ export * from './driver/ai-driver';
3
+ export * from './driver/mock-driver';
4
+ export * from './driver/model-registry';
5
+ export * from './driver/presets';
6
+ export * from './service/ai-object-stream';
7
+ export * from './service/ai-service';
8
+ export * from './service/ai-stream';
9
+ export * from './service/prompt';
10
+ export * from './tools/create-tool';
11
+ export * from './types';
12
+ export * from './utils/json-util';
13
+ export * from './utils/stream-util';
14
+ export * from './utils/vector-util';
package/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./config"), exports);
18
+ __exportStar(require("./driver/ai-driver"), exports);
19
+ __exportStar(require("./driver/mock-driver"), exports);
20
+ __exportStar(require("./driver/model-registry"), exports);
21
+ __exportStar(require("./driver/presets"), exports);
22
+ __exportStar(require("./service/ai-object-stream"), exports);
23
+ __exportStar(require("./service/ai-service"), exports);
24
+ __exportStar(require("./service/ai-stream"), exports);
25
+ __exportStar(require("./service/prompt"), exports);
26
+ __exportStar(require("./tools/create-tool"), exports);
27
+ __exportStar(require("./types"), exports);
28
+ __exportStar(require("./utils/json-util"), exports);
29
+ __exportStar(require("./utils/stream-util"), exports);
30
+ __exportStar(require("./utils/vector-util"), exports);
package/module.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The calls of the AI models: the endpoints of the `ai` config, `AiService`, the tools and the built-in `mock`
3
+ * driver. The drivers of the endpoints are the `AiDriver` providers of the driver modules, i.e. `openai` of
4
+ * `@appweaver/openai`, all of them usable at once.
5
+ */
6
+ declare const _default: import("@appweaver/common").Module;
7
+ export default _default;
package/module.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const common_1 = require("@appweaver/common");
37
+ const config_1 = require("./config");
38
+ /**
39
+ * The calls of the AI models: the endpoints of the `ai` config, `AiService`, the tools and the built-in `mock`
40
+ * driver. The drivers of the endpoints are the `AiDriver` providers of the driver modules, i.e. `openai` of
41
+ * `@appweaver/openai`, all of them usable at once.
42
+ */
43
+ exports.default = (0, common_1.defineModule)({
44
+ name: '@appweaver/ai',
45
+ config: [config_1.aiConfig],
46
+ loggers: ['ai'],
47
+ services: [{ load: () => Promise.resolve().then(() => __importStar(require('./service/ai-service'))) }]
48
+ });
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@appweaver/ai",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.8.0",
4
+ "description": "Appweaver - the backend framework for AI-first development (@ai)",
5
+ "author": "Luka Matosevic",
6
+ "license": "MIT",
7
+ "main": "./index.js",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "default": "./index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "keywords": [
17
+ "appweaver",
18
+ "appweaver-module",
19
+ "ai",
20
+ "llm",
21
+ "openai",
22
+ "ollama",
23
+ "openrouter"
24
+ ],
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/lmatosevic/appweaver.git",
28
+ "directory": "packages/ai"
29
+ },
30
+ "maintainers": [
31
+ {
32
+ "name": "Luka Matosevic",
33
+ "email": "lukamatosevic5@gmail.com",
34
+ "web": "https://lukamatosevic.com"
35
+ }
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "engines": {
41
+ "node": ">= 24.7"
42
+ },
43
+ "appweaver": {
44
+ "module": "./module.js"
45
+ },
46
+ "dependencies": {
47
+ "@sinclair/typebox": "0.34.49"
48
+ },
49
+ "peerDependencies": {
50
+ "@appweaver/common": "^2.8.0",
51
+ "@appweaver/core": "^2.8.0"
52
+ },
53
+ "devDependencies": {
54
+ "@appweaver/common": "^2.8.0",
55
+ "@appweaver/core": "^2.8.0"
56
+ }
6
57
  }
@@ -0,0 +1,35 @@
1
+ import { Static, TSchema } from '@sinclair/typebox';
2
+ import { AiStream } from './ai-stream';
3
+ import { AiObjectResult } from './prompt';
4
+ /** A value with every property optional, at every depth: an object while it is streamed. */
5
+ export type DeepPartial<T> = T extends (infer U)[] ? DeepPartial<U>[] : T extends object ? {
6
+ [K in keyof T]?: DeepPartial<T[K]>;
7
+ } : T;
8
+ /**
9
+ * A streamed object of a model: an async iterable of the object parsed so far, each one yielded once it changes, with
10
+ * the object once it ends, checked against its schema.
11
+ *
12
+ * @example
13
+ * const stream = ai.streamObject({ prompt: 'List three cities', schema: Cities });
14
+ * for await (const partial of stream) {
15
+ * render(partial.cities ?? []);
16
+ * }
17
+ * const cities = await stream.object;
18
+ */
19
+ export declare class AiObjectStream<T extends TSchema> implements AsyncIterable<DeepPartial<Static<T>>> {
20
+ private readonly stream;
21
+ constructor(stream: AiStream<AiObjectResult<T>>);
22
+ /** The result once the stream ends, with the object. */
23
+ get result(): Promise<AiObjectResult<T>>;
24
+ /** The object once the stream ends, matching the schema. */
25
+ get object(): Promise<Static<T>>;
26
+ /** Aborted once the stream is aborted. */
27
+ get signal(): AbortSignal;
28
+ /**
29
+ * Aborts the stream.
30
+ *
31
+ * @param {unknown} [reason] - The reason of the abort.
32
+ */
33
+ abort(reason?: unknown): void;
34
+ [Symbol.asyncIterator](): AsyncIterator<DeepPartial<Static<T>>>;
35
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AiObjectStream = void 0;
4
+ const json_util_1 = require("../utils/json-util");
5
+ /**
6
+ * A streamed object of a model: an async iterable of the object parsed so far, each one yielded once it changes, with
7
+ * the object once it ends, checked against its schema.
8
+ *
9
+ * @example
10
+ * const stream = ai.streamObject({ prompt: 'List three cities', schema: Cities });
11
+ * for await (const partial of stream) {
12
+ * render(partial.cities ?? []);
13
+ * }
14
+ * const cities = await stream.object;
15
+ */
16
+ class AiObjectStream {
17
+ stream;
18
+ constructor(stream) {
19
+ this.stream = stream;
20
+ }
21
+ /** The result once the stream ends, with the object. */
22
+ get result() {
23
+ return this.stream.result;
24
+ }
25
+ /** The object once the stream ends, matching the schema. */
26
+ get object() {
27
+ return this.stream.result.then((result) => result.object);
28
+ }
29
+ /** Aborted once the stream is aborted. */
30
+ get signal() {
31
+ return this.stream.signal;
32
+ }
33
+ /**
34
+ * Aborts the stream.
35
+ *
36
+ * @param {unknown} [reason] - The reason of the abort.
37
+ */
38
+ abort(reason) {
39
+ this.stream.abort(reason);
40
+ }
41
+ async *[Symbol.asyncIterator]() {
42
+ let text = '';
43
+ let last;
44
+ for await (const part of this.stream.parts()) {
45
+ if (part.type !== 'text-delta') {
46
+ continue;
47
+ }
48
+ text += part.text;
49
+ const value = (0, json_util_1.parsePartialJson)(text);
50
+ const key = JSON.stringify(value);
51
+ if (value !== undefined && key !== last) {
52
+ last = key;
53
+ yield value;
54
+ }
55
+ }
56
+ }
57
+ }
58
+ exports.AiObjectStream = AiObjectStream;