@goharvest/simforge 0.1.0 → 0.2.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.
- package/README.md +173 -7
- package/dist/baml.d.ts +87 -0
- package/dist/baml.d.ts.map +1 -0
- package/dist/baml.js +247 -0
- package/dist/baml.js.map +1 -0
- package/dist/client.d.ts +16 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +82 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +357 -0
- package/dist/client.test.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/package.json +15 -5
package/README.md
CHANGED
|
@@ -2,6 +2,84 @@
|
|
|
2
2
|
|
|
3
3
|
Simforge client for provider-based API calls.
|
|
4
4
|
|
|
5
|
+
## Monorepo Setup
|
|
6
|
+
|
|
7
|
+
This package is part of a **pnpm workspace** monorepo. The workspace structure separates concerns:
|
|
8
|
+
|
|
9
|
+
- **Root `package.json`**: Contains only shared dev dependencies (Biome, TypeScript, Vitest, Knip, Madge)
|
|
10
|
+
- **Package `package.json`**: Contains runtime dependencies specific to this package
|
|
11
|
+
- You can run tests and validation from the root directory: `pnpm test` or `pnpm validate`
|
|
12
|
+
- No need to manually update shared dev tooling versions across packages
|
|
13
|
+
|
|
14
|
+
### Installing Dependencies
|
|
15
|
+
|
|
16
|
+
Always install dependencies from the **root directory**:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# From the root directory
|
|
20
|
+
pnpm install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Updating pnpm
|
|
24
|
+
|
|
25
|
+
To update pnpm across the monorepo:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# From the root directory
|
|
29
|
+
pnpm pnpm:update
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
This automatically updates pnpm and syncs the version in `package.json` and CI/CD workflows.
|
|
33
|
+
|
|
34
|
+
### Running Commands
|
|
35
|
+
|
|
36
|
+
You can run commands from this directory or from the root:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# From this directory
|
|
40
|
+
pnpm test
|
|
41
|
+
pnpm build
|
|
42
|
+
pnpm validate
|
|
43
|
+
|
|
44
|
+
# From the root directory (runs across all packages)
|
|
45
|
+
pnpm test # Run all tests
|
|
46
|
+
pnpm validate # Run lint + tsc + test
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Available Development Tools
|
|
50
|
+
|
|
51
|
+
This package includes the following shared development tools:
|
|
52
|
+
|
|
53
|
+
| Tool | Purpose | Command |
|
|
54
|
+
|------|---------|---------|
|
|
55
|
+
| **Vitest** | Unit testing with coverage | `pnpm test` |
|
|
56
|
+
| **Biome** | Fast linting & formatting | `pnpm lint` |
|
|
57
|
+
| **TypeScript** | Type checking | `pnpm tsc` |
|
|
58
|
+
| **Knip** | Find unused dependencies/exports | `pnpm knip` |
|
|
59
|
+
| **Madge** | Detect circular dependencies | `pnpm madge` |
|
|
60
|
+
|
|
61
|
+
**Run individual tools:**
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
# Run tests with coverage
|
|
65
|
+
pnpm test
|
|
66
|
+
|
|
67
|
+
# Lint and format code
|
|
68
|
+
pnpm lint
|
|
69
|
+
|
|
70
|
+
# Type check without building
|
|
71
|
+
pnpm tsc
|
|
72
|
+
|
|
73
|
+
# Find unused dependencies
|
|
74
|
+
pnpm knip
|
|
75
|
+
|
|
76
|
+
# Check for circular dependencies
|
|
77
|
+
pnpm madge
|
|
78
|
+
|
|
79
|
+
# Run all validation checks
|
|
80
|
+
pnpm validate
|
|
81
|
+
```
|
|
82
|
+
|
|
5
83
|
## Installation
|
|
6
84
|
|
|
7
85
|
```bash
|
|
@@ -16,7 +94,6 @@ yarn add @goharvest/simforge
|
|
|
16
94
|
|
|
17
95
|
```bash
|
|
18
96
|
cd simforge-typescript-sdk
|
|
19
|
-
pnpm install
|
|
20
97
|
pnpm build
|
|
21
98
|
```
|
|
22
99
|
|
|
@@ -39,12 +116,36 @@ pnpm add @harvest/simforge
|
|
|
39
116
|
|
|
40
117
|
## Usage
|
|
41
118
|
|
|
119
|
+
### Basic Usage (Local Execution)
|
|
120
|
+
|
|
121
|
+
By default, the SDK executes BAML functions **locally on the client** by fetching the BAML prompt from the server and running it with your own API keys:
|
|
122
|
+
|
|
42
123
|
```typescript
|
|
43
124
|
import { Simforge } from "@goharvest/simforge";
|
|
44
125
|
|
|
45
126
|
const client = new Simforge({
|
|
46
127
|
apiKey: "sf_your_api_key_here", // Required: Your Simforge API key
|
|
47
|
-
serviceUrl: "https://
|
|
128
|
+
serviceUrl: "https://simforge.goharvest.ai", // Optional
|
|
129
|
+
envVars: {
|
|
130
|
+
OPENAI_API_KEY: process.env.OPENAI_API_KEY, // Your LLM provider API keys
|
|
131
|
+
},
|
|
132
|
+
executeLocally: true, // Default: true
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const result = await client.call("method_name", {
|
|
136
|
+
arg1: "value1",
|
|
137
|
+
arg2: "value2",
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Server-Side Execution
|
|
142
|
+
|
|
143
|
+
If you prefer to execute BAML on the server (using the server's API keys), set `executeLocally: false`:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const client = new Simforge({
|
|
147
|
+
apiKey: "sf_your_api_key_here",
|
|
148
|
+
executeLocally: false, // Execute on server instead of locally
|
|
48
149
|
});
|
|
49
150
|
|
|
50
151
|
const result = await client.call("method_name", {
|
|
@@ -94,15 +195,80 @@ try {
|
|
|
94
195
|
|
|
95
196
|
## Configuration
|
|
96
197
|
|
|
97
|
-
| Option
|
|
98
|
-
|
|
|
99
|
-
| `apiKey`
|
|
100
|
-
| `serviceUrl`
|
|
101
|
-
| `timeout`
|
|
198
|
+
| Option | Required | Description |
|
|
199
|
+
| ---------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
200
|
+
| `apiKey` | Yes | Your Simforge API key (generate from your Simforge dashboard) |
|
|
201
|
+
| `serviceUrl` | No | The base URL for the Simforge API (default: https://simforge.goharvest.ai) |
|
|
202
|
+
| `timeout` | No | Request timeout in milliseconds (default: 120000) |
|
|
203
|
+
| `envVars` | No | Environment variables for LLM provider API keys (e.g., `{ OPENAI_API_KEY: "..." }`). Required for local execution. |
|
|
204
|
+
| `executeLocally` | No | Whether to execute BAML locally on the client (default: true). Set to false to execute on the server with the server's API keys. |
|
|
205
|
+
|
|
206
|
+
## Execution Modes
|
|
207
|
+
|
|
208
|
+
### Local Execution (Default)
|
|
209
|
+
|
|
210
|
+
When `executeLocally: true` (default), the SDK:
|
|
211
|
+
|
|
212
|
+
1. Fetches the BAML function definition from the Simforge server
|
|
213
|
+
2. Executes the BAML locally using the `@boundaryml/baml` runtime
|
|
214
|
+
3. Uses your own LLM provider API keys (passed via `envVars`)
|
|
215
|
+
4. Returns the result directly to you
|
|
216
|
+
|
|
217
|
+
**Benefits:**
|
|
218
|
+
|
|
219
|
+
- Full control over API keys and costs
|
|
220
|
+
- Lower latency (no server round-trip for execution)
|
|
221
|
+
- Privacy: Your data doesn't go through the Simforge server
|
|
222
|
+
|
|
223
|
+
**Requirements:**
|
|
224
|
+
|
|
225
|
+
- You must provide LLM provider API keys via `envVars`
|
|
226
|
+
- The BAML runtime runs in your environment (Node.js, browser with polyfills, etc.)
|
|
227
|
+
|
|
228
|
+
### Server-Side Execution
|
|
229
|
+
|
|
230
|
+
When `executeLocally: false`, the SDK:
|
|
231
|
+
|
|
232
|
+
1. Sends the function name and inputs to the Simforge server
|
|
233
|
+
2. The server executes the BAML using its own API keys
|
|
234
|
+
3. Returns the result to you
|
|
235
|
+
|
|
236
|
+
**Benefits:**
|
|
237
|
+
|
|
238
|
+
- No need to manage LLM provider API keys
|
|
239
|
+
- Simpler setup
|
|
240
|
+
- Works in any environment
|
|
241
|
+
|
|
242
|
+
**Requirements:**
|
|
243
|
+
|
|
244
|
+
- The Simforge server must have the necessary API keys configured
|
|
102
245
|
|
|
103
246
|
## Environment Variables
|
|
104
247
|
|
|
105
248
|
- `SIMFORGE_URL`: The base URL for the Simforge API (used if `serviceUrl` is not provided)
|
|
249
|
+
- `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc.: LLM provider API keys (required for local execution)
|
|
250
|
+
|
|
251
|
+
## Testing
|
|
252
|
+
|
|
253
|
+
A test script is provided to verify both local and server-side execution:
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
# Set up environment variables
|
|
257
|
+
export SIMFORGE_API_KEY="sf_your_api_key"
|
|
258
|
+
export OPENAI_API_KEY="your_openai_key"
|
|
259
|
+
|
|
260
|
+
# Run the test script
|
|
261
|
+
pnpm tsx test-local-execution.ts [function-name] [query]
|
|
262
|
+
|
|
263
|
+
# Example
|
|
264
|
+
pnpm tsx test-local-execution.ts extract-entities "What is the capital of France?"
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The test script will:
|
|
268
|
+
|
|
269
|
+
1. Test local execution with your API keys
|
|
270
|
+
2. Test server-side execution
|
|
271
|
+
3. Compare results and performance
|
|
106
272
|
|
|
107
273
|
## Publishing
|
|
108
274
|
|
package/dist/baml.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BAML execution utilities for the Simforge TypeScript SDK.
|
|
3
|
+
* This module provides functions to execute BAML prompts dynamically on the client side.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Provider definition from the server.
|
|
7
|
+
*/
|
|
8
|
+
export interface ProviderDefinition {
|
|
9
|
+
provider: string;
|
|
10
|
+
apiKeyEnv: string;
|
|
11
|
+
models: Array<{
|
|
12
|
+
model: string;
|
|
13
|
+
description: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Model parameters extracted from the LLM request.
|
|
18
|
+
*/
|
|
19
|
+
export interface ModelParameters {
|
|
20
|
+
temperature?: number;
|
|
21
|
+
max_tokens?: number;
|
|
22
|
+
top_p?: number;
|
|
23
|
+
frequency_penalty?: number;
|
|
24
|
+
presence_penalty?: number;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Metadata extracted from BAML execution.
|
|
29
|
+
*/
|
|
30
|
+
export interface BamlExecutionMetadata {
|
|
31
|
+
/** The actual model/client used (e.g., "OpenAI_GPT4_1_mini") */
|
|
32
|
+
clientName: string | null;
|
|
33
|
+
/** The provider (e.g., "openai", "anthropic") */
|
|
34
|
+
provider: string | null;
|
|
35
|
+
/** The actual model name from the API request (e.g., "gpt-4o") */
|
|
36
|
+
model: string | null;
|
|
37
|
+
/** Model parameters (temperature, max_tokens, etc.) */
|
|
38
|
+
modelParameters: ModelParameters | null;
|
|
39
|
+
/** Input tokens used */
|
|
40
|
+
inputTokens: number | null;
|
|
41
|
+
/** Output tokens used */
|
|
42
|
+
outputTokens: number | null;
|
|
43
|
+
/** Cached input tokens (if any) */
|
|
44
|
+
cachedInputTokens: number | null;
|
|
45
|
+
/** Execution duration in milliseconds */
|
|
46
|
+
durationMs: number | null;
|
|
47
|
+
/** Raw LLM response before parsing */
|
|
48
|
+
rawLlmResponse: string | null;
|
|
49
|
+
/** The actual rendered messages sent to the LLM (with ctx.output_format filled in) */
|
|
50
|
+
renderedMessages: Array<{
|
|
51
|
+
role: string;
|
|
52
|
+
content: string;
|
|
53
|
+
}> | null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Result of a BAML function execution with metadata.
|
|
57
|
+
*/
|
|
58
|
+
export interface BamlExecutionResult {
|
|
59
|
+
/** The parsed result of the function */
|
|
60
|
+
result: unknown;
|
|
61
|
+
/** Metadata about the execution (tokens, timing, model, etc.) */
|
|
62
|
+
metadata: BamlExecutionMetadata;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Generate the BAML client name from provider and model.
|
|
66
|
+
* e.g., "openai" + "gpt-4.1-mini" -> "OpenAI_GPT4_1_mini"
|
|
67
|
+
*/
|
|
68
|
+
export declare function getClientName(provider: string, model: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Type for allowed environment variables.
|
|
71
|
+
* Only OPENAI_API_KEY is currently supported.
|
|
72
|
+
*/
|
|
73
|
+
export type AllowedEnvVars = {
|
|
74
|
+
OPENAI_API_KEY?: string;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Runs the BAML function with the given inputs using the BAML runtime directly.
|
|
78
|
+
* No file generation or subprocess spawning needed.
|
|
79
|
+
*
|
|
80
|
+
* @param bamlSource - The BAML source code containing the function
|
|
81
|
+
* @param inputs - Named arguments to pass to the function
|
|
82
|
+
* @param providers - Available provider definitions
|
|
83
|
+
* @param envVars - Environment variables for API keys (only OPENAI_API_KEY is allowed)
|
|
84
|
+
* @returns The result and execution metadata of the BAML function call
|
|
85
|
+
*/
|
|
86
|
+
export declare function runFunctionWithBaml(bamlSource: string, inputs: Record<string, unknown>, providers: ProviderDefinition[], envVars: AllowedEnvVars): Promise<BamlExecutionResult>;
|
|
87
|
+
//# sourceMappingURL=baml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"baml.d.ts","sourceRoot":"","sources":["../src/baml.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,KAAK,CAAC;QACZ,KAAK,EAAE,MAAM,CAAA;QACb,WAAW,EAAE,MAAM,CAAA;KACpB,CAAC,CAAA;CACH;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,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CACvC,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,kBAAkB,EAAE,EAC/B,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,mBAAmB,CAAC,CAoD9B"}
|
package/dist/baml.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BAML execution utilities for the Simforge TypeScript SDK.
|
|
3
|
+
* This module provides functions to execute BAML prompts dynamically on the client side.
|
|
4
|
+
*/
|
|
5
|
+
import { BamlRuntime, Collector } from "@boundaryml/baml";
|
|
6
|
+
/**
|
|
7
|
+
* Capitalize first letter of a string.
|
|
8
|
+
*/
|
|
9
|
+
function capitalize(str) {
|
|
10
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Convert provider name to PascalCase.
|
|
14
|
+
* e.g., "openai" -> "OpenAI", "anthropic" -> "Anthropic"
|
|
15
|
+
*/
|
|
16
|
+
function formatProvider(provider) {
|
|
17
|
+
const providerMap = {
|
|
18
|
+
openai: "OpenAI",
|
|
19
|
+
anthropic: "Anthropic",
|
|
20
|
+
google: "Google",
|
|
21
|
+
};
|
|
22
|
+
return providerMap[provider] ?? capitalize(provider);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a model name to a valid BAML identifier part.
|
|
26
|
+
* e.g., "gpt-5-mini" -> "GPT5_mini", "gpt-4.1" -> "GPT4_1"
|
|
27
|
+
*/
|
|
28
|
+
function formatModel(model) {
|
|
29
|
+
return model
|
|
30
|
+
.replace(/^gpt-/, "GPT") // gpt- prefix -> GPT
|
|
31
|
+
.replace(/\./g, "_") // dots -> underscore
|
|
32
|
+
.replace(/-/g, "_"); // hyphens -> underscore
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Generate the BAML client name from provider and model.
|
|
36
|
+
* e.g., "openai" + "gpt-4.1-mini" -> "OpenAI_GPT4_1_mini"
|
|
37
|
+
*/
|
|
38
|
+
export function getClientName(provider, model) {
|
|
39
|
+
return `${formatProvider(provider)}_${formatModel(model)}`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Generates BAML client definition strings.
|
|
43
|
+
* BamlRuntime.fromFiles requires clients to be defined in source for parsing.
|
|
44
|
+
*/
|
|
45
|
+
function generateClientDefinitions(providers) {
|
|
46
|
+
const definitions = [];
|
|
47
|
+
for (const providerDef of providers) {
|
|
48
|
+
for (const model of providerDef.models) {
|
|
49
|
+
const clientName = getClientName(providerDef.provider, model.model);
|
|
50
|
+
definitions.push(`client<llm> ${clientName} {
|
|
51
|
+
provider ${providerDef.provider}
|
|
52
|
+
options {
|
|
53
|
+
model "${model.model}"
|
|
54
|
+
api_key env.${providerDef.apiKeyEnv}
|
|
55
|
+
}
|
|
56
|
+
}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return definitions.join("\n\n");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Prepends the default client definitions to a BAML source if it doesn't already define them.
|
|
63
|
+
*/
|
|
64
|
+
function withDefaultClients(bamlSource, providers) {
|
|
65
|
+
const hasDefaultClient = bamlSource.includes("client<llm> OpenAI_");
|
|
66
|
+
if (hasDefaultClient) {
|
|
67
|
+
return bamlSource;
|
|
68
|
+
}
|
|
69
|
+
const defaultClients = generateClientDefinitions(providers);
|
|
70
|
+
return `${defaultClients}\n\n${bamlSource}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Extracts the first function name from BAML source code.
|
|
74
|
+
*/
|
|
75
|
+
function extractFunctionName(bamlSource) {
|
|
76
|
+
const match = bamlSource.match(/function\s+(\w+)\s*\(/);
|
|
77
|
+
return match?.[1] ?? null;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Coerces input values from strings to their appropriate types.
|
|
81
|
+
* Web UI inputs are always strings, but BAML expects typed values.
|
|
82
|
+
*/
|
|
83
|
+
function coerceInputs(inputs) {
|
|
84
|
+
const coerced = {};
|
|
85
|
+
for (const [key, value] of Object.entries(inputs)) {
|
|
86
|
+
if (typeof value === "string") {
|
|
87
|
+
// Try to parse as JSON (handles numbers, booleans, arrays, objects)
|
|
88
|
+
try {
|
|
89
|
+
coerced[key] = JSON.parse(value);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Keep as string if not valid JSON
|
|
93
|
+
coerced[key] = value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
coerced[key] = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return coerced;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Extract data from the LLM HTTP request body.
|
|
104
|
+
* This contains the actual prompt, model, and parameters sent to the LLM.
|
|
105
|
+
*/
|
|
106
|
+
function extractFromRequestBody(selectedCall) {
|
|
107
|
+
try {
|
|
108
|
+
const requestBody = selectedCall?.httpRequest?.body?.json();
|
|
109
|
+
if (!requestBody || typeof requestBody !== "object") {
|
|
110
|
+
return { model: null, modelParameters: null, renderedMessages: null };
|
|
111
|
+
}
|
|
112
|
+
// Extract the actual model name
|
|
113
|
+
const model = typeof requestBody.model === "string" ? requestBody.model : null;
|
|
114
|
+
// Extract model parameters
|
|
115
|
+
const modelParameters = {};
|
|
116
|
+
if (typeof requestBody.temperature === "number") {
|
|
117
|
+
modelParameters.temperature = requestBody.temperature;
|
|
118
|
+
}
|
|
119
|
+
if (requestBody.max_tokens !== undefined) {
|
|
120
|
+
modelParameters.max_tokens = requestBody.max_tokens;
|
|
121
|
+
}
|
|
122
|
+
if (typeof requestBody.top_p === "number") {
|
|
123
|
+
modelParameters.top_p = requestBody.top_p;
|
|
124
|
+
}
|
|
125
|
+
if (typeof requestBody.frequency_penalty === "number") {
|
|
126
|
+
modelParameters.frequency_penalty = requestBody.frequency_penalty;
|
|
127
|
+
}
|
|
128
|
+
if (typeof requestBody.presence_penalty === "number") {
|
|
129
|
+
modelParameters.presence_penalty = requestBody.presence_penalty;
|
|
130
|
+
}
|
|
131
|
+
// Include response_format if present (for JSON mode)
|
|
132
|
+
if (requestBody.response_format) {
|
|
133
|
+
modelParameters.response_format = requestBody.response_format;
|
|
134
|
+
}
|
|
135
|
+
// Extract rendered messages
|
|
136
|
+
let renderedMessages = null;
|
|
137
|
+
if (Array.isArray(requestBody.messages)) {
|
|
138
|
+
renderedMessages = requestBody.messages
|
|
139
|
+
.filter((msg) => typeof msg === "object" &&
|
|
140
|
+
msg !== null &&
|
|
141
|
+
"role" in msg &&
|
|
142
|
+
typeof msg.role === "string")
|
|
143
|
+
.map((msg) => ({
|
|
144
|
+
role: msg.role,
|
|
145
|
+
content: typeof msg.content === "string"
|
|
146
|
+
? msg.content
|
|
147
|
+
: JSON.stringify(msg.content),
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
model,
|
|
152
|
+
modelParameters: Object.keys(modelParameters).length > 0 ? modelParameters : null,
|
|
153
|
+
renderedMessages,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return { model: null, modelParameters: null, renderedMessages: null };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Extract execution metadata from a BAML collector.
|
|
162
|
+
*/
|
|
163
|
+
function extractMetadataFromCollector(collector) {
|
|
164
|
+
const lastLog = collector.last;
|
|
165
|
+
const usage = collector.usage;
|
|
166
|
+
// Get the selected LLM call from the last function log
|
|
167
|
+
const calls = lastLog?.calls ?? [];
|
|
168
|
+
const selectedCall = calls.find((call) => call.selected) ?? calls[0];
|
|
169
|
+
// Extract the model, parameters, and rendered messages from the HTTP request
|
|
170
|
+
const { model, modelParameters, renderedMessages } = extractFromRequestBody(selectedCall);
|
|
171
|
+
return {
|
|
172
|
+
clientName: selectedCall?.clientName ?? null,
|
|
173
|
+
provider: selectedCall?.provider ?? null,
|
|
174
|
+
model,
|
|
175
|
+
modelParameters,
|
|
176
|
+
inputTokens: usage?.inputTokens ?? selectedCall?.usage?.inputTokens ?? null,
|
|
177
|
+
outputTokens: usage?.outputTokens ?? selectedCall?.usage?.outputTokens ?? null,
|
|
178
|
+
cachedInputTokens: usage?.cachedInputTokens ?? null,
|
|
179
|
+
durationMs: lastLog?.timing?.durationMs ?? null,
|
|
180
|
+
rawLlmResponse: lastLog?.rawLlmResponse ?? null,
|
|
181
|
+
renderedMessages,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Allowed environment variable keys for LLM providers.
|
|
186
|
+
* Only these keys will be passed to the BAML runtime.
|
|
187
|
+
*/
|
|
188
|
+
const ALLOWED_ENV_KEYS = ["OPENAI_API_KEY"];
|
|
189
|
+
/**
|
|
190
|
+
* Filters environment variables to only include allowed keys.
|
|
191
|
+
* This prevents accidentally passing sensitive environment variables to the BAML runtime.
|
|
192
|
+
*/
|
|
193
|
+
function filterEnvVars(envVars) {
|
|
194
|
+
const filtered = {};
|
|
195
|
+
for (const key of ALLOWED_ENV_KEYS) {
|
|
196
|
+
const value = envVars[key];
|
|
197
|
+
if (value) {
|
|
198
|
+
filtered[key] = value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return filtered;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Runs the BAML function with the given inputs using the BAML runtime directly.
|
|
205
|
+
* No file generation or subprocess spawning needed.
|
|
206
|
+
*
|
|
207
|
+
* @param bamlSource - The BAML source code containing the function
|
|
208
|
+
* @param inputs - Named arguments to pass to the function
|
|
209
|
+
* @param providers - Available provider definitions
|
|
210
|
+
* @param envVars - Environment variables for API keys (only OPENAI_API_KEY is allowed)
|
|
211
|
+
* @returns The result and execution metadata of the BAML function call
|
|
212
|
+
*/
|
|
213
|
+
export async function runFunctionWithBaml(bamlSource, inputs, providers, envVars) {
|
|
214
|
+
// Extract function name from the BAML source
|
|
215
|
+
const functionName = extractFunctionName(bamlSource);
|
|
216
|
+
if (!functionName) {
|
|
217
|
+
throw new Error("No function found in BAML source");
|
|
218
|
+
}
|
|
219
|
+
// Add default client definitions (runtime needs them for parsing)
|
|
220
|
+
const fullSource = withDefaultClients(bamlSource, providers);
|
|
221
|
+
// Filter env vars to only allowed keys
|
|
222
|
+
const filteredEnvVars = filterEnvVars(envVars);
|
|
223
|
+
// Create runtime from source with env vars
|
|
224
|
+
const runtime = BamlRuntime.fromFiles("/tmp/baml_runtime", { "source.baml": fullSource }, filteredEnvVars);
|
|
225
|
+
// Create context manager
|
|
226
|
+
const ctx = runtime.createContextManager();
|
|
227
|
+
// Create collector to capture execution metadata
|
|
228
|
+
const collector = new Collector("simforge-collector");
|
|
229
|
+
// Coerce inputs from strings to proper types
|
|
230
|
+
const args = coerceInputs(inputs);
|
|
231
|
+
// Call the function with collector
|
|
232
|
+
const functionResult = await runtime.callFunction(functionName, args, ctx, null, // TypeBuilder
|
|
233
|
+
null, // ClientRegistry
|
|
234
|
+
[collector], // Collectors - capture execution data
|
|
235
|
+
{}, // Tags
|
|
236
|
+
filteredEnvVars);
|
|
237
|
+
if (!functionResult.isOk()) {
|
|
238
|
+
throw new Error("BAML function execution failed");
|
|
239
|
+
}
|
|
240
|
+
// Extract metadata from collector
|
|
241
|
+
const metadata = extractMetadataFromCollector(collector);
|
|
242
|
+
return {
|
|
243
|
+
result: functionResult.parsed(false),
|
|
244
|
+
metadata,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=baml.js.map
|
package/dist/baml.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"baml.js","sourceRoot":"","sources":["../src/baml.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AA8DzD;;GAEG;AACH,SAAS,UAAU,CAAC,GAAW;IAC7B,OAAO,GAAG,CAAC,MAAM,CAAC,CA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";
|
|
4
5
|
export interface SimforgeConfig {
|
|
5
6
|
/** The API key for Simforge API authentication */
|
|
6
7
|
apiKey: string;
|
|
@@ -8,6 +9,10 @@ export interface SimforgeConfig {
|
|
|
8
9
|
serviceUrl?: string;
|
|
9
10
|
/** Request timeout in milliseconds (default: 120000) */
|
|
10
11
|
timeout?: number;
|
|
12
|
+
/** Environment variables for LLM provider API keys (only OPENAI_API_KEY is supported) */
|
|
13
|
+
envVars?: AllowedEnvVars;
|
|
14
|
+
/** Whether to execute BAML locally on the client (default: true) */
|
|
15
|
+
executeLocally?: boolean;
|
|
11
16
|
}
|
|
12
17
|
export interface CallResponse<T = unknown> {
|
|
13
18
|
result?: T;
|
|
@@ -26,12 +31,22 @@ export declare class Simforge {
|
|
|
26
31
|
private readonly apiKey;
|
|
27
32
|
private readonly serviceUrl;
|
|
28
33
|
private readonly timeout;
|
|
34
|
+
private readonly envVars;
|
|
35
|
+
private readonly executeLocally;
|
|
29
36
|
/**
|
|
30
37
|
* Initialize the Simforge client.
|
|
31
38
|
*
|
|
32
39
|
* @param config - Configuration options for the client
|
|
33
40
|
*/
|
|
34
41
|
constructor(config: SimforgeConfig);
|
|
42
|
+
/**
|
|
43
|
+
* Fetch the function with its current version and BAML prompt from the server.
|
|
44
|
+
*
|
|
45
|
+
* @param methodName - The name of the method to fetch
|
|
46
|
+
* @returns The function with current version, BAML prompt, and provider definitions
|
|
47
|
+
* @throws {SimforgeError} If the function is not found or an error occurs
|
|
48
|
+
*/
|
|
49
|
+
private fetchFunctionVersion;
|
|
35
50
|
/**
|
|
36
51
|
* Call a method with the given named arguments via BAML execution.
|
|
37
52
|
*
|
|
@@ -42,3 +57,4 @@ export declare class Simforge {
|
|
|
42
57
|
*/
|
|
43
58
|
call<T = unknown>(methodName: string, inputs?: Record<string, unknown>): Promise<T>;
|
|
44
59
|
}
|
|
60
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.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ANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Simforge client for provider-based API calls.
|
|
3
3
|
*/
|
|
4
|
+
import { runFunctionWithBaml, } from "./baml.js";
|
|
4
5
|
const DEFAULT_SERVICE_URL = "https://simforge.goharvest.ai";
|
|
5
6
|
export class SimforgeError extends Error {
|
|
6
7
|
constructor(message, url) {
|
|
@@ -22,6 +23,68 @@ export class Simforge {
|
|
|
22
23
|
this.apiKey = config.apiKey;
|
|
23
24
|
this.serviceUrl = config.serviceUrl ?? DEFAULT_SERVICE_URL;
|
|
24
25
|
this.timeout = config.timeout ?? 120000;
|
|
26
|
+
this.envVars = config.envVars ?? {};
|
|
27
|
+
this.executeLocally = config.executeLocally ?? true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fetch the function with its current version and BAML prompt from the server.
|
|
31
|
+
*
|
|
32
|
+
* @param methodName - The name of the method to fetch
|
|
33
|
+
* @returns The function with current version, BAML prompt, and provider definitions
|
|
34
|
+
* @throws {SimforgeError} If the function is not found or an error occurs
|
|
35
|
+
*/
|
|
36
|
+
async fetchFunctionVersion(methodName) {
|
|
37
|
+
const url = `${this.serviceUrl}/api/sdk/functions/lookup`;
|
|
38
|
+
const payload = { name: methodName };
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch(url, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify(payload),
|
|
49
|
+
signal: controller.signal,
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
const errorText = await response.text();
|
|
53
|
+
throw new SimforgeError(`HTTP ${response.status}: ${errorText.slice(0, 500)}`);
|
|
54
|
+
}
|
|
55
|
+
const result = await response.json();
|
|
56
|
+
// Check if function was not found
|
|
57
|
+
if (result.id === null) {
|
|
58
|
+
throw new SimforgeError(`Function "${methodName}" not found. Create it at: ${this.serviceUrl}/functions`, "/functions");
|
|
59
|
+
}
|
|
60
|
+
// Check if function has no prompt
|
|
61
|
+
if (!result.prompt) {
|
|
62
|
+
throw new SimforgeError(`Function "${methodName}" has no prompt configured. Add one at: ${this.serviceUrl}/functions/${result.id}`, `/functions/${result.id}`);
|
|
63
|
+
}
|
|
64
|
+
// Check for errors in the response
|
|
65
|
+
if (result.error) {
|
|
66
|
+
if (result.url) {
|
|
67
|
+
throw new SimforgeError(`${result.error} Configure it at: ${this.serviceUrl}${result.url}`, result.url);
|
|
68
|
+
}
|
|
69
|
+
throw new SimforgeError(result.error);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error instanceof SimforgeError) {
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
if (error instanceof Error) {
|
|
78
|
+
if (error.name === "AbortError") {
|
|
79
|
+
throw new SimforgeError(`Request timed out after ${this.timeout}ms`);
|
|
80
|
+
}
|
|
81
|
+
throw new SimforgeError(error.message);
|
|
82
|
+
}
|
|
83
|
+
throw new SimforgeError("Unknown error occurred");
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
clearTimeout(timeoutId);
|
|
87
|
+
}
|
|
25
88
|
}
|
|
26
89
|
/**
|
|
27
90
|
* Call a method with the given named arguments via BAML execution.
|
|
@@ -32,6 +95,24 @@ export class Simforge {
|
|
|
32
95
|
* @throws {SimforgeError} If service_url is not set, or if an error occurs
|
|
33
96
|
*/
|
|
34
97
|
async call(methodName, inputs = {}) {
|
|
98
|
+
// If executeLocally is true, fetch the BAML and execute it locally
|
|
99
|
+
if (this.executeLocally) {
|
|
100
|
+
try {
|
|
101
|
+
const functionVersion = await this.fetchFunctionVersion(methodName);
|
|
102
|
+
const result = await runFunctionWithBaml(functionVersion.prompt, inputs, functionVersion.providers, this.envVars);
|
|
103
|
+
return result.result;
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof SimforgeError) {
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
if (error instanceof Error) {
|
|
110
|
+
throw new SimforgeError(error.message);
|
|
111
|
+
}
|
|
112
|
+
throw new SimforgeError("Unknown error occurred during local execution");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Otherwise, fall back to server-side execution
|
|
35
116
|
const url = `${this.serviceUrl}/api/sdk/call`;
|
|
36
117
|
const payload = {
|
|
37
118
|
name: methodName,
|
|
@@ -80,3 +161,4 @@ export class Simforge {
|
|
|
80
161
|
}
|
|
81
162
|
}
|
|
82
163
|
}
|
|
164
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAGL,mBAAmB,GACpB,MAAM,WAAW,CAAA;AAElB,MAAM,mBAAmB,GAAG,+BAA+B,CAAA;AA+B3D,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YACE,OAAe,EACC,GAAY;QAE5B,KAAK,CAAC,OAAO,CAAC,CAAA;QAFE,QAAG,GAAH,GAAG,CAAS;QAG5B,IAAI,CAAC,IAAI,GAAG,eAAe,CAAA;IAC7B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,QAAQ;IAOnB;;;;OAIG;IACH,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;QAC3B,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,mBAAmB,CAAA;QAC1D,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAA;QACvC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,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":[],"mappings":""}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import * as baml from "./baml.js";
|
|
3
|
+
import { Simforge, SimforgeError } from "./client";
|
|
4
|
+
// Mock fetch globally
|
|
5
|
+
globalThis.fetch = vi.fn();
|
|
6
|
+
// Mock BAML execution
|
|
7
|
+
vi.mock("./baml.js", () => ({
|
|
8
|
+
runFunctionWithBaml: vi.fn(),
|
|
9
|
+
}));
|
|
10
|
+
describe("Simforge Client", () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.clearAllMocks();
|
|
13
|
+
});
|
|
14
|
+
describe("constructor", () => {
|
|
15
|
+
it("should create a client with default values", () => {
|
|
16
|
+
const client = new Simforge({ apiKey: "test-key" });
|
|
17
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
18
|
+
});
|
|
19
|
+
it("should create client even with empty apiKey (validation happens at call time)", () => {
|
|
20
|
+
const client = new Simforge({ apiKey: "" });
|
|
21
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
22
|
+
});
|
|
23
|
+
it("should use custom serviceUrl", () => {
|
|
24
|
+
const client = new Simforge({
|
|
25
|
+
apiKey: "test-key",
|
|
26
|
+
serviceUrl: "https://custom.example.com",
|
|
27
|
+
});
|
|
28
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
29
|
+
});
|
|
30
|
+
it("should use custom timeout", () => {
|
|
31
|
+
const client = new Simforge({
|
|
32
|
+
apiKey: "test-key",
|
|
33
|
+
timeout: 5000,
|
|
34
|
+
});
|
|
35
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
36
|
+
});
|
|
37
|
+
it("should accept envVars", () => {
|
|
38
|
+
const client = new Simforge({
|
|
39
|
+
apiKey: "test-key",
|
|
40
|
+
envVars: { OPENAI_API_KEY: "test-openai-key" },
|
|
41
|
+
});
|
|
42
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
43
|
+
});
|
|
44
|
+
it("should accept executeLocally flag", () => {
|
|
45
|
+
const client = new Simforge({
|
|
46
|
+
apiKey: "test-key",
|
|
47
|
+
executeLocally: false,
|
|
48
|
+
});
|
|
49
|
+
expect(client).toBeInstanceOf(Simforge);
|
|
50
|
+
});
|
|
51
|
+
describe("call method - local execution", () => {
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
// Reset BAML mock
|
|
54
|
+
vi.mocked(baml.runFunctionWithBaml).mockReset();
|
|
55
|
+
});
|
|
56
|
+
it("should execute BAML locally with correct types", async () => {
|
|
57
|
+
const mockFunctionData = {
|
|
58
|
+
id: "func-123",
|
|
59
|
+
name: "ExtractName",
|
|
60
|
+
hasPrompt: true,
|
|
61
|
+
versionId: "v1",
|
|
62
|
+
versionNumber: 1,
|
|
63
|
+
prompt: `
|
|
64
|
+
class Name {
|
|
65
|
+
first_name string
|
|
66
|
+
middle_name string?
|
|
67
|
+
last_name string
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function ExtractName(text: string) -> Name {
|
|
71
|
+
client GPT4
|
|
72
|
+
prompt #"Extract name from: {{ text }}"#
|
|
73
|
+
}
|
|
74
|
+
`,
|
|
75
|
+
providers: [
|
|
76
|
+
{
|
|
77
|
+
provider: "openai",
|
|
78
|
+
apiKeyEnv: "OPENAI_API_KEY",
|
|
79
|
+
models: [{ model: "gpt-4", description: "GPT-4" }],
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
const mockBamlResult = {
|
|
84
|
+
result: {
|
|
85
|
+
first_name: "John",
|
|
86
|
+
middle_name: "Michael",
|
|
87
|
+
last_name: "Doe",
|
|
88
|
+
},
|
|
89
|
+
metadata: {
|
|
90
|
+
clientName: "GPT4",
|
|
91
|
+
provider: "openai",
|
|
92
|
+
model: "gpt-4",
|
|
93
|
+
modelParameters: { temperature: 0.7 },
|
|
94
|
+
inputTokens: 10,
|
|
95
|
+
outputTokens: 20,
|
|
96
|
+
cachedInputTokens: 0,
|
|
97
|
+
durationMs: 100,
|
|
98
|
+
rawLlmResponse: '{"first_name":"John","middle_name":"Michael","last_name":"Doe"}',
|
|
99
|
+
renderedMessages: [
|
|
100
|
+
{
|
|
101
|
+
role: "system",
|
|
102
|
+
content: "Extract name from: John Michael Doe",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
108
|
+
ok: true,
|
|
109
|
+
json: async () => mockFunctionData,
|
|
110
|
+
});
|
|
111
|
+
// Mock BAML execution
|
|
112
|
+
vi.mocked(baml.runFunctionWithBaml).mockResolvedValueOnce(mockBamlResult);
|
|
113
|
+
const client = new Simforge({
|
|
114
|
+
apiKey: "test-key",
|
|
115
|
+
executeLocally: true,
|
|
116
|
+
envVars: { OPENAI_API_KEY: "test-openai-key" },
|
|
117
|
+
});
|
|
118
|
+
const result = await client.call("ExtractName", {
|
|
119
|
+
text: "John Michael Doe",
|
|
120
|
+
});
|
|
121
|
+
// Verify result structure
|
|
122
|
+
expect(result).toEqual({
|
|
123
|
+
first_name: "John",
|
|
124
|
+
middle_name: "Michael",
|
|
125
|
+
last_name: "Doe",
|
|
126
|
+
});
|
|
127
|
+
// Verify BAML was called with correct parameters
|
|
128
|
+
expect(baml.runFunctionWithBaml).toHaveBeenCalledWith(mockFunctionData.prompt, { text: "John Michael Doe" }, mockFunctionData.providers, { OPENAI_API_KEY: "test-openai-key" });
|
|
129
|
+
});
|
|
130
|
+
it("should handle wrong input types in local execution", async () => {
|
|
131
|
+
const mockFunctionData = {
|
|
132
|
+
id: "func-123",
|
|
133
|
+
name: "ExtractName",
|
|
134
|
+
hasPrompt: true,
|
|
135
|
+
versionId: "v1",
|
|
136
|
+
versionNumber: 1,
|
|
137
|
+
prompt: "function ExtractName(text: string) -> Name {}",
|
|
138
|
+
providers: [],
|
|
139
|
+
};
|
|
140
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
141
|
+
ok: true,
|
|
142
|
+
json: async () => mockFunctionData,
|
|
143
|
+
});
|
|
144
|
+
// Mock BAML to throw type error
|
|
145
|
+
vi.mocked(baml.runFunctionWithBaml).mockRejectedValueOnce(new Error("Invalid argument: Expected type String, got Number(12345)"));
|
|
146
|
+
const client = new Simforge({
|
|
147
|
+
apiKey: "test-key",
|
|
148
|
+
executeLocally: true,
|
|
149
|
+
});
|
|
150
|
+
await expect(
|
|
151
|
+
// biome-ignore lint/suspicious/noExplicitAny: Testing wrong type handling
|
|
152
|
+
client.call("ExtractName", { text: 12345 })).rejects.toThrow("Invalid argument");
|
|
153
|
+
});
|
|
154
|
+
it("should handle missing required inputs in local execution", async () => {
|
|
155
|
+
const mockFunctionData = {
|
|
156
|
+
id: "func-123",
|
|
157
|
+
name: "ExtractName",
|
|
158
|
+
hasPrompt: true,
|
|
159
|
+
versionId: "v1",
|
|
160
|
+
versionNumber: 1,
|
|
161
|
+
prompt: "function ExtractName(text: string) -> Name {}",
|
|
162
|
+
providers: [],
|
|
163
|
+
};
|
|
164
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
165
|
+
ok: true,
|
|
166
|
+
json: async () => mockFunctionData,
|
|
167
|
+
});
|
|
168
|
+
// Mock BAML to throw missing parameter error
|
|
169
|
+
vi.mocked(baml.runFunctionWithBaml).mockRejectedValueOnce(new Error("Missing required parameter: text"));
|
|
170
|
+
const client = new Simforge({
|
|
171
|
+
apiKey: "test-key",
|
|
172
|
+
executeLocally: true,
|
|
173
|
+
});
|
|
174
|
+
await expect(client.call("ExtractName", {})).rejects.toThrow("Missing required parameter");
|
|
175
|
+
});
|
|
176
|
+
it("should return typed Pydantic-like object for complex types", async () => {
|
|
177
|
+
const mockFunctionData = {
|
|
178
|
+
id: "func-123",
|
|
179
|
+
name: "ExtractPerson",
|
|
180
|
+
hasPrompt: true,
|
|
181
|
+
versionId: "v1",
|
|
182
|
+
versionNumber: 1,
|
|
183
|
+
prompt: `
|
|
184
|
+
class Address {
|
|
185
|
+
street string
|
|
186
|
+
city string
|
|
187
|
+
zip string?
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
class Person {
|
|
191
|
+
name string
|
|
192
|
+
age int
|
|
193
|
+
email string?
|
|
194
|
+
address Address
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function ExtractPerson(text: string) -> Person {}
|
|
198
|
+
`,
|
|
199
|
+
providers: [],
|
|
200
|
+
};
|
|
201
|
+
const mockBamlResult = {
|
|
202
|
+
result: {
|
|
203
|
+
name: "John Doe",
|
|
204
|
+
age: 30,
|
|
205
|
+
email: "john@example.com",
|
|
206
|
+
address: {
|
|
207
|
+
street: "123 Main St",
|
|
208
|
+
city: "New York",
|
|
209
|
+
zip: "10001",
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
metadata: {
|
|
213
|
+
clientName: "GPT4",
|
|
214
|
+
provider: "openai",
|
|
215
|
+
model: "gpt-4",
|
|
216
|
+
modelParameters: {},
|
|
217
|
+
inputTokens: 0,
|
|
218
|
+
outputTokens: 0,
|
|
219
|
+
cachedInputTokens: 0,
|
|
220
|
+
durationMs: 0,
|
|
221
|
+
rawLlmResponse: null,
|
|
222
|
+
renderedMessages: [],
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
226
|
+
ok: true,
|
|
227
|
+
json: async () => mockFunctionData,
|
|
228
|
+
});
|
|
229
|
+
vi.mocked(baml.runFunctionWithBaml).mockResolvedValueOnce(mockBamlResult);
|
|
230
|
+
const client = new Simforge({
|
|
231
|
+
apiKey: "test-key",
|
|
232
|
+
executeLocally: true,
|
|
233
|
+
});
|
|
234
|
+
const result = (await client.call("ExtractPerson", {
|
|
235
|
+
text: "John Doe, 30, john@example.com, 123 Main St, New York, 10001",
|
|
236
|
+
}));
|
|
237
|
+
// Verify nested structure
|
|
238
|
+
expect(result).toHaveProperty("name", "John Doe");
|
|
239
|
+
expect(result).toHaveProperty("age", 30);
|
|
240
|
+
expect(result).toHaveProperty("email", "john@example.com");
|
|
241
|
+
expect(result).toHaveProperty("address");
|
|
242
|
+
expect(result.address).toHaveProperty("street", "123 Main St");
|
|
243
|
+
expect(result.address).toHaveProperty("city", "New York");
|
|
244
|
+
expect(result.address).toHaveProperty("zip", "10001");
|
|
245
|
+
});
|
|
246
|
+
it("should throw error if function has no prompt configured", async () => {
|
|
247
|
+
const mockFunctionData = {
|
|
248
|
+
id: "func-123",
|
|
249
|
+
name: "ExtractName",
|
|
250
|
+
hasPrompt: false,
|
|
251
|
+
versionId: "v1",
|
|
252
|
+
versionNumber: 1,
|
|
253
|
+
prompt: null,
|
|
254
|
+
providers: [],
|
|
255
|
+
};
|
|
256
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
257
|
+
ok: true,
|
|
258
|
+
json: async () => mockFunctionData,
|
|
259
|
+
});
|
|
260
|
+
const client = new Simforge({
|
|
261
|
+
apiKey: "test-key",
|
|
262
|
+
executeLocally: true,
|
|
263
|
+
});
|
|
264
|
+
await expect(client.call("ExtractName", { text: "test" })).rejects.toThrow("has no prompt");
|
|
265
|
+
});
|
|
266
|
+
it("should pass only allowed env vars to BAML", async () => {
|
|
267
|
+
const mockFunctionData = {
|
|
268
|
+
id: "func-123",
|
|
269
|
+
name: "ExtractName",
|
|
270
|
+
hasPrompt: true,
|
|
271
|
+
versionId: "v1",
|
|
272
|
+
versionNumber: 1,
|
|
273
|
+
prompt: "function ExtractName(text: string) -> Name {}",
|
|
274
|
+
providers: [],
|
|
275
|
+
};
|
|
276
|
+
const mockBamlResult = {
|
|
277
|
+
result: { first_name: "John", last_name: "Doe" },
|
|
278
|
+
metadata: {
|
|
279
|
+
clientName: "GPT4",
|
|
280
|
+
provider: "openai",
|
|
281
|
+
model: "gpt-4",
|
|
282
|
+
modelParameters: {},
|
|
283
|
+
inputTokens: 0,
|
|
284
|
+
outputTokens: 0,
|
|
285
|
+
cachedInputTokens: 0,
|
|
286
|
+
durationMs: 0,
|
|
287
|
+
rawLlmResponse: null,
|
|
288
|
+
renderedMessages: [],
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
292
|
+
ok: true,
|
|
293
|
+
json: async () => mockFunctionData,
|
|
294
|
+
});
|
|
295
|
+
vi.mocked(baml.runFunctionWithBaml).mockResolvedValueOnce(mockBamlResult);
|
|
296
|
+
const client = new Simforge({
|
|
297
|
+
apiKey: "test-key",
|
|
298
|
+
executeLocally: true,
|
|
299
|
+
envVars: {
|
|
300
|
+
OPENAI_API_KEY: "test-key",
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
await client.call("ExtractName", { text: "John Doe" });
|
|
304
|
+
// Verify OPENAI_API_KEY was passed
|
|
305
|
+
const callArgs = vi.mocked(baml.runFunctionWithBaml).mock.calls[0];
|
|
306
|
+
expect(callArgs[3]).toEqual({ OPENAI_API_KEY: "test-key" });
|
|
307
|
+
// Verify no other keys are present
|
|
308
|
+
expect(Object.keys(callArgs[3])).toHaveLength(1);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
describe("call method - server-side execution", () => {
|
|
313
|
+
it("should make successful API call", async () => {
|
|
314
|
+
const mockResponse = "success";
|
|
315
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
316
|
+
ok: true,
|
|
317
|
+
json: async () => ({ result: mockResponse }),
|
|
318
|
+
});
|
|
319
|
+
const client = new Simforge({
|
|
320
|
+
apiKey: "test-key",
|
|
321
|
+
executeLocally: false,
|
|
322
|
+
});
|
|
323
|
+
const result = await client.call("testMethod", { input: "test" });
|
|
324
|
+
expect(result).toEqual(mockResponse);
|
|
325
|
+
expect(globalThis.fetch).toHaveBeenCalledWith(expect.stringContaining("/api/sdk/call"), expect.objectContaining({
|
|
326
|
+
method: "POST",
|
|
327
|
+
headers: expect.objectContaining({
|
|
328
|
+
Authorization: "Bearer test-key",
|
|
329
|
+
"Content-Type": "application/json",
|
|
330
|
+
}),
|
|
331
|
+
}));
|
|
332
|
+
});
|
|
333
|
+
it("should handle API errors", async () => {
|
|
334
|
+
;
|
|
335
|
+
globalThis.fetch.mockResolvedValueOnce({
|
|
336
|
+
ok: false,
|
|
337
|
+
status: 400,
|
|
338
|
+
json: async () => ({ error: "Bad request" }),
|
|
339
|
+
});
|
|
340
|
+
const client = new Simforge({
|
|
341
|
+
apiKey: "test-key",
|
|
342
|
+
executeLocally: false,
|
|
343
|
+
});
|
|
344
|
+
await expect(client.call("testMethod", {})).rejects.toThrow(SimforgeError);
|
|
345
|
+
});
|
|
346
|
+
it("should handle network errors", async () => {
|
|
347
|
+
;
|
|
348
|
+
globalThis.fetch.mockRejectedValueOnce(new Error("Network error"));
|
|
349
|
+
const client = new Simforge({
|
|
350
|
+
apiKey: "test-key",
|
|
351
|
+
executeLocally: false,
|
|
352
|
+
});
|
|
353
|
+
await expect(client.call("testMethod", {})).rejects.toThrow(SimforgeError);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
//# sourceMappingURL=client.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.test.js","sourceRoot":"","sources":["../src/client.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7D,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAElD,sBAAsB;AACtB,UAAU,CAAC,KAAK,GAAG,EAAE,CAAC,EAAE,EAAkB,CAAA;AAE1C,sBAAsB;AACtB,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1B,mBAAmB,EAAE,EAAE,CAAC,EAAE,EAAE;CAC7B,CAAC,CAAC,CAAA;AAEH,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,UAAU,CAAC,GAAG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AK,CAAC,eAAe,CAAC,CAC3B,CAAA;YAED,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC;gBAC1B,MAAM,EAAE,UAAU;gBAClB,cAAc,EAAE,KAAK;aACtB,CAAC,CAAA;YAEF,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;QAC5E,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Simforge client for provider-based API calls.
|
|
3
3
|
*/
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
4
|
+
export type { AllowedEnvVars, BamlExecutionMetadata, BamlExecutionResult, ModelParameters, ProviderDefinition, } from "./baml.js";
|
|
5
|
+
export type { CallResponse, SimforgeConfig } from "./client.js";
|
|
6
|
+
export { Simforge, SimforgeError } from "./client.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,kBAAkB,GACnB,MAAM,WAAW,CAAA;AAClB,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC/D,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAUH,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goharvest/simforge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Simforge client for provider-based API calls",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Simforge client for provider-based API calls with local BAML execution",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -16,7 +16,16 @@
|
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsc",
|
|
19
|
-
"clean": "rm -rf dist"
|
|
19
|
+
"clean": "rm -rf dist",
|
|
20
|
+
"lint": "biome check src",
|
|
21
|
+
"lint:fix": "biome check --write src",
|
|
22
|
+
"format": "biome format --write src",
|
|
23
|
+
"tsc": "tsc --noEmit",
|
|
24
|
+
"knip": "knip",
|
|
25
|
+
"madge": "madge --warning --ts-config ./tsconfig.json --extensions ts --circular ./src",
|
|
26
|
+
"validate": "pnpm lint && pnpm tsc && pnpm knip && pnpm madge",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"test:watch": "vitest"
|
|
20
29
|
},
|
|
21
30
|
"keywords": [
|
|
22
31
|
"simforge",
|
|
@@ -35,8 +44,9 @@
|
|
|
35
44
|
"bugs": {
|
|
36
45
|
"url": "https://github.com/anthropic-harvest/simforge-typescript-sdk/issues"
|
|
37
46
|
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@boundaryml/baml": "^0.214.0"
|
|
49
|
+
},
|
|
38
50
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^22.15.21",
|
|
40
|
-
"typescript": "^5.7.2"
|
|
41
51
|
}
|
|
42
52
|
}
|