@framers/agentos 0.1.69 → 0.1.70
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/api/agent.d.ts +98 -2
- package/dist/api/agent.d.ts.map +1 -1
- package/dist/api/agent.js +64 -9
- package/dist/api/agent.js.map +1 -1
- package/dist/api/generateImage.d.ts +109 -0
- package/dist/api/generateImage.d.ts.map +1 -0
- package/dist/api/generateImage.js +41 -0
- package/dist/api/generateImage.js.map +1 -0
- package/dist/api/generateText.d.ts +73 -3
- package/dist/api/generateText.d.ts.map +1 -1
- package/dist/api/generateText.js +29 -4
- package/dist/api/generateText.js.map +1 -1
- package/dist/api/model.d.ts +74 -4
- package/dist/api/model.d.ts.map +1 -1
- package/dist/api/model.js +79 -5
- package/dist/api/model.js.map +1 -1
- package/dist/api/streamText.d.ts +37 -1
- package/dist/api/streamText.d.ts.map +1 -1
- package/dist/api/streamText.js +188 -49
- package/dist/api/streamText.js.map +1 -1
- package/dist/api/toolAdapter.d.ts +58 -0
- package/dist/api/toolAdapter.d.ts.map +1 -0
- package/dist/api/{tool-adapter.js → toolAdapter.js} +24 -2
- package/dist/api/toolAdapter.js.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/orchestration/builders/AgentGraph.d.ts +250 -0
- package/dist/orchestration/builders/AgentGraph.d.ts.map +1 -0
- package/dist/orchestration/builders/AgentGraph.js +338 -0
- package/dist/orchestration/builders/AgentGraph.js.map +1 -0
- package/dist/orchestration/builders/MissionBuilder.d.ts +231 -0
- package/dist/orchestration/builders/MissionBuilder.d.ts.map +1 -0
- package/dist/orchestration/builders/MissionBuilder.js +321 -0
- package/dist/orchestration/builders/MissionBuilder.js.map +1 -0
- package/dist/orchestration/builders/WorkflowBuilder.d.ts +265 -0
- package/dist/orchestration/builders/WorkflowBuilder.d.ts.map +1 -0
- package/dist/orchestration/builders/WorkflowBuilder.js +472 -0
- package/dist/orchestration/builders/WorkflowBuilder.js.map +1 -0
- package/dist/orchestration/builders/index.d.ts +7 -0
- package/dist/orchestration/builders/index.d.ts.map +1 -0
- package/dist/orchestration/builders/index.js +5 -0
- package/dist/orchestration/builders/index.js.map +1 -0
- package/dist/orchestration/builders/nodes.d.ts +36 -0
- package/dist/orchestration/builders/nodes.d.ts.map +1 -0
- package/dist/orchestration/builders/nodes.js +98 -0
- package/dist/orchestration/builders/nodes.js.map +1 -0
- package/dist/orchestration/checkpoint/ICheckpointStore.d.ts +152 -0
- package/dist/orchestration/checkpoint/ICheckpointStore.d.ts.map +1 -0
- package/dist/orchestration/checkpoint/ICheckpointStore.js +8 -0
- package/dist/orchestration/checkpoint/ICheckpointStore.js.map +1 -0
- package/dist/orchestration/checkpoint/InMemoryCheckpointStore.d.ts +89 -0
- package/dist/orchestration/checkpoint/InMemoryCheckpointStore.d.ts.map +1 -0
- package/dist/orchestration/checkpoint/InMemoryCheckpointStore.js +156 -0
- package/dist/orchestration/checkpoint/InMemoryCheckpointStore.js.map +1 -0
- package/dist/orchestration/checkpoint/index.d.ts +10 -0
- package/dist/orchestration/checkpoint/index.d.ts.map +1 -0
- package/dist/orchestration/checkpoint/index.js +10 -0
- package/dist/orchestration/checkpoint/index.js.map +1 -0
- package/dist/orchestration/compiler/GraphCompiler.d.ts +85 -0
- package/dist/orchestration/compiler/GraphCompiler.d.ts.map +1 -0
- package/dist/orchestration/compiler/GraphCompiler.js +71 -0
- package/dist/orchestration/compiler/GraphCompiler.js.map +1 -0
- package/dist/orchestration/compiler/MissionCompiler.d.ts +181 -0
- package/dist/orchestration/compiler/MissionCompiler.d.ts.map +1 -0
- package/dist/orchestration/compiler/MissionCompiler.js +219 -0
- package/dist/orchestration/compiler/MissionCompiler.js.map +1 -0
- package/dist/orchestration/compiler/SchemaLowering.d.ts +37 -0
- package/dist/orchestration/compiler/SchemaLowering.d.ts.map +1 -0
- package/dist/orchestration/compiler/SchemaLowering.js +95 -0
- package/dist/orchestration/compiler/SchemaLowering.js.map +1 -0
- package/dist/orchestration/compiler/Validator.d.ts +61 -0
- package/dist/orchestration/compiler/Validator.d.ts.map +1 -0
- package/dist/orchestration/compiler/Validator.js +96 -0
- package/dist/orchestration/compiler/Validator.js.map +1 -0
- package/dist/orchestration/compiler/index.d.ts +16 -0
- package/dist/orchestration/compiler/index.d.ts.map +1 -0
- package/dist/orchestration/compiler/index.js +13 -0
- package/dist/orchestration/compiler/index.js.map +1 -0
- package/dist/orchestration/events/GraphEvent.d.ts +233 -0
- package/dist/orchestration/events/GraphEvent.d.ts.map +1 -0
- package/dist/orchestration/events/GraphEvent.js +203 -0
- package/dist/orchestration/events/GraphEvent.js.map +1 -0
- package/dist/orchestration/events/index.d.ts +2 -0
- package/dist/orchestration/events/index.d.ts.map +1 -0
- package/dist/orchestration/events/index.js +2 -0
- package/dist/orchestration/events/index.js.map +1 -0
- package/dist/orchestration/index.d.ts +20 -0
- package/dist/orchestration/index.d.ts.map +1 -0
- package/dist/orchestration/index.js +26 -0
- package/dist/orchestration/index.js.map +1 -0
- package/dist/orchestration/ir/index.d.ts +2 -0
- package/dist/orchestration/ir/index.d.ts.map +1 -0
- package/dist/orchestration/ir/index.js +2 -0
- package/dist/orchestration/ir/index.js.map +1 -0
- package/dist/orchestration/ir/types.d.ts +540 -0
- package/dist/orchestration/ir/types.d.ts.map +1 -0
- package/dist/orchestration/ir/types.js +20 -0
- package/dist/orchestration/ir/types.js.map +1 -0
- package/dist/orchestration/runtime/GraphRuntime.d.ts +135 -0
- package/dist/orchestration/runtime/GraphRuntime.d.ts.map +1 -0
- package/dist/orchestration/runtime/GraphRuntime.js +381 -0
- package/dist/orchestration/runtime/GraphRuntime.js.map +1 -0
- package/dist/orchestration/runtime/LoopController.d.ts +173 -0
- package/dist/orchestration/runtime/LoopController.d.ts.map +1 -0
- package/dist/orchestration/runtime/LoopController.js +130 -0
- package/dist/orchestration/runtime/LoopController.js.map +1 -0
- package/dist/orchestration/runtime/NodeExecutor.d.ts +206 -0
- package/dist/orchestration/runtime/NodeExecutor.d.ts.map +1 -0
- package/dist/orchestration/runtime/NodeExecutor.js +227 -0
- package/dist/orchestration/runtime/NodeExecutor.js.map +1 -0
- package/dist/orchestration/runtime/NodeScheduler.d.ts +85 -0
- package/dist/orchestration/runtime/NodeScheduler.d.ts.map +1 -0
- package/dist/orchestration/runtime/NodeScheduler.js +180 -0
- package/dist/orchestration/runtime/NodeScheduler.js.map +1 -0
- package/dist/orchestration/runtime/StateManager.d.ts +122 -0
- package/dist/orchestration/runtime/StateManager.d.ts.map +1 -0
- package/dist/orchestration/runtime/StateManager.js +243 -0
- package/dist/orchestration/runtime/StateManager.js.map +1 -0
- package/dist/orchestration/runtime/index.d.ts +16 -0
- package/dist/orchestration/runtime/index.d.ts.map +1 -0
- package/dist/orchestration/runtime/index.js +13 -0
- package/dist/orchestration/runtime/index.js.map +1 -0
- package/package.json +10 -4
- package/dist/api/tool-adapter.d.ts +0 -12
- package/dist/api/tool-adapter.d.ts.map +0 -1
- package/dist/api/tool-adapter.js.map +0 -1
package/dist/api/model.d.ts
CHANGED
|
@@ -1,28 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file model.ts
|
|
3
|
+
* Provider resolution utilities for the AgentOS high-level API.
|
|
4
|
+
*
|
|
5
|
+
* This module is responsible for parsing `provider:model` strings, resolving
|
|
6
|
+
* credentials from environment variables or caller overrides, and constructing
|
|
7
|
+
* an {@link AIModelProviderManager} ready for use by {@link generateText} and
|
|
8
|
+
* {@link streamText}.
|
|
9
|
+
*/
|
|
1
10
|
import { AIModelProviderManager } from '../core/llm/providers/AIModelProviderManager.js';
|
|
11
|
+
/**
|
|
12
|
+
* The result of splitting a `provider:model` string at the first colon.
|
|
13
|
+
* Produced by {@link parseModelString}.
|
|
14
|
+
*/
|
|
2
15
|
export interface ParsedModel {
|
|
16
|
+
/** The provider identifier (e.g. `"openai"`, `"anthropic"`, `"ollama"`). */
|
|
3
17
|
providerId: string;
|
|
18
|
+
/** The model identifier within the provider (e.g. `"gpt-4o"`, `"llama3.2"`). */
|
|
4
19
|
modelId: string;
|
|
5
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* A fully resolved provider configuration including optional credentials.
|
|
23
|
+
* Produced by {@link resolveProvider} and {@link resolveMediaProvider}.
|
|
24
|
+
*/
|
|
6
25
|
export interface ResolvedProvider {
|
|
26
|
+
/** Canonical provider identifier after any fallback remapping (e.g. anthropic → openrouter). */
|
|
7
27
|
providerId: string;
|
|
28
|
+
/** Model identifier, potentially rewritten for the remapped provider. */
|
|
8
29
|
modelId: string;
|
|
30
|
+
/** API key to use. Absent for providers that rely solely on a base URL (e.g. Ollama). */
|
|
9
31
|
apiKey?: string;
|
|
32
|
+
/** Base URL override forwarded to the provider SDK. */
|
|
10
33
|
baseUrl?: string;
|
|
11
34
|
}
|
|
12
35
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
36
|
+
* Splits a `provider:model` string into its constituent parts.
|
|
37
|
+
*
|
|
38
|
+
* The format is strict: the provider portion must be non-empty, separated from
|
|
39
|
+
* the model portion by exactly one colon, and the model portion must also be
|
|
40
|
+
* non-empty.
|
|
41
|
+
*
|
|
42
|
+
* @param model - A `provider:model` string such as `"openai:gpt-4o"`,
|
|
43
|
+
* `"ollama:llama3.2"`, or `"openrouter:anthropic/claude-sonnet-4-5-20250929"`.
|
|
44
|
+
* @returns A {@link ParsedModel} with `providerId` and `modelId` fields.
|
|
45
|
+
* @throws {Error} When the string is missing, not a string, or does not match
|
|
46
|
+
* the expected `provider:model` format.
|
|
15
47
|
*/
|
|
16
48
|
export declare function parseModelString(model: string): ParsedModel;
|
|
17
49
|
/**
|
|
18
|
-
* Resolves
|
|
50
|
+
* Resolves a complete provider configuration for LLM text providers.
|
|
51
|
+
*
|
|
52
|
+
* Reads API keys and base URLs from well-known environment variables
|
|
53
|
+
* (e.g. `OPENAI_API_KEY`, `OLLAMA_BASE_URL`) and merges caller-supplied
|
|
54
|
+
* `overrides`. Applies the Anthropic → OpenRouter fallback when
|
|
55
|
+
* `ANTHROPIC_API_KEY` is absent but `OPENROUTER_API_KEY` is set.
|
|
56
|
+
*
|
|
57
|
+
* @param providerId - Provider identifier (e.g. `"openai"`, `"anthropic"`, `"ollama"`).
|
|
58
|
+
* @param modelId - Model identifier within the provider.
|
|
59
|
+
* @param overrides - Optional explicit API key and/or base URL that take precedence
|
|
60
|
+
* over environment variable lookups.
|
|
61
|
+
* @returns A {@link ResolvedProvider} ready for {@link createProviderManager}.
|
|
62
|
+
* @throws {Error} When no credentials can be resolved for the given provider.
|
|
19
63
|
*/
|
|
20
64
|
export declare function resolveProvider(providerId: string, modelId: string, overrides?: {
|
|
21
65
|
apiKey?: string;
|
|
22
66
|
baseUrl?: string;
|
|
23
67
|
}): ResolvedProvider;
|
|
24
68
|
/**
|
|
25
|
-
*
|
|
69
|
+
* Resolves a provider configuration for image and other media providers.
|
|
70
|
+
*
|
|
71
|
+
* Behaves like {@link resolveProvider} but relaxes the API-key requirement:
|
|
72
|
+
* when the provider is not listed in the known key map, the call succeeds
|
|
73
|
+
* without a key (allowing custom or keyless providers). Ollama still
|
|
74
|
+
* requires a `baseUrl`.
|
|
75
|
+
*
|
|
76
|
+
* @param providerId - Provider identifier (e.g. `"stability"`, `"replicate"`, `"ollama"`).
|
|
77
|
+
* @param modelId - Model identifier within the provider.
|
|
78
|
+
* @param overrides - Optional explicit API key and/or base URL overrides.
|
|
79
|
+
* @returns A {@link ResolvedProvider} ready for use with an image provider factory.
|
|
80
|
+
* @throws {Error} When a known provider is missing its required API key or base URL.
|
|
81
|
+
*/
|
|
82
|
+
export declare function resolveMediaProvider(providerId: string, modelId: string, overrides?: {
|
|
83
|
+
apiKey?: string;
|
|
84
|
+
baseUrl?: string;
|
|
85
|
+
}): ResolvedProvider;
|
|
86
|
+
/**
|
|
87
|
+
* Instantiates and initialises an {@link AIModelProviderManager} for a single provider.
|
|
88
|
+
*
|
|
89
|
+
* Constructs the provider config object from the `resolved` credentials and calls
|
|
90
|
+
* `manager.initialize()` before returning. The returned manager is ready for
|
|
91
|
+
* immediate use via `manager.getProvider(providerId)`.
|
|
92
|
+
*
|
|
93
|
+
* @param resolved - A {@link ResolvedProvider} produced by {@link resolveProvider}
|
|
94
|
+
* or {@link resolveMediaProvider}.
|
|
95
|
+
* @returns A fully initialised {@link AIModelProviderManager} instance.
|
|
26
96
|
*/
|
|
27
97
|
export declare function createProviderManager(resolved: ResolvedProvider): Promise<AIModelProviderManager>;
|
|
28
98
|
//# sourceMappingURL=model.d.ts.map
|
package/dist/api/model.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/api/model.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/api/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AAEzF;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,4EAA4E;IAC5E,UAAU,EAAE,MAAM,CAAC;IACnB,gFAAgF;IAChF,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gGAAgG;IAChG,UAAU,EAAE,MAAM,CAAC;IACnB,yEAAyE;IACzE,OAAO,EAAE,MAAM,CAAC;IAChB,yFAAyF;IACzF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAmBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAY3D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAChD,gBAAgB,CA4BlB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,SAAS,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAChD,gBAAgB,CAmBlB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,qBAAqB,CACzC,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,sBAAsB,CAAC,CAoBjC"}
|
package/dist/api/model.js
CHANGED
|
@@ -1,17 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file model.ts
|
|
3
|
+
* Provider resolution utilities for the AgentOS high-level API.
|
|
4
|
+
*
|
|
5
|
+
* This module is responsible for parsing `provider:model` strings, resolving
|
|
6
|
+
* credentials from environment variables or caller overrides, and constructing
|
|
7
|
+
* an {@link AIModelProviderManager} ready for use by {@link generateText} and
|
|
8
|
+
* {@link streamText}.
|
|
9
|
+
*/
|
|
2
10
|
import { AIModelProviderManager } from '../core/llm/providers/AIModelProviderManager.js';
|
|
3
11
|
const ENV_KEY_MAP = {
|
|
4
12
|
openai: 'OPENAI_API_KEY',
|
|
5
13
|
anthropic: 'ANTHROPIC_API_KEY',
|
|
6
14
|
openrouter: 'OPENROUTER_API_KEY',
|
|
7
15
|
gemini: 'GEMINI_API_KEY',
|
|
16
|
+
stability: 'STABILITY_API_KEY',
|
|
17
|
+
replicate: 'REPLICATE_API_TOKEN',
|
|
8
18
|
};
|
|
9
19
|
const ENV_URL_MAP = {
|
|
20
|
+
openai: 'OPENAI_BASE_URL',
|
|
21
|
+
openrouter: 'OPENROUTER_BASE_URL',
|
|
22
|
+
stability: 'STABILITY_BASE_URL',
|
|
23
|
+
replicate: 'REPLICATE_BASE_URL',
|
|
10
24
|
ollama: 'OLLAMA_BASE_URL',
|
|
11
25
|
};
|
|
12
26
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
27
|
+
* Splits a `provider:model` string into its constituent parts.
|
|
28
|
+
*
|
|
29
|
+
* The format is strict: the provider portion must be non-empty, separated from
|
|
30
|
+
* the model portion by exactly one colon, and the model portion must also be
|
|
31
|
+
* non-empty.
|
|
32
|
+
*
|
|
33
|
+
* @param model - A `provider:model` string such as `"openai:gpt-4o"`,
|
|
34
|
+
* `"ollama:llama3.2"`, or `"openrouter:anthropic/claude-sonnet-4-5-20250929"`.
|
|
35
|
+
* @returns A {@link ParsedModel} with `providerId` and `modelId` fields.
|
|
36
|
+
* @throws {Error} When the string is missing, not a string, or does not match
|
|
37
|
+
* the expected `provider:model` format.
|
|
15
38
|
*/
|
|
16
39
|
export function parseModelString(model) {
|
|
17
40
|
if (!model || typeof model !== 'string') {
|
|
@@ -27,7 +50,19 @@ export function parseModelString(model) {
|
|
|
27
50
|
};
|
|
28
51
|
}
|
|
29
52
|
/**
|
|
30
|
-
* Resolves
|
|
53
|
+
* Resolves a complete provider configuration for LLM text providers.
|
|
54
|
+
*
|
|
55
|
+
* Reads API keys and base URLs from well-known environment variables
|
|
56
|
+
* (e.g. `OPENAI_API_KEY`, `OLLAMA_BASE_URL`) and merges caller-supplied
|
|
57
|
+
* `overrides`. Applies the Anthropic → OpenRouter fallback when
|
|
58
|
+
* `ANTHROPIC_API_KEY` is absent but `OPENROUTER_API_KEY` is set.
|
|
59
|
+
*
|
|
60
|
+
* @param providerId - Provider identifier (e.g. `"openai"`, `"anthropic"`, `"ollama"`).
|
|
61
|
+
* @param modelId - Model identifier within the provider.
|
|
62
|
+
* @param overrides - Optional explicit API key and/or base URL that take precedence
|
|
63
|
+
* over environment variable lookups.
|
|
64
|
+
* @returns A {@link ResolvedProvider} ready for {@link createProviderManager}.
|
|
65
|
+
* @throws {Error} When no credentials can be resolved for the given provider.
|
|
31
66
|
*/
|
|
32
67
|
export function resolveProvider(providerId, modelId, overrides) {
|
|
33
68
|
const apiKey = overrides?.apiKey
|
|
@@ -55,7 +90,46 @@ export function resolveProvider(providerId, modelId, overrides) {
|
|
|
55
90
|
return { providerId, modelId, apiKey, baseUrl };
|
|
56
91
|
}
|
|
57
92
|
/**
|
|
58
|
-
*
|
|
93
|
+
* Resolves a provider configuration for image and other media providers.
|
|
94
|
+
*
|
|
95
|
+
* Behaves like {@link resolveProvider} but relaxes the API-key requirement:
|
|
96
|
+
* when the provider is not listed in the known key map, the call succeeds
|
|
97
|
+
* without a key (allowing custom or keyless providers). Ollama still
|
|
98
|
+
* requires a `baseUrl`.
|
|
99
|
+
*
|
|
100
|
+
* @param providerId - Provider identifier (e.g. `"stability"`, `"replicate"`, `"ollama"`).
|
|
101
|
+
* @param modelId - Model identifier within the provider.
|
|
102
|
+
* @param overrides - Optional explicit API key and/or base URL overrides.
|
|
103
|
+
* @returns A {@link ResolvedProvider} ready for use with an image provider factory.
|
|
104
|
+
* @throws {Error} When a known provider is missing its required API key or base URL.
|
|
105
|
+
*/
|
|
106
|
+
export function resolveMediaProvider(providerId, modelId, overrides) {
|
|
107
|
+
const apiKey = overrides?.apiKey
|
|
108
|
+
?? (ENV_KEY_MAP[providerId] ? process.env[ENV_KEY_MAP[providerId]] : undefined);
|
|
109
|
+
const baseUrl = overrides?.baseUrl
|
|
110
|
+
?? (ENV_URL_MAP[providerId] ? process.env[ENV_URL_MAP[providerId]] : undefined);
|
|
111
|
+
if (providerId === 'ollama') {
|
|
112
|
+
if (!baseUrl) {
|
|
113
|
+
throw new Error(`No base URL for ollama. Set OLLAMA_BASE_URL or pass baseUrl.`);
|
|
114
|
+
}
|
|
115
|
+
return { providerId, modelId, baseUrl };
|
|
116
|
+
}
|
|
117
|
+
const envVar = ENV_KEY_MAP[providerId];
|
|
118
|
+
if (envVar && !apiKey) {
|
|
119
|
+
throw new Error(`No API key for ${providerId}. Set ${envVar} or pass apiKey.`);
|
|
120
|
+
}
|
|
121
|
+
return { providerId, modelId, apiKey, baseUrl };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Instantiates and initialises an {@link AIModelProviderManager} for a single provider.
|
|
125
|
+
*
|
|
126
|
+
* Constructs the provider config object from the `resolved` credentials and calls
|
|
127
|
+
* `manager.initialize()` before returning. The returned manager is ready for
|
|
128
|
+
* immediate use via `manager.getProvider(providerId)`.
|
|
129
|
+
*
|
|
130
|
+
* @param resolved - A {@link ResolvedProvider} produced by {@link resolveProvider}
|
|
131
|
+
* or {@link resolveMediaProvider}.
|
|
132
|
+
* @returns A fully initialised {@link AIModelProviderManager} instance.
|
|
59
133
|
*/
|
|
60
134
|
export async function createProviderManager(resolved) {
|
|
61
135
|
const manager = new AIModelProviderManager();
|
package/dist/api/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/api/model.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/api/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AA4BzF,MAAM,WAAW,GAA2B;IAC1C,MAAM,EAAE,gBAAgB;IACxB,SAAS,EAAE,mBAAmB;IAC9B,UAAU,EAAE,oBAAoB;IAChC,MAAM,EAAE,gBAAgB;IACxB,SAAS,EAAE,mBAAmB;IAC9B,SAAS,EAAE,qBAAqB;CACjC,CAAC;AAEF,MAAM,WAAW,GAA2B;IAC1C,MAAM,EAAE,iBAAiB;IACzB,UAAU,EAAE,qBAAqB;IACjC,SAAS,EAAE,oBAAoB;IAC/B,SAAS,EAAE,oBAAoB;IAC/B,MAAM,EAAE,iBAAiB;CAC1B,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK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AO,EAAE,OAAO,EAAE,CAAC;IAC1C,CAAC;IAED,MAAM,MAAM,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;IACvC,IAAI,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,kBAAkB,UAAU,SAAS,MAAM,kBAAkB,CAAC,CAAC;IACjF,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;AAClD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,QAA0B;IAE1B,MAAM,OAAO,GAAG,IAAI,sBAAsB,EAAE,CAAC;IAE7C,MAAM,cAAc,GAA4B,EAAE,CAAC;IACnD,IAAI,QAAQ,CAAC,MAAM;QAAE,cAAc,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;IAC7D,IAAI,QAAQ,CAAC,OAAO,EAAE,CAAC;QACrB,cAAc,CAAC,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC;QAC1C,cAAc,CAAC,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC;IAC5C,CAAC;IAED,MAAM,OAAO,CAAC,UAAU,CAAC;QACvB,SAAS,EAAE,CAAC;gBACV,UAAU,EAAE,QAAQ,CAAC,UAAU;gBAC/B,OAAO,EAAE,IAAI;gBACb,SAAS,EAAE,IAAI;gBACf,MAAM,EAAE,cAAc;aACvB,CAAC;KACH,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/api/streamText.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import type { GenerateTextOptions, TokenUsage, ToolCallRecord } from './generateText.js';
|
|
2
|
+
/**
|
|
3
|
+
* A discriminated union representing a single event emitted by the
|
|
4
|
+
* {@link StreamTextResult.fullStream} iterable.
|
|
5
|
+
*
|
|
6
|
+
* - `"text"` — incremental token delta from the model.
|
|
7
|
+
* - `"tool-call"` — the model requested a tool invocation.
|
|
8
|
+
* - `"tool-result"` — the tool has been executed and the result is available.
|
|
9
|
+
* - `"error"` — an unrecoverable error occurred; the stream ends after this part.
|
|
10
|
+
*/
|
|
2
11
|
export type StreamPart = {
|
|
3
12
|
type: 'text';
|
|
4
13
|
text: string;
|
|
@@ -14,15 +23,42 @@ export type StreamPart = {
|
|
|
14
23
|
type: 'error';
|
|
15
24
|
error: Error;
|
|
16
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* The object returned immediately by {@link streamText}.
|
|
28
|
+
*
|
|
29
|
+
* Consumers may iterate `textStream` for raw token deltas, `fullStream` for
|
|
30
|
+
* all event types, or simply `await` the promise properties for aggregated
|
|
31
|
+
* results once the stream has drained.
|
|
32
|
+
*/
|
|
17
33
|
export interface StreamTextResult {
|
|
34
|
+
/** Async iterable that yields only raw text-delta strings (filters out non-text parts). */
|
|
18
35
|
textStream: AsyncIterable<string>;
|
|
36
|
+
/** Async iterable that yields all {@link StreamPart} events in order. */
|
|
19
37
|
fullStream: AsyncIterable<StreamPart>;
|
|
38
|
+
/** Resolves to the fully assembled assistant reply when the stream completes. */
|
|
20
39
|
text: Promise<string>;
|
|
40
|
+
/** Resolves to aggregated {@link TokenUsage} when the stream completes. */
|
|
21
41
|
usage: Promise<TokenUsage>;
|
|
42
|
+
/** Resolves to the ordered list of {@link ToolCallRecord}s when the stream completes. */
|
|
22
43
|
toolCalls: Promise<ToolCallRecord[]>;
|
|
23
44
|
}
|
|
24
45
|
/**
|
|
25
|
-
* Stateless streaming text generation
|
|
46
|
+
* Stateless streaming text generation with optional multi-step tool calling.
|
|
47
|
+
*
|
|
48
|
+
* Returns a {@link StreamTextResult} immediately; the underlying provider call
|
|
49
|
+
* begins lazily when a consumer starts iterating `textStream` or `fullStream`.
|
|
50
|
+
* Awaiting `text`, `usage`, or `toolCalls` will also drain the stream.
|
|
51
|
+
*
|
|
52
|
+
* @param opts - Generation options (same shape as {@link generateText}).
|
|
53
|
+
* @returns A {@link StreamTextResult} with async iterables and awaitable promises.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const { textStream } = streamText({ model: 'openai:gpt-4o', prompt: 'Tell me a joke.' });
|
|
58
|
+
* for await (const chunk of textStream) {
|
|
59
|
+
* process.stdout.write(chunk);
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
26
62
|
*/
|
|
27
63
|
export declare function streamText(opts: GenerateTextOptions): StreamTextResult;
|
|
28
64
|
//# sourceMappingURL=streamText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamText.d.ts","sourceRoot":"","sources":["../../src/api/streamText.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"streamText.d.ts","sourceRoot":"","sources":["../../src/api/streamText.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,mBAAmB,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIzF;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpC;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAClC,yEAAyE;IACzE,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IACtC,iFAAiF;IACjF,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACtB,2EAA2E;IAC3E,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3B,yFAAyF;IACzF,SAAS,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;CACtC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,mBAAmB,GAAG,gBAAgB,CA2NtE"}
|
package/dist/api/streamText.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file streamText.ts
|
|
3
|
+
* Stateless streaming text generation for the AgentOS high-level API.
|
|
4
|
+
*
|
|
5
|
+
* Accepts the same {@link GenerateTextOptions} as {@link generateText} but returns
|
|
6
|
+
* immediately with async iterables so callers can process tokens incrementally.
|
|
7
|
+
* Multi-step tool calling is supported: tool-call and tool-result parts are
|
|
8
|
+
* yielded inline before the next LLM step begins.
|
|
9
|
+
*/
|
|
2
10
|
import { parseModelString, resolveProvider, createProviderManager } from './model.js';
|
|
3
|
-
import { adaptTools } from './
|
|
11
|
+
import { adaptTools } from './toolAdapter.js';
|
|
12
|
+
import { StreamingReconstructor } from '../core/llm/streaming/StreamingReconstructor.js';
|
|
4
13
|
/**
|
|
5
|
-
* Stateless streaming text generation
|
|
14
|
+
* Stateless streaming text generation with optional multi-step tool calling.
|
|
15
|
+
*
|
|
16
|
+
* Returns a {@link StreamTextResult} immediately; the underlying provider call
|
|
17
|
+
* begins lazily when a consumer starts iterating `textStream` or `fullStream`.
|
|
18
|
+
* Awaiting `text`, `usage`, or `toolCalls` will also drain the stream.
|
|
19
|
+
*
|
|
20
|
+
* @param opts - Generation options (same shape as {@link generateText}).
|
|
21
|
+
* @returns A {@link StreamTextResult} with async iterables and awaitable promises.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const { textStream } = streamText({ model: 'openai:gpt-4o', prompt: 'Tell me a joke.' });
|
|
26
|
+
* for await (const chunk of textStream) {
|
|
27
|
+
* process.stdout.write(chunk);
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
6
30
|
*/
|
|
7
31
|
export function streamText(opts) {
|
|
8
32
|
let resolveText;
|
|
@@ -12,57 +36,172 @@ export function streamText(opts) {
|
|
|
12
36
|
const usagePromise = new Promise(r => { resolveUsage = r; });
|
|
13
37
|
const toolCallsPromise = new Promise(r => { resolveToolCalls = r; });
|
|
14
38
|
const parts = [];
|
|
15
|
-
let fullText = '';
|
|
16
39
|
const allToolCalls = [];
|
|
17
40
|
async function* runStream() {
|
|
18
|
-
const { providerId, modelId } = parseModelString(opts.model);
|
|
19
|
-
const resolved = resolveProvider(providerId, modelId, { apiKey: opts.apiKey, baseUrl: opts.baseUrl });
|
|
20
|
-
const manager = await createProviderManager(resolved);
|
|
21
|
-
const provider = manager.getProvider(resolved.providerId);
|
|
22
|
-
if (!provider)
|
|
23
|
-
throw new Error(`Provider ${resolved.providerId} not available.`);
|
|
24
|
-
const messages = [];
|
|
25
|
-
if (opts.system)
|
|
26
|
-
messages.push({ role: 'system', content: opts.system });
|
|
27
|
-
if (opts.messages)
|
|
28
|
-
for (const m of opts.messages)
|
|
29
|
-
messages.push({ role: m.role, content: m.content });
|
|
30
|
-
if (opts.prompt)
|
|
31
|
-
messages.push({ role: 'user', content: opts.prompt });
|
|
32
|
-
const tools = adaptTools(opts.tools);
|
|
33
|
-
const toolSchemas = tools.length > 0
|
|
34
|
-
? tools.map(t => ({
|
|
35
|
-
type: 'function',
|
|
36
|
-
function: { name: t.name, description: t.description, parameters: t.inputSchema },
|
|
37
|
-
}))
|
|
38
|
-
: undefined;
|
|
39
|
-
const stream = provider.generateCompletionStream(resolved.modelId, messages, {
|
|
40
|
-
tools: toolSchemas,
|
|
41
|
-
temperature: opts.temperature,
|
|
42
|
-
maxTokens: opts.maxTokens,
|
|
43
|
-
});
|
|
44
41
|
const usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
42
|
+
let finalText = '';
|
|
43
|
+
try {
|
|
44
|
+
const { providerId, modelId } = parseModelString(opts.model);
|
|
45
|
+
const resolved = resolveProvider(providerId, modelId, { apiKey: opts.apiKey, baseUrl: opts.baseUrl });
|
|
46
|
+
const manager = await createProviderManager(resolved);
|
|
47
|
+
const provider = manager.getProvider(resolved.providerId);
|
|
48
|
+
if (!provider)
|
|
49
|
+
throw new Error(`Provider ${resolved.providerId} not available.`);
|
|
50
|
+
const messages = [];
|
|
51
|
+
if (opts.system)
|
|
52
|
+
messages.push({ role: 'system', content: opts.system });
|
|
53
|
+
if (opts.messages)
|
|
54
|
+
for (const m of opts.messages)
|
|
55
|
+
messages.push({ role: m.role, content: m.content });
|
|
56
|
+
if (opts.prompt)
|
|
57
|
+
messages.push({ role: 'user', content: opts.prompt });
|
|
58
|
+
const tools = adaptTools(opts.tools);
|
|
59
|
+
const toolMap = new Map();
|
|
60
|
+
for (const tool of tools)
|
|
61
|
+
toolMap.set(tool.name, tool);
|
|
62
|
+
const toolSchemas = tools.length > 0
|
|
63
|
+
? tools.map((tool) => ({
|
|
64
|
+
type: 'function',
|
|
65
|
+
function: { name: tool.name, description: tool.description, parameters: tool.inputSchema },
|
|
66
|
+
}))
|
|
67
|
+
: undefined;
|
|
68
|
+
const maxSteps = opts.maxSteps ?? 1;
|
|
69
|
+
for (let step = 0; step < maxSteps; step++) {
|
|
70
|
+
const stream = provider.generateCompletionStream(resolved.modelId, messages, {
|
|
71
|
+
tools: toolSchemas,
|
|
72
|
+
temperature: opts.temperature,
|
|
73
|
+
maxTokens: opts.maxTokens,
|
|
74
|
+
});
|
|
75
|
+
const reconstructor = new StreamingReconstructor();
|
|
76
|
+
for await (const chunk of stream) {
|
|
77
|
+
reconstructor.push(chunk);
|
|
78
|
+
const textDelta = chunk.responseTextDelta ?? '';
|
|
79
|
+
if (textDelta) {
|
|
80
|
+
const part = { type: 'text', text: textDelta };
|
|
81
|
+
parts.push(part);
|
|
82
|
+
yield part;
|
|
83
|
+
}
|
|
84
|
+
if (chunk.error) {
|
|
85
|
+
const error = new Error(chunk.error.message);
|
|
86
|
+
const part = { type: 'error', error };
|
|
87
|
+
parts.push(part);
|
|
88
|
+
yield part;
|
|
89
|
+
resolveText(finalText);
|
|
90
|
+
resolveUsage(usage);
|
|
91
|
+
resolveToolCalls(allToolCalls);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (chunk.isFinal && chunk.usage) {
|
|
95
|
+
usage.promptTokens += chunk.usage.promptTokens ?? 0;
|
|
96
|
+
usage.completionTokens += chunk.usage.completionTokens ?? 0;
|
|
97
|
+
usage.totalTokens += chunk.usage.totalTokens ?? 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const stepText = reconstructor.getFullText();
|
|
101
|
+
const finalChunk = reconstructor.getFinalChunk();
|
|
102
|
+
const streamedToolCalls = finalChunk?.choices?.[0]?.message?.tool_calls
|
|
103
|
+
?? reconstructor.getToolCalls()
|
|
104
|
+
.filter((toolCall) => toolCall.id && toolCall.name)
|
|
105
|
+
.map((toolCall) => ({
|
|
106
|
+
id: toolCall.id,
|
|
107
|
+
type: 'function',
|
|
108
|
+
function: {
|
|
109
|
+
name: toolCall.name,
|
|
110
|
+
arguments: toolCall.rawArguments || JSON.stringify(toolCall.arguments ?? {}),
|
|
111
|
+
},
|
|
112
|
+
}));
|
|
113
|
+
if (!streamedToolCalls || streamedToolCalls.length === 0) {
|
|
114
|
+
finalText = stepText;
|
|
115
|
+
resolveText(finalText);
|
|
116
|
+
resolveUsage(usage);
|
|
117
|
+
resolveToolCalls(allToolCalls);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
messages.push({
|
|
121
|
+
role: 'assistant',
|
|
122
|
+
content: stepText || null,
|
|
123
|
+
tool_calls: streamedToolCalls,
|
|
124
|
+
});
|
|
125
|
+
for (const toolCall of streamedToolCalls) {
|
|
126
|
+
const fnName = toolCall.function?.name ?? '';
|
|
127
|
+
const rawArgs = toolCall.function?.arguments ?? '{}';
|
|
128
|
+
const toolCallId = toolCall.id ?? '';
|
|
129
|
+
let parsedArgs;
|
|
130
|
+
try {
|
|
131
|
+
parsedArgs = typeof rawArgs === 'string' ? JSON.parse(rawArgs) : rawArgs;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
parsedArgs = {};
|
|
135
|
+
}
|
|
136
|
+
const toolCallRecord = {
|
|
137
|
+
name: fnName,
|
|
138
|
+
args: parsedArgs,
|
|
139
|
+
};
|
|
140
|
+
const requestPart = { type: 'tool-call', toolName: fnName, args: parsedArgs };
|
|
141
|
+
parts.push(requestPart);
|
|
142
|
+
yield requestPart;
|
|
143
|
+
const tool = toolMap.get(fnName);
|
|
144
|
+
if (!tool) {
|
|
145
|
+
toolCallRecord.error = `Tool "${fnName}" not found.`;
|
|
146
|
+
const resultPart = { type: 'tool-result', toolName: fnName, result: { error: toolCallRecord.error } };
|
|
147
|
+
parts.push(resultPart);
|
|
148
|
+
yield resultPart;
|
|
149
|
+
messages.push({
|
|
150
|
+
role: 'tool',
|
|
151
|
+
tool_call_id: toolCallId,
|
|
152
|
+
content: JSON.stringify({ error: toolCallRecord.error }),
|
|
153
|
+
});
|
|
154
|
+
allToolCalls.push(toolCallRecord);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const result = await tool.execute(parsedArgs, {});
|
|
159
|
+
toolCallRecord.result = result.output;
|
|
160
|
+
toolCallRecord.error = result.success ? undefined : result.error;
|
|
161
|
+
const resultPart = {
|
|
162
|
+
type: 'tool-result',
|
|
163
|
+
toolName: fnName,
|
|
164
|
+
result: result.output ?? { error: result.error },
|
|
165
|
+
};
|
|
166
|
+
parts.push(resultPart);
|
|
167
|
+
yield resultPart;
|
|
168
|
+
messages.push({
|
|
169
|
+
role: 'tool',
|
|
170
|
+
tool_call_id: toolCallId,
|
|
171
|
+
content: JSON.stringify(result.output ?? { error: result.error ?? 'Tool execution failed.' }),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
toolCallRecord.error = err?.message ?? String(err);
|
|
176
|
+
const resultPart = {
|
|
177
|
+
type: 'tool-result',
|
|
178
|
+
toolName: fnName,
|
|
179
|
+
result: { error: toolCallRecord.error },
|
|
180
|
+
};
|
|
181
|
+
parts.push(resultPart);
|
|
182
|
+
yield resultPart;
|
|
183
|
+
messages.push({
|
|
184
|
+
role: 'tool',
|
|
185
|
+
tool_call_id: toolCallId,
|
|
186
|
+
content: JSON.stringify({ error: toolCallRecord.error }),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
allToolCalls.push(toolCallRecord);
|
|
190
|
+
}
|
|
61
191
|
}
|
|
192
|
+
resolveText(finalText);
|
|
193
|
+
resolveUsage(usage);
|
|
194
|
+
resolveToolCalls(allToolCalls);
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
198
|
+
const part = { type: 'error', error };
|
|
199
|
+
parts.push(part);
|
|
200
|
+
yield part;
|
|
201
|
+
resolveText(finalText);
|
|
202
|
+
resolveUsage(usage);
|
|
203
|
+
resolveToolCalls(allToolCalls);
|
|
62
204
|
}
|
|
63
|
-
resolveText(fullText);
|
|
64
|
-
resolveUsage(usage);
|
|
65
|
-
resolveToolCalls(allToolCalls);
|
|
66
205
|
}
|
|
67
206
|
const fullStreamIterable = runStream();
|
|
68
207
|
const textStreamIterable = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamText.js","sourceRoot":"","sources":["../../src/api/streamText.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":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CAAC;IAED,MAAM,kBAAkB,GAAG,SAAS,EAAE,CAAC;IAEvC,MAAM,kBAAkB,GAA0B;QAChD,CAAC,MAAM,CAAC,aAAa,CAAC;YACpB,MAAM,KAAK,GAAG,kBAAkB,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;YACzD,OAAO;gBACL,KAAK,CAAC,IAAI;oBACR,OAAO,IAAI,EAAE,CAAC;wBACZ,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;wBAC3C,IAAI,IAAI;4BAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;wBAClD,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;4BAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;oBACvE,CAAC;gBACH,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,OAAO;QACL,UAAU,EAAE,kBAAkB;QAC9B,UAAU,EAAE,kBAAkB;QAC9B,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,YAAY;QACnB,SAAS,EAAE,gBAAgB;KAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file toolAdapter.ts
|
|
3
|
+
* Adapts user-supplied tool definitions (plain objects, Zod schemas, or {@link ITool}
|
|
4
|
+
* instances) into the canonical {@link ITool} shape expected by the AgentOS runtime.
|
|
5
|
+
*
|
|
6
|
+
* This module is used internally by {@link generateText} and {@link streamText} to
|
|
7
|
+
* normalise the `tools` option before handing them to a provider.
|
|
8
|
+
*/
|
|
9
|
+
import type { ITool } from '../core/tools/ITool.js';
|
|
10
|
+
/**
|
|
11
|
+
* Loose tool definition accepted by the high-level API.
|
|
12
|
+
* Consumers may supply a plain object with an optional Zod or JSON Schema
|
|
13
|
+
* `parameters` shape plus an `execute` callback.
|
|
14
|
+
*/
|
|
15
|
+
export interface ToolDefinition {
|
|
16
|
+
/** Human-readable description forwarded to the model in the tool schema. */
|
|
17
|
+
description?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Parameter schema for the tool. Accepts a JSON Schema object or a Zod schema.
|
|
20
|
+
* When a Zod schema is detected (`_def` property present) the adapter attempts
|
|
21
|
+
* to convert it via `zod-to-json-schema`, falling back to an empty object schema
|
|
22
|
+
* if that package is not installed.
|
|
23
|
+
*/
|
|
24
|
+
parameters?: Record<string, unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Async function that receives the parsed tool arguments and returns any value.
|
|
27
|
+
* Omit to create a no-op tool (returns `{ success: true }`).
|
|
28
|
+
*/
|
|
29
|
+
execute?: (args: any) => Promise<any>;
|
|
30
|
+
}
|
|
31
|
+
/** Map of tool name → definition accepted by the high-level API. */
|
|
32
|
+
export type ToolDefinitionMap = Record<string, ToolDefinition | ITool>;
|
|
33
|
+
/**
|
|
34
|
+
* Converts a {@link ToolDefinitionMap} into an array of {@link ITool} instances
|
|
35
|
+
* suitable for use with the AgentOS provider layer.
|
|
36
|
+
*
|
|
37
|
+
* - Existing {@link ITool} instances (identified by `inputSchema` + `id` properties)
|
|
38
|
+
* are passed through unchanged.
|
|
39
|
+
* - Plain {@link ToolDefinition} objects are wrapped in a minimal {@link ITool}
|
|
40
|
+
* implementation. Zod schemas are converted to JSON Schema when `zod-to-json-schema`
|
|
41
|
+
* is available.
|
|
42
|
+
*
|
|
43
|
+
* @param tools - Optional map of tool names to definitions. Returns `[]` when falsy.
|
|
44
|
+
* @returns Flat array of normalised {@link ITool} instances ready for provider dispatch.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const tools = adaptTools({
|
|
49
|
+
* getWeather: {
|
|
50
|
+
* description: 'Returns current weather for a city.',
|
|
51
|
+
* parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] },
|
|
52
|
+
* execute: async ({ city }) => fetchWeather(city),
|
|
53
|
+
* },
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function adaptTools(tools: ToolDefinitionMap | undefined): ITool[];
|
|
58
|
+
//# sourceMappingURL=toolAdapter.d.ts.map
|