smoltalk 0.0.52 → 0.0.53
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/classes/ToolCall.d.ts +8 -6
- package/dist/classes/ToolCall.js +9 -1
- package/dist/classes/message/AssistantMessage.d.ts +37 -13
- package/dist/classes/message/AssistantMessage.js +27 -19
- package/dist/classes/message/BaseMessage.d.ts +1 -0
- package/dist/classes/message/BaseMessage.js +5 -0
- package/dist/classes/message/DeveloperMessage.d.ts +12 -6
- package/dist/classes/message/DeveloperMessage.js +13 -6
- package/dist/classes/message/SystemMessage.d.ts +12 -6
- package/dist/classes/message/SystemMessage.js +13 -6
- package/dist/classes/message/ToolMessage.d.ts +13 -7
- package/dist/classes/message/ToolMessage.js +15 -7
- package/dist/classes/message/UserMessage.d.ts +9 -6
- package/dist/classes/message/UserMessage.js +11 -3
- package/dist/classes/message/index.js +1 -1
- package/dist/client.d.ts +4 -7
- package/dist/client.js +10 -7
- package/dist/clients/baseClient.d.ts +4 -5
- package/dist/clients/baseClient.js +26 -26
- package/dist/clients/google.js +2 -1
- package/dist/clients/ollama.js +78 -72
- package/dist/clients/openai.js +5 -3
- package/dist/clients/openaiResponses.js +2 -3
- package/dist/functions.d.ts +6 -2
- package/dist/functions.js +11 -32
- package/dist/model.d.ts +14 -12
- package/dist/model.js +36 -12
- package/dist/models.d.ts +13 -1
- package/dist/models.js +12 -0
- package/dist/statelogClient.d.ts +3 -4
- package/dist/statelogClient.js +6 -4
- package/dist/strategies/baseStrategy.js +2 -2
- package/dist/strategies/fallbackStrategy.d.ts +6 -7
- package/dist/strategies/fallbackStrategy.js +61 -48
- package/dist/strategies/idStrategy.d.ts +5 -3
- package/dist/strategies/idStrategy.js +44 -10
- package/dist/strategies/index.d.ts +3 -3
- package/dist/strategies/index.js +20 -22
- package/dist/strategies/raceStrategy.d.ts +3 -2
- package/dist/strategies/raceStrategy.js +8 -1
- package/dist/strategies/types.d.ts +68 -13
- package/dist/strategies/types.js +57 -1
- package/dist/types.d.ts +32 -11
- package/dist/types.js +32 -0
- package/dist/util/tool.js +4 -0
- package/dist/util.d.ts +10 -0
- package/dist/util.js +34 -0
- package/package.json +4 -5
package/dist/statelogClient.js
CHANGED
|
@@ -24,9 +24,9 @@ export class StatelogClient {
|
|
|
24
24
|
this.debugMode = debugMode || false;
|
|
25
25
|
this.traceId = traceId || nanoid();
|
|
26
26
|
if (this.debugMode) {
|
|
27
|
-
console.log(`Statelog client initialized with host: ${host} and traceId: ${this.traceId}`, { config });
|
|
27
|
+
console.log(`Statelog client initialized with host: ${host} and traceId: ${this.traceId}`, { ...config, apiKey: "unset or [REDACTED]" });
|
|
28
28
|
}
|
|
29
|
-
if (!this.apiKey) {
|
|
29
|
+
if (!this.apiKey || this.apiKey.trim() === "") {
|
|
30
30
|
throw new Error("API key is required for StatelogClient");
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -141,7 +141,8 @@ export class StatelogClient {
|
|
|
141
141
|
const fullUrl = new URL(`/api/projects/${projectId}/upload`, this.host);
|
|
142
142
|
const url = fullUrl.toString();
|
|
143
143
|
const postBody = JSON.stringify({ entrypoint, files });
|
|
144
|
-
|
|
144
|
+
if (this.debugMode)
|
|
145
|
+
console.log({ entrypoint, fileCount: files.length });
|
|
145
146
|
const result = await fetch(url, {
|
|
146
147
|
method: "POST",
|
|
147
148
|
headers: {
|
|
@@ -183,7 +184,8 @@ export class StatelogClient {
|
|
|
183
184
|
entrypoint,
|
|
184
185
|
args,
|
|
185
186
|
});
|
|
186
|
-
|
|
187
|
+
if (this.debugMode)
|
|
188
|
+
console.log({ entrypoint, argCount: args?.length });
|
|
187
189
|
const result = await fetch(url, {
|
|
188
190
|
method: "POST",
|
|
189
191
|
headers: {
|
|
@@ -10,14 +10,14 @@ export class BaseStrategy {
|
|
|
10
10
|
this.statelogClient?.debug(`Calling onStrategyStart hook for strategy ${this.toString()}`);
|
|
11
11
|
config.hooks.onStrategyStart(this, config);
|
|
12
12
|
}
|
|
13
|
-
return this._text(
|
|
13
|
+
return this._text(config);
|
|
14
14
|
}
|
|
15
15
|
async textSync(config) {
|
|
16
16
|
this.statelogClient = config.statelog
|
|
17
17
|
? getStatelogClient(config.statelog)
|
|
18
18
|
: undefined;
|
|
19
19
|
this.statelogClient?.debug(`Starting strategy (sync) ${this.toString()}`);
|
|
20
|
-
return this._textSync(
|
|
20
|
+
return this._textSync(config);
|
|
21
21
|
}
|
|
22
22
|
async textStream(config) {
|
|
23
23
|
throw new Error("textStream method not implemented.");
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { SmolPromptConfig } from "../types.js";
|
|
1
|
+
import { ModelParam, PromptResult, Result, SmolPromptConfig } from "../types.js";
|
|
2
2
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
3
3
|
import { FallbackStrategyConfig, Strategy, StrategyJSON } from "./types.js";
|
|
4
4
|
export declare class FallbackStrategy extends BaseStrategy {
|
|
5
|
-
|
|
5
|
+
primaryStrategy: Strategy;
|
|
6
6
|
config: FallbackStrategyConfig;
|
|
7
|
-
constructor(
|
|
7
|
+
constructor(primaryStrategy: ModelParam, config: FallbackStrategyConfig);
|
|
8
8
|
toString(): string;
|
|
9
9
|
toShortString(): string;
|
|
10
|
-
_text(config: SmolPromptConfig): Promise<
|
|
11
|
-
|
|
12
|
-
toolCalls: never[];
|
|
13
|
-
}>>;
|
|
10
|
+
_text(config: SmolPromptConfig): Promise<Result<PromptResult>>;
|
|
11
|
+
_textWithFallbacks(config: SmolPromptConfig, strategy: Strategy, fallbackStrategies: FallbackStrategyConfig): Promise<Result<PromptResult>>;
|
|
14
12
|
toJSON(): StrategyJSON;
|
|
13
|
+
static fromJSON(json: unknown): FallbackStrategy;
|
|
15
14
|
}
|
|
@@ -1,80 +1,93 @@
|
|
|
1
1
|
import { SmolStructuredOutputError, SmolTimeoutError } from "../smolError.js";
|
|
2
|
-
import { success } from "../types.js";
|
|
2
|
+
import { success, } from "../types.js";
|
|
3
3
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
4
|
+
import { IDStrategy } from "./idStrategy.js";
|
|
5
|
+
import { fromJSON } from "./index.js";
|
|
6
|
+
import { FallbackStrategyJSONSchema, } from "./types.js";
|
|
4
7
|
export class FallbackStrategy extends BaseStrategy {
|
|
5
|
-
|
|
8
|
+
primaryStrategy;
|
|
6
9
|
config;
|
|
7
|
-
constructor(
|
|
10
|
+
constructor(primaryStrategy, config) {
|
|
8
11
|
super();
|
|
9
|
-
this.
|
|
12
|
+
this.primaryStrategy =
|
|
13
|
+
primaryStrategy instanceof BaseStrategy
|
|
14
|
+
? primaryStrategy
|
|
15
|
+
: new IDStrategy(primaryStrategy);
|
|
10
16
|
this.config = config;
|
|
11
17
|
}
|
|
12
18
|
toString() {
|
|
13
|
-
return `FallbackStrategy([${this.
|
|
19
|
+
return `FallbackStrategy([${this.primaryStrategy.toString()}], config: ${JSON.stringify(this.config)})`;
|
|
14
20
|
}
|
|
15
21
|
toShortString() {
|
|
16
|
-
return `fallback([${this.
|
|
22
|
+
return `fallback([${this.primaryStrategy.toString()}])`;
|
|
17
23
|
}
|
|
18
24
|
async _text(config) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
return this._textWithFallbacks(config, this.primaryStrategy, this.config);
|
|
26
|
+
}
|
|
27
|
+
async _textWithFallbacks(config, strategy, fallbackStrategies) {
|
|
28
|
+
try {
|
|
29
|
+
const result = await strategy.text(config);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
// If the abort signal was triggered (e.g. by a race strategy winner
|
|
34
|
+
// or external cancellation), stop without trying further fallbacks.
|
|
35
|
+
if (config.abortSignal?.aborted) {
|
|
36
|
+
return success({ output: null, toolCalls: [] });
|
|
24
37
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
failedStrategy: strategy.toString(),
|
|
35
|
-
strategyIndex: i,
|
|
36
|
-
});
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
else if (error instanceof SmolStructuredOutputError) {
|
|
41
|
-
if (this.config.fallbackOn.includes("structuredOutputFailure")) {
|
|
42
|
-
this.statelogClient?.debug("FallbackStrategy: falling back due to structured output failure", {
|
|
43
|
-
failedStrategy: strategy.toString(),
|
|
44
|
-
strategyIndex: i,
|
|
45
|
-
});
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
38
|
+
if (error instanceof SmolTimeoutError) {
|
|
39
|
+
if (fallbackStrategies.timeout &&
|
|
40
|
+
fallbackStrategies.timeout.length > 0) {
|
|
41
|
+
this.statelogClient?.debug("FallbackStrategy: falling back due to timeout", {
|
|
42
|
+
failedStrategy: strategy.toString(),
|
|
43
|
+
});
|
|
44
|
+
return this._textWithFallbacks(config, fromJSON(fallbackStrategies.timeout[0]),
|
|
45
|
+
// from here on, only consider the remaining fallbacks for this specific reason
|
|
46
|
+
{ timeout: fallbackStrategies.timeout.slice(1) });
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
}
|
|
49
|
+
else if (error instanceof SmolStructuredOutputError) {
|
|
50
|
+
if (fallbackStrategies.structuredOutputFailure &&
|
|
51
|
+
fallbackStrategies.structuredOutputFailure.length > 0) {
|
|
52
|
+
this.statelogClient?.debug("FallbackStrategy: falling back due to structured output failure", {
|
|
51
53
|
failedStrategy: strategy.toString(),
|
|
52
|
-
strategyIndex: i,
|
|
53
|
-
error: error.message,
|
|
54
54
|
});
|
|
55
|
-
|
|
55
|
+
return this._textWithFallbacks(config, fromJSON(fallbackStrategies.structuredOutputFailure[0]),
|
|
56
|
+
// from here on, only consider the remaining fallbacks for this specific reason
|
|
57
|
+
{
|
|
58
|
+
structuredOutputFailure: fallbackStrategies.structuredOutputFailure.slice(1),
|
|
59
|
+
});
|
|
56
60
|
}
|
|
57
|
-
|
|
61
|
+
}
|
|
62
|
+
if (fallbackStrategies.error && fallbackStrategies.error.length > 0) {
|
|
63
|
+
this.statelogClient?.debug("FallbackStrategy: falling back due to error", {
|
|
58
64
|
failedStrategy: strategy.toString(),
|
|
59
|
-
strategyIndex: i,
|
|
60
|
-
strategies: this.strategies.map((s) => s.toString()),
|
|
61
65
|
error: error.message,
|
|
62
66
|
});
|
|
63
|
-
|
|
67
|
+
return this._textWithFallbacks(config, fromJSON(fallbackStrategies.error[0]),
|
|
68
|
+
// from here on, only consider the remaining fallbacks for this specific reason
|
|
69
|
+
{ error: fallbackStrategies.error.slice(1) });
|
|
64
70
|
}
|
|
71
|
+
this.statelogClient?.debug("All strategies in FallbackStrategy failed", {
|
|
72
|
+
fallbackStrategy: this.toJSON(),
|
|
73
|
+
strategy,
|
|
74
|
+
fallbackStrategies,
|
|
75
|
+
});
|
|
76
|
+
throw error;
|
|
65
77
|
}
|
|
66
|
-
this.statelogClient?.debug("All strategies in FallbackStrategy failed", {
|
|
67
|
-
strategies: this.strategies.map((s) => s.toString()),
|
|
68
|
-
});
|
|
69
|
-
throw new Error(`All fallback strategies failed.`);
|
|
70
78
|
}
|
|
71
79
|
toJSON() {
|
|
72
80
|
return {
|
|
73
81
|
type: "fallback",
|
|
74
82
|
params: {
|
|
75
|
-
|
|
83
|
+
primaryStrategy: this.primaryStrategy.toJSON(),
|
|
76
84
|
config: this.config,
|
|
77
85
|
},
|
|
78
86
|
};
|
|
79
87
|
}
|
|
88
|
+
static fromJSON(json) {
|
|
89
|
+
const parsed = FallbackStrategyJSONSchema.parse(json);
|
|
90
|
+
const primaryStrategy = fromJSON(parsed.params.primaryStrategy);
|
|
91
|
+
return new FallbackStrategy(primaryStrategy, parsed.params.config);
|
|
92
|
+
}
|
|
80
93
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Model } from "../model.js";
|
|
2
|
-
import { SmolPromptConfig } from "../types.js";
|
|
2
|
+
import { ModelLike, PromptResult, Result, SmolPromptConfig } from "../types.js";
|
|
3
3
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
4
4
|
import { StrategyJSON } from "./types.js";
|
|
5
5
|
export declare class IDStrategy extends BaseStrategy {
|
|
6
6
|
model: Model;
|
|
7
|
-
constructor(model:
|
|
7
|
+
constructor(model: ModelLike, provider?: string);
|
|
8
8
|
toString(): string;
|
|
9
9
|
toShortString(): string;
|
|
10
|
-
_text(
|
|
10
|
+
_text(config: SmolPromptConfig): Promise<Result<PromptResult>>;
|
|
11
|
+
_textSync(config: SmolPromptConfig): Promise<Result<PromptResult>>;
|
|
11
12
|
toJSON(): StrategyJSON;
|
|
13
|
+
static fromJSON(json: unknown): IDStrategy;
|
|
12
14
|
}
|
|
@@ -1,25 +1,59 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getClient } from "../client.js";
|
|
2
|
+
import { splitConfig } from "../functions.js";
|
|
3
|
+
import { Model } from "../model.js";
|
|
2
4
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
5
|
+
import { IDStrategyJSONSchema, ModelNameAndProviderSchema, } from "./types.js";
|
|
3
6
|
export class IDStrategy extends BaseStrategy {
|
|
4
7
|
model;
|
|
5
|
-
constructor(model) {
|
|
8
|
+
constructor(model, provider) {
|
|
6
9
|
super();
|
|
7
|
-
this.model = model;
|
|
10
|
+
this.model = Model.create(model, provider);
|
|
8
11
|
}
|
|
9
12
|
toString() {
|
|
10
|
-
return `IDStrategy(
|
|
13
|
+
return `IDStrategy(${this.model.toString()})`;
|
|
11
14
|
}
|
|
12
15
|
toShortString() {
|
|
13
|
-
return `id(${this.model.
|
|
16
|
+
return `id(${this.model.toString()})`;
|
|
14
17
|
}
|
|
15
|
-
async _text(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
async _text(config) {
|
|
19
|
+
return this._textSync(config);
|
|
20
|
+
}
|
|
21
|
+
async _textSync(config) {
|
|
22
|
+
const configOverrides = {
|
|
18
23
|
model: this.model.getResolvedModel(),
|
|
24
|
+
provider: this.model.getProvider(),
|
|
19
25
|
};
|
|
20
|
-
|
|
26
|
+
const { smolConfig, promptConfig } = splitConfig({
|
|
27
|
+
...config,
|
|
28
|
+
...configOverrides,
|
|
29
|
+
});
|
|
30
|
+
const client = getClient({
|
|
31
|
+
...smolConfig,
|
|
32
|
+
...configOverrides,
|
|
33
|
+
});
|
|
34
|
+
return client.textSync(promptConfig);
|
|
21
35
|
}
|
|
36
|
+
// todo: this toJSON isn't fully accurate as it resolves the model,
|
|
37
|
+
// so strategy vs strategy.fromJSON(strategy.toJSON()) won't be the same
|
|
22
38
|
toJSON() {
|
|
23
|
-
return {
|
|
39
|
+
return {
|
|
40
|
+
type: "id",
|
|
41
|
+
params: {
|
|
42
|
+
model: this.model.getResolvedModel(),
|
|
43
|
+
provider: this.model.getProvider(),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
static fromJSON(json) {
|
|
48
|
+
const parsed = IDStrategyJSONSchema.safeParse(json);
|
|
49
|
+
if (parsed.success) {
|
|
50
|
+
return new IDStrategy(parsed.data.params.model, parsed.data.params.provider);
|
|
51
|
+
}
|
|
52
|
+
const parsedNameAndProvider = ModelNameAndProviderSchema.safeParse(json);
|
|
53
|
+
if (parsedNameAndProvider.success) {
|
|
54
|
+
const { model, provider } = parsedNameAndProvider.data;
|
|
55
|
+
return new IDStrategy(model, provider);
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`Invalid IDStrategy JSON: ${JSON.stringify(json)}`);
|
|
24
58
|
}
|
|
25
59
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ModelLike } from "../types.js";
|
|
1
|
+
import { ModelLike, ModelParam } from "../types.js";
|
|
2
2
|
import { FallbackStrategyConfig, Strategy, StrategyJSON } from "./types.js";
|
|
3
3
|
export * from "./baseStrategy.js";
|
|
4
4
|
export * from "./fallbackStrategy.js";
|
|
5
5
|
export * from "./idStrategy.js";
|
|
6
6
|
export * from "./raceStrategy.js";
|
|
7
7
|
export * from "./types.js";
|
|
8
|
-
export declare function race(...
|
|
8
|
+
export declare function race(...strategies: ModelParam[]): Strategy;
|
|
9
9
|
export declare function id(model: ModelLike): Strategy;
|
|
10
|
-
export declare function fallback(
|
|
10
|
+
export declare function fallback(primaryStrategy: ModelParam, config: FallbackStrategyConfig): Strategy;
|
|
11
11
|
export declare function fromJSON(json: StrategyJSON): Strategy;
|
package/dist/strategies/index.js
CHANGED
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
import { Model } from "../model.js";
|
|
2
|
-
import { BaseStrategy } from "./baseStrategy.js";
|
|
3
1
|
import { FallbackStrategy } from "./fallbackStrategy.js";
|
|
4
2
|
import { IDStrategy } from "./idStrategy.js";
|
|
5
3
|
import { RaceStrategy } from "./raceStrategy.js";
|
|
4
|
+
import { FallbackStrategyJSONSchema, IDStrategyJSONSchema, ModelNameAndProviderSchema, RaceStrategyJSONSchema, } from "./types.js";
|
|
6
5
|
export * from "./baseStrategy.js";
|
|
7
6
|
export * from "./fallbackStrategy.js";
|
|
8
7
|
export * from "./idStrategy.js";
|
|
9
8
|
export * from "./raceStrategy.js";
|
|
10
9
|
export * from "./types.js";
|
|
11
|
-
export function race(...
|
|
12
|
-
const strategies = _strategies.map((s) => s instanceof BaseStrategy
|
|
13
|
-
? s
|
|
14
|
-
: new IDStrategy(Model.create(s)));
|
|
10
|
+
export function race(...strategies) {
|
|
15
11
|
return new RaceStrategy(strategies);
|
|
16
12
|
}
|
|
17
13
|
export function id(model) {
|
|
18
|
-
return new IDStrategy(
|
|
14
|
+
return new IDStrategy(model);
|
|
19
15
|
}
|
|
20
|
-
export function fallback(
|
|
21
|
-
|
|
22
|
-
? s
|
|
23
|
-
: new IDStrategy(Model.create(s)));
|
|
24
|
-
return new FallbackStrategy(strategies, config);
|
|
16
|
+
export function fallback(primaryStrategy, config) {
|
|
17
|
+
return new FallbackStrategy(primaryStrategy, config);
|
|
25
18
|
}
|
|
26
19
|
export function fromJSON(json) {
|
|
27
|
-
if (
|
|
20
|
+
if (IDStrategyJSONSchema.safeParse(json).success) {
|
|
21
|
+
return IDStrategy.fromJSON(json);
|
|
22
|
+
}
|
|
23
|
+
else if (ModelNameAndProviderSchema.safeParse(json).success) {
|
|
24
|
+
return IDStrategy.fromJSON(json);
|
|
25
|
+
}
|
|
26
|
+
else if (RaceStrategyJSONSchema.safeParse(json).success) {
|
|
27
|
+
return RaceStrategy.fromJSON(json);
|
|
28
|
+
}
|
|
29
|
+
else if (FallbackStrategyJSONSchema.safeParse(json).success) {
|
|
30
|
+
return FallbackStrategy.fromJSON(json);
|
|
31
|
+
}
|
|
32
|
+
else if (typeof json === "string") {
|
|
28
33
|
return id(json);
|
|
29
34
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return id(json.params.model);
|
|
33
|
-
case "race":
|
|
34
|
-
return race(...json.params.strategies.map(fromJSON));
|
|
35
|
-
case "fallback":
|
|
36
|
-
return fallback(json.params.strategies.map(fromJSON), json.params.config);
|
|
37
|
-
default:
|
|
38
|
-
throw new Error(`Unknown strategy type: ${json.type}`);
|
|
35
|
+
else {
|
|
36
|
+
throw new Error(`Unknown strategy JSON: ${JSON.stringify(json)}`);
|
|
39
37
|
}
|
|
40
38
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { SmolPromptConfig } from "../types.js";
|
|
1
|
+
import { ModelParam, SmolPromptConfig } from "../types.js";
|
|
2
2
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
3
3
|
import { Strategy, StrategyJSON } from "./types.js";
|
|
4
4
|
export declare class RaceStrategy extends BaseStrategy {
|
|
5
5
|
strategies: Strategy[];
|
|
6
|
-
constructor(strategies:
|
|
6
|
+
constructor(strategies: ModelParam[]);
|
|
7
7
|
toString(): string;
|
|
8
8
|
toShortString(): string;
|
|
9
9
|
_text(config: SmolPromptConfig): Promise<import("../types.js").Failure | import("../types.js").Success<import("../types.js").PromptResult>>;
|
|
10
10
|
toJSON(): StrategyJSON;
|
|
11
|
+
static fromJSON(json: unknown): RaceStrategy;
|
|
11
12
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { getLogger } from "../logger.js";
|
|
2
2
|
import { BaseStrategy } from "./baseStrategy.js";
|
|
3
|
+
import { fromJSON, IDStrategy } from "./index.js";
|
|
4
|
+
import { RaceStrategyJSONSchema, } from "./types.js";
|
|
3
5
|
export class RaceStrategy extends BaseStrategy {
|
|
4
6
|
strategies;
|
|
5
7
|
constructor(strategies) {
|
|
6
8
|
super();
|
|
7
|
-
this.strategies = strategies;
|
|
9
|
+
this.strategies = strategies.map((s) => s instanceof BaseStrategy ? s : new IDStrategy(s));
|
|
8
10
|
}
|
|
9
11
|
toString() {
|
|
10
12
|
return `RaceStrategy([${this.strategies.map((s) => s.toString()).join(", ")}])`;
|
|
@@ -55,4 +57,9 @@ export class RaceStrategy extends BaseStrategy {
|
|
|
55
57
|
params: { strategies: this.strategies.map((s) => s.toJSON()) },
|
|
56
58
|
};
|
|
57
59
|
}
|
|
60
|
+
static fromJSON(json) {
|
|
61
|
+
const parsed = RaceStrategyJSONSchema.parse(json);
|
|
62
|
+
const strategies = parsed.params.strategies.map((s) => fromJSON(s));
|
|
63
|
+
return new RaceStrategy(strategies);
|
|
64
|
+
}
|
|
58
65
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { SmolPromptConfig, Result, PromptResult } from "../types.js";
|
|
2
3
|
export interface Strategy {
|
|
3
4
|
text(config: SmolPromptConfig): Promise<Result<PromptResult>>;
|
|
@@ -9,25 +10,79 @@ export interface Strategy {
|
|
|
9
10
|
toString(): string;
|
|
10
11
|
toShortString(): string;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
13
|
+
export declare const FallbackReasonSchema: z.ZodEnum<{
|
|
14
|
+
error: "error";
|
|
15
|
+
timeout: "timeout";
|
|
16
|
+
structuredOutputFailure: "structuredOutputFailure";
|
|
17
|
+
}>;
|
|
18
|
+
export declare const FallbackStrategyConfigSchema: z.ZodLazy<z.ZodRecord<z.ZodEnum<{
|
|
19
|
+
error: "error";
|
|
20
|
+
timeout: "timeout";
|
|
21
|
+
structuredOutputFailure: "structuredOutputFailure";
|
|
22
|
+
}> & z.core.$partial, z.ZodArray<z.ZodType<StrategyJSON, unknown, z.core.$ZodTypeInternals<StrategyJSON, unknown>>>>>;
|
|
23
|
+
export type FallbackReason = z.infer<typeof FallbackReasonSchema>;
|
|
24
|
+
export type FallbackStrategyConfig = z.infer<typeof FallbackStrategyConfigSchema>;
|
|
25
|
+
export type StrategyJSON = string | ModelNameAndProvider | IDStrategyJSON | RaceStrategyJSON | FallbackStrategyJSON;
|
|
26
|
+
export declare const IDStrategyJSONSchema: z.ZodObject<{
|
|
27
|
+
type: z.ZodLiteral<"id">;
|
|
28
|
+
params: z.ZodObject<{
|
|
29
|
+
model: z.ZodString;
|
|
30
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
export type IDStrategyJSON = z.infer<typeof IDStrategyJSONSchema>;
|
|
34
|
+
export declare const RaceStrategyJSONSchema: z.ZodType<RaceStrategyJSON>;
|
|
35
|
+
export type RaceStrategyJSON = {
|
|
22
36
|
type: "race";
|
|
23
37
|
params: {
|
|
24
38
|
strategies: StrategyJSON[];
|
|
25
39
|
};
|
|
26
|
-
}
|
|
40
|
+
};
|
|
41
|
+
export declare const FallbackStrategyJSONSchema: z.ZodType<FallbackStrategyJSON>;
|
|
42
|
+
export type FallbackStrategyJSON = {
|
|
27
43
|
type: "fallback";
|
|
28
44
|
params: {
|
|
29
|
-
|
|
45
|
+
primaryStrategy: StrategyJSON;
|
|
30
46
|
config: FallbackStrategyConfig;
|
|
31
47
|
};
|
|
32
48
|
};
|
|
33
|
-
export {
|
|
49
|
+
export type ModelNameAndProvider = {
|
|
50
|
+
model: string;
|
|
51
|
+
provider: string;
|
|
52
|
+
};
|
|
53
|
+
export declare const ModelNameAndProviderSchema: z.ZodObject<{
|
|
54
|
+
model: z.ZodString;
|
|
55
|
+
provider: z.ZodString;
|
|
56
|
+
}, z.core.$strip>;
|
|
57
|
+
export declare const ModelNameSchema: z.ZodString;
|
|
58
|
+
export declare const OptimizationSchema: z.ZodEnum<{
|
|
59
|
+
reasoning: "reasoning";
|
|
60
|
+
speed: "speed";
|
|
61
|
+
cost: "cost";
|
|
62
|
+
"large-context": "large-context";
|
|
63
|
+
}>;
|
|
64
|
+
export type Optimization = z.infer<typeof OptimizationSchema>;
|
|
65
|
+
export declare const ModelConfigSchema: z.ZodObject<{
|
|
66
|
+
optimizeFor: z.ZodArray<z.ZodEnum<{
|
|
67
|
+
reasoning: "reasoning";
|
|
68
|
+
speed: "speed";
|
|
69
|
+
cost: "cost";
|
|
70
|
+
"large-context": "large-context";
|
|
71
|
+
}>>;
|
|
72
|
+
providers: z.ZodArray<z.ZodEnum<{
|
|
73
|
+
local: "local";
|
|
74
|
+
ollama: "ollama";
|
|
75
|
+
openai: "openai";
|
|
76
|
+
"openai-responses": "openai-responses";
|
|
77
|
+
anthropic: "anthropic";
|
|
78
|
+
google: "google";
|
|
79
|
+
replicate: "replicate";
|
|
80
|
+
modal: "modal";
|
|
81
|
+
}>>;
|
|
82
|
+
limit: z.ZodOptional<z.ZodObject<{
|
|
83
|
+
cost: z.ZodOptional<z.ZodNumber>;
|
|
84
|
+
}, z.core.$strip>>;
|
|
85
|
+
}, z.core.$strip>;
|
|
86
|
+
export type ModelConfig = z.infer<typeof ModelConfigSchema>;
|
|
87
|
+
export declare const StrategyJSONSchema: z.ZodType<StrategyJSON>;
|
|
88
|
+
export declare function isStrategy(value: unknown): value is StrategyJSON;
|
package/dist/strategies/types.js
CHANGED
|
@@ -1 +1,57 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ProviderSchema } from "../models.js";
|
|
3
|
+
export const FallbackReasonSchema = z.enum([
|
|
4
|
+
"error",
|
|
5
|
+
"timeout",
|
|
6
|
+
"structuredOutputFailure",
|
|
7
|
+
]);
|
|
8
|
+
export const FallbackStrategyConfigSchema = z.lazy(() => z.partialRecord(FallbackReasonSchema, z.array(StrategyJSONSchema)));
|
|
9
|
+
export const IDStrategyJSONSchema = z.object({
|
|
10
|
+
type: z.literal("id"),
|
|
11
|
+
params: z.object({ model: z.string(), provider: z.string().optional() }),
|
|
12
|
+
});
|
|
13
|
+
export const RaceStrategyJSONSchema = z.lazy(() => z.object({
|
|
14
|
+
type: z.literal("race"),
|
|
15
|
+
params: z.object({ strategies: z.array(StrategyJSONSchema) }),
|
|
16
|
+
}));
|
|
17
|
+
export const FallbackStrategyJSONSchema = z.lazy(() => z.object({
|
|
18
|
+
type: z.literal("fallback"),
|
|
19
|
+
params: z.object({
|
|
20
|
+
primaryStrategy: StrategyJSONSchema,
|
|
21
|
+
config: FallbackStrategyConfigSchema,
|
|
22
|
+
}),
|
|
23
|
+
}));
|
|
24
|
+
export const ModelNameAndProviderSchema = z.object({
|
|
25
|
+
model: z.string(),
|
|
26
|
+
provider: z.string(),
|
|
27
|
+
});
|
|
28
|
+
export const ModelNameSchema = z
|
|
29
|
+
.string()
|
|
30
|
+
.regex(/^[a-zA-Z0-9._:-]+$/, "Model name must only contain letters, numbers, dots, underscores, hyphens, and colons");
|
|
31
|
+
export const OptimizationSchema = z.enum([
|
|
32
|
+
"speed",
|
|
33
|
+
"reasoning",
|
|
34
|
+
"cost",
|
|
35
|
+
"large-context",
|
|
36
|
+
]);
|
|
37
|
+
export const ModelConfigSchema = z.object({
|
|
38
|
+
optimizeFor: z.array(OptimizationSchema),
|
|
39
|
+
providers: z.array(ProviderSchema),
|
|
40
|
+
limit: z
|
|
41
|
+
.object({
|
|
42
|
+
cost: z.number().optional(),
|
|
43
|
+
})
|
|
44
|
+
.optional(),
|
|
45
|
+
});
|
|
46
|
+
export const StrategyJSONSchema = z.lazy(() => z.union([
|
|
47
|
+
ModelNameSchema,
|
|
48
|
+
ModelNameAndProviderSchema,
|
|
49
|
+
IDStrategyJSONSchema,
|
|
50
|
+
RaceStrategyJSONSchema,
|
|
51
|
+
FallbackStrategyJSONSchema,
|
|
52
|
+
]));
|
|
53
|
+
// Helper to detect if a value is a StrategyJSON object (not a plain string)
|
|
54
|
+
export function isStrategy(value) {
|
|
55
|
+
const result = StrategyJSONSchema.safeParse(value);
|
|
56
|
+
return result.success;
|
|
57
|
+
}
|