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.
Files changed (48) hide show
  1. package/dist/classes/ToolCall.d.ts +8 -6
  2. package/dist/classes/ToolCall.js +9 -1
  3. package/dist/classes/message/AssistantMessage.d.ts +37 -13
  4. package/dist/classes/message/AssistantMessage.js +27 -19
  5. package/dist/classes/message/BaseMessage.d.ts +1 -0
  6. package/dist/classes/message/BaseMessage.js +5 -0
  7. package/dist/classes/message/DeveloperMessage.d.ts +12 -6
  8. package/dist/classes/message/DeveloperMessage.js +13 -6
  9. package/dist/classes/message/SystemMessage.d.ts +12 -6
  10. package/dist/classes/message/SystemMessage.js +13 -6
  11. package/dist/classes/message/ToolMessage.d.ts +13 -7
  12. package/dist/classes/message/ToolMessage.js +15 -7
  13. package/dist/classes/message/UserMessage.d.ts +9 -6
  14. package/dist/classes/message/UserMessage.js +11 -3
  15. package/dist/classes/message/index.js +1 -1
  16. package/dist/client.d.ts +4 -7
  17. package/dist/client.js +10 -7
  18. package/dist/clients/baseClient.d.ts +4 -5
  19. package/dist/clients/baseClient.js +26 -26
  20. package/dist/clients/google.js +2 -1
  21. package/dist/clients/ollama.js +78 -72
  22. package/dist/clients/openai.js +5 -3
  23. package/dist/clients/openaiResponses.js +2 -3
  24. package/dist/functions.d.ts +6 -2
  25. package/dist/functions.js +11 -32
  26. package/dist/model.d.ts +14 -12
  27. package/dist/model.js +36 -12
  28. package/dist/models.d.ts +13 -1
  29. package/dist/models.js +12 -0
  30. package/dist/statelogClient.d.ts +3 -4
  31. package/dist/statelogClient.js +6 -4
  32. package/dist/strategies/baseStrategy.js +2 -2
  33. package/dist/strategies/fallbackStrategy.d.ts +6 -7
  34. package/dist/strategies/fallbackStrategy.js +61 -48
  35. package/dist/strategies/idStrategy.d.ts +5 -3
  36. package/dist/strategies/idStrategy.js +44 -10
  37. package/dist/strategies/index.d.ts +3 -3
  38. package/dist/strategies/index.js +20 -22
  39. package/dist/strategies/raceStrategy.d.ts +3 -2
  40. package/dist/strategies/raceStrategy.js +8 -1
  41. package/dist/strategies/types.d.ts +68 -13
  42. package/dist/strategies/types.js +57 -1
  43. package/dist/types.d.ts +32 -11
  44. package/dist/types.js +32 -0
  45. package/dist/util/tool.js +4 -0
  46. package/dist/util.d.ts +10 -0
  47. package/dist/util.js +34 -0
  48. package/package.json +4 -5
package/dist/types.d.ts CHANGED
@@ -1,16 +1,24 @@
1
1
  export * from "./types/result.js";
2
2
  import { LogLevel } from "egonlog";
3
- import { ZodType } from "zod";
3
+ import z, { ZodType } from "zod";
4
4
  import { Message } from "./classes/message/index.js";
5
5
  import { ToolCall } from "./classes/ToolCall.js";
6
+ import { Model } from "./model.js";
6
7
  import { ModelName, Provider } from "./models.js";
8
+ import { ModelConfig, ModelNameAndProvider, Strategy, StrategyJSON } from "./strategies/types.js";
7
9
  import { Result } from "./types/result.js";
8
- import { Model, ModelConfig } from "./model.js";
9
- import { Strategy, StrategyJSON } from "./strategies/types.js";
10
10
  export type ThinkingBlock = {
11
11
  text: string;
12
12
  signature: string;
13
13
  };
14
+ export declare const TextPartSchema: z.ZodObject<{
15
+ type: z.ZodLiteral<"text">;
16
+ text: z.ZodString;
17
+ }, z.z.core.$strip>;
18
+ export declare const ThinkingBlockSchema: z.ZodObject<{
19
+ text: z.ZodString;
20
+ signature: z.ZodString;
21
+ }, z.z.core.$strip>;
14
22
  export type Budget = {
15
23
  timeBudgetMs?: number;
16
24
  tokenBudget?: number;
@@ -49,6 +57,7 @@ export type PromptConfig = {
49
57
  maxMessages?: number;
50
58
  budget?: Budget;
51
59
  abortSignal?: AbortSignal;
60
+ toolLoopDetection?: ToolLoopDetection;
52
61
  hooks?: Partial<{
53
62
  onStart: (config: PromptConfig) => void;
54
63
  onToolCall: (toolCall: ToolCall) => void;
@@ -56,7 +65,6 @@ export type PromptConfig = {
56
65
  onError: (error: Error) => void;
57
66
  onStrategyStart: (strategy: Strategy, config: SmolPromptConfig) => void;
58
67
  }>;
59
- strategy?: Strategy | StrategyJSON;
60
68
  };
61
69
  export type SmolConfig = {
62
70
  openAiApiKey?: string;
@@ -64,10 +72,9 @@ export type SmolConfig = {
64
72
  anthropicApiKey?: string;
65
73
  ollamaApiKey?: string;
66
74
  ollamaHost?: string;
67
- model: ModelName | ModelConfig;
75
+ model: ModelParam;
68
76
  provider?: Provider;
69
77
  logLevel?: LogLevel;
70
- toolLoopDetection?: ToolLoopDetection;
71
78
  statelog?: Partial<{
72
79
  host: string;
73
80
  projectId: string;
@@ -78,20 +85,26 @@ export type SmolConfig = {
78
85
  };
79
86
  export type ToolLoopDetection = {
80
87
  enabled: boolean;
81
- maxConsecutive: number;
88
+ maxCalls: number;
82
89
  intervention?: "remove-tool" | "remove-all-tools" | "throw-error" | "halt-execution";
83
90
  excludeTools?: string[];
84
91
  };
85
92
  export type ResolvedSmolConfig = Omit<SmolConfig, "model"> & {
86
93
  model: ModelName;
87
94
  };
88
- export type BaseClientConfig = ResolvedSmolConfig & {};
95
+ export type BaseClientConfig = ResolvedSmolConfig;
89
96
  export type TokenUsage = {
90
97
  inputTokens: number;
91
98
  outputTokens: number;
92
99
  cachedInputTokens?: number;
93
100
  totalTokens?: number;
94
101
  };
102
+ export declare const TokenUsageSchema: z.ZodObject<{
103
+ inputTokens: z.ZodNumber;
104
+ outputTokens: z.ZodNumber;
105
+ cachedInputTokens: z.ZodOptional<z.ZodNumber>;
106
+ totalTokens: z.ZodOptional<z.ZodNumber>;
107
+ }, z.z.core.$strip>;
95
108
  export type CostEstimate = {
96
109
  inputCost: number;
97
110
  outputCost: number;
@@ -99,6 +112,13 @@ export type CostEstimate = {
99
112
  totalCost: number;
100
113
  currency: string;
101
114
  };
115
+ export declare const CostEstimateSchema: z.ZodObject<{
116
+ inputCost: z.ZodNumber;
117
+ outputCost: z.ZodNumber;
118
+ cachedInputCost: z.ZodOptional<z.ZodNumber>;
119
+ totalCost: z.ZodNumber;
120
+ currency: z.ZodString;
121
+ }, z.z.core.$strip>;
102
122
  export declare function addTokenUsage(_a?: TokenUsage, _b?: TokenUsage): TokenUsage;
103
123
  export declare function addCosts(_a?: CostEstimate, _b?: CostEstimate): CostEstimate;
104
124
  export type PromptResult = {
@@ -109,6 +129,7 @@ export type PromptResult = {
109
129
  cost?: CostEstimate;
110
130
  model?: ModelName | ModelConfig;
111
131
  };
132
+ export declare function promptResult({ output, toolCalls, thinkingBlocks, usage, cost, model, }: Partial<PromptResult>): PromptResult;
112
133
  export type StreamChunk = {
113
134
  type: "text";
114
135
  text: string;
@@ -135,11 +156,11 @@ export interface SmolClient {
135
156
  _textSync(config: PromptConfig): Promise<Result<PromptResult>>;
136
157
  textStream(config: PromptConfig): AsyncGenerator<StreamChunk>;
137
158
  _textStream(config: PromptConfig): AsyncGenerator<StreamChunk>;
138
- prompt(text: string, config?: PromptConfig): Promise<Result<PromptResult>> | AsyncGenerator<StreamChunk>;
139
159
  }
140
- export type SmolPromptConfig = SmolConfig & PromptConfig;
160
+ export type SmolPromptConfig = PromptConfig & SmolConfig;
141
161
  export type TextPart = {
142
162
  type: "text";
143
163
  text: string;
144
164
  };
145
- export type ModelLike = ModelName | ModelConfig | Model;
165
+ export type ModelLike = ModelName | ModelConfig | Model | ModelNameAndProvider;
166
+ export type ModelParam = ModelName | ModelConfig | ModelNameAndProvider | Strategy | StrategyJSON;
package/dist/types.js CHANGED
@@ -1,4 +1,26 @@
1
1
  export * from "./types/result.js";
2
+ import z from "zod";
3
+ export const TextPartSchema = z.object({
4
+ type: z.literal("text"),
5
+ text: z.string(),
6
+ });
7
+ export const ThinkingBlockSchema = z.object({
8
+ text: z.string(),
9
+ signature: z.string(),
10
+ });
11
+ export const TokenUsageSchema = z.object({
12
+ inputTokens: z.number(),
13
+ outputTokens: z.number(),
14
+ cachedInputTokens: z.number().optional(),
15
+ totalTokens: z.number().optional(),
16
+ });
17
+ export const CostEstimateSchema = z.object({
18
+ inputCost: z.number(),
19
+ outputCost: z.number(),
20
+ cachedInputCost: z.number().optional(),
21
+ totalCost: z.number(),
22
+ currency: z.string(),
23
+ });
2
24
  export function addTokenUsage(_a, _b) {
3
25
  let a = _a;
4
26
  let b = _b;
@@ -39,3 +61,13 @@ export function addCosts(_a, _b) {
39
61
  currency: a.currency,
40
62
  };
41
63
  }
64
+ export function promptResult({ output, toolCalls, thinkingBlocks, usage, cost, model, }) {
65
+ return {
66
+ output: output || null,
67
+ toolCalls: toolCalls || [],
68
+ thinkingBlocks: thinkingBlocks,
69
+ usage,
70
+ cost,
71
+ model,
72
+ };
73
+ }
package/dist/util/tool.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { validateToolName } from "../util.js";
1
2
  export function zodToOpenAITool(name, schema, options = {}) {
3
+ validateToolName(name);
2
4
  // Convert Zod schema to JSON Schema
3
5
  const jsonSchema = schema.toJSONSchema();
4
6
  let description = "";
@@ -34,6 +36,7 @@ export function zodToOpenAITool(name, schema, options = {}) {
34
36
  };
35
37
  }
36
38
  export function zodToOpenAIResponsesTool(name, schema, options = {}) {
39
+ validateToolName(name);
37
40
  const jsonSchema = schema.toJSONSchema();
38
41
  const strict = options?.strict ?? false;
39
42
  const parameters = {
@@ -63,6 +66,7 @@ export function zodToOpenAIResponsesTool(name, schema, options = {}) {
63
66
  return tool;
64
67
  }
65
68
  export function zodToAnthropicTool(name, schema, options = {}) {
69
+ validateToolName(name);
66
70
  const jsonSchema = schema.toJSONSchema();
67
71
  let description;
68
72
  if (options?.description) {
package/dist/util.d.ts CHANGED
@@ -1,2 +1,12 @@
1
1
  export * from "./util/openai.js";
2
2
  export declare function round(num: number, places: number): number;
3
+ /**
4
+ * Sanitizes an object by removing keys that could cause prototype pollution.
5
+ * Returns a shallow copy with dangerous keys filtered out.
6
+ */
7
+ export declare function sanitizeAttributes(attrs: Record<string, any> | undefined): Record<string, any>;
8
+ /**
9
+ * Validates that a tool name contains only safe characters.
10
+ * Throws if the name contains characters outside [a-zA-Z0-9_-].
11
+ */
12
+ export declare function validateToolName(name: string): void;
package/dist/util.js CHANGED
@@ -3,3 +3,37 @@ export function round(num, places) {
3
3
  const factor = Math.pow(10, places);
4
4
  return Math.round(num * factor) / factor;
5
5
  }
6
+ const DANGEROUS_KEYS = new Set([
7
+ "__proto__",
8
+ "constructor",
9
+ "prototype",
10
+ "__defineGetter__",
11
+ "__defineSetter__",
12
+ "__lookupGetter__",
13
+ "__lookupSetter__",
14
+ ]);
15
+ /**
16
+ * Sanitizes an object by removing keys that could cause prototype pollution.
17
+ * Returns a shallow copy with dangerous keys filtered out.
18
+ */
19
+ export function sanitizeAttributes(attrs) {
20
+ if (!attrs)
21
+ return {};
22
+ const result = {};
23
+ for (const key of Object.keys(attrs)) {
24
+ if (!DANGEROUS_KEYS.has(key)) {
25
+ result[key] = attrs[key];
26
+ }
27
+ }
28
+ return result;
29
+ }
30
+ const VALID_TOOL_NAME = /^[a-zA-Z0-9_-]+$/;
31
+ /**
32
+ * Validates that a tool name contains only safe characters.
33
+ * Throws if the name contains characters outside [a-zA-Z0-9_-].
34
+ */
35
+ export function validateToolName(name) {
36
+ if (!VALID_TOOL_NAME.test(name)) {
37
+ throw new Error(`Invalid tool name "${name}": must match /^[a-zA-Z0-9_-]+$/`);
38
+ }
39
+ }
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "smoltalk",
3
- "version": "0.0.52",
3
+ "version": "0.0.53",
4
4
  "description": "A common interface for LLM APIs",
5
5
  "homepage": "https://github.com/egonSchiele/smoltalk",
6
6
  "scripts": {
7
7
  "test": "vitest",
8
8
  "test:tsc": "tsc -p tests/tsconfig.json",
9
- "coverage": "vitest --coverage",
10
9
  "build": "rm -rf dist && tsc",
11
10
  "start": "cd dist && node index.js",
12
11
  "doc": "typedoc --disableSources --out docs lib && prettier docs/ --write",
@@ -37,8 +36,7 @@
37
36
  "termcolors": "github:egonSchiele/termcolors",
38
37
  "typedoc": "^0.28.15",
39
38
  "typescript": "^5.9.3",
40
- "vitest": "^4.0.16",
41
- "zod": "^4.3.5"
39
+ "vitest": "^4.0.16"
42
40
  },
43
41
  "dependencies": {
44
42
  "@anthropic-ai/sdk": "^0.78.0",
@@ -46,6 +44,7 @@
46
44
  "egonlog": "^0.0.2",
47
45
  "nanoid": "^5.1.6",
48
46
  "ollama": "^0.6.3",
49
- "openai": "^6.15.0"
47
+ "openai": "^6.15.0",
48
+ "zod": "^4.3.6"
50
49
  }
51
50
  }