@struktur/sdk 1.2.1 → 2.0.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/LICENSE +110 -0
- package/README.md +7 -3
- package/package.json +3 -1
- package/src/agent-cli-integration.test.ts +47 -0
- package/src/agent-export.test.ts +17 -0
- package/src/agent-tool-labels.test.ts +50 -0
- package/src/artifacts/AGENTS.md +1 -1
- package/src/index.ts +4 -0
- package/src/llm/LLMClient.test.ts +198 -0
- package/src/llm/LLMClient.ts +96 -19
- package/src/llm/resolveModel.ts +86 -0
- package/src/types.ts +1 -0
- package/src/validation/AGENTS.md +3 -2
- package/src/validation/validator.test.ts +32 -0
- package/src/validation/validator.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 mateffy
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT license, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
95
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
96
|
+
the Software without restriction, including without limitation the rights to
|
|
97
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
98
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
99
|
+
so, subject to the following conditions:
|
|
100
|
+
|
|
101
|
+
The above copyright notice and this permission notice shall be included in all
|
|
102
|
+
copies or substantial portions of the Software.
|
|
103
|
+
|
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
105
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
106
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
107
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
108
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
109
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
110
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -36,8 +36,7 @@ bun add @struktur/sdk
|
|
|
36
36
|
## Quick Example
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
|
-
import { extract,
|
|
40
|
-
import { openai } from "@ai-sdk/openai";
|
|
39
|
+
import { extract, agent, urlToArtifact } from "@struktur/sdk";
|
|
41
40
|
import type { JSONSchemaType } from "ajv";
|
|
42
41
|
|
|
43
42
|
type Invoice = { number: string; vendor: string; total: number };
|
|
@@ -58,13 +57,18 @@ const artifact = await urlToArtifact("https://example.com/invoice.pdf");
|
|
|
58
57
|
const result = await extract({
|
|
59
58
|
artifacts: [artifact],
|
|
60
59
|
schema,
|
|
61
|
-
strategy:
|
|
60
|
+
strategy: agent({
|
|
61
|
+
provider: "anthropic",
|
|
62
|
+
modelId: "claude-sonnet-4",
|
|
63
|
+
}),
|
|
62
64
|
});
|
|
63
65
|
|
|
64
66
|
console.log(result.data.number); // fully typed
|
|
65
67
|
console.log(result.usage.totalTokens);
|
|
66
68
|
```
|
|
67
69
|
|
|
70
|
+
The **Agent strategy** is the default. It autonomously explores documents using a virtual filesystem and extracts data incrementally. For specific use cases, other strategies like `simple`, `parallel`, and `sequential` are also available.
|
|
71
|
+
|
|
68
72
|
## Documentation
|
|
69
73
|
|
|
70
74
|
Full documentation at **[struktur.sh](https://struktur.sh)**
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@struktur/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"license": "FSL-1.1-MIT",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "src/index.ts",
|
|
6
7
|
"types": "src/index.ts",
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
"@ai-sdk/google": "^3.0.0",
|
|
20
21
|
"@ai-sdk/openai": "^3.0.0",
|
|
21
22
|
"@openrouter/ai-sdk-provider": "^2.0.0",
|
|
23
|
+
"@struktur/agent-strategy": "1.0.0",
|
|
22
24
|
"ai": "^6.0.97",
|
|
23
25
|
"ajv": "^8.17.1",
|
|
24
26
|
"ajv-formats": "^3.0.1",
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { test, expect, describe } from "bun:test";
|
|
2
|
+
import { agent, AgentStrategy } from "@struktur/sdk";
|
|
3
|
+
|
|
4
|
+
describe("Agent Strategy CLI Integration", () => {
|
|
5
|
+
test("agent strategy accepts provider and modelId", () => {
|
|
6
|
+
const strategy = agent<{ name: string }>({
|
|
7
|
+
provider: "anthropic",
|
|
8
|
+
modelId: "claude-sonnet-4",
|
|
9
|
+
maxSteps: 30,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
expect(strategy).toBeInstanceOf(AgentStrategy);
|
|
13
|
+
expect(strategy.name).toBe("agent");
|
|
14
|
+
expect(strategy.getEstimatedSteps()).toBe(30);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("agent strategy works with openai", () => {
|
|
18
|
+
const strategy = agent<{ name: string }>({
|
|
19
|
+
provider: "openai",
|
|
20
|
+
modelId: "gpt-4o",
|
|
21
|
+
maxSteps: 50,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(strategy).toBeInstanceOf(AgentStrategy);
|
|
25
|
+
expect(strategy.name).toBe("agent");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("agent strategy works with openrouter nested paths", () => {
|
|
29
|
+
const strategy = agent<{ name: string }>({
|
|
30
|
+
provider: "openrouter",
|
|
31
|
+
modelId: "anthropic/claude-sonnet-4",
|
|
32
|
+
maxSteps: 50,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(strategy).toBeInstanceOf(AgentStrategy);
|
|
36
|
+
expect(strategy.name).toBe("agent");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("agent strategy uses default maxSteps", () => {
|
|
40
|
+
const strategy = agent<{ name: string }>({
|
|
41
|
+
provider: "anthropic",
|
|
42
|
+
modelId: "claude-sonnet-4",
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
expect(strategy.getEstimatedSteps()).toBe(50);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { test, expect } from "bun:test";
|
|
2
|
+
import { agent, AgentStrategy } from "@struktur/sdk";
|
|
3
|
+
|
|
4
|
+
test("agent strategy is exported from @struktur/sdk", () => {
|
|
5
|
+
expect(agent).toBeDefined();
|
|
6
|
+
expect(typeof agent).toBe("function");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("AgentStrategy class is exported from @struktur/sdk", () => {
|
|
10
|
+
const strategy = agent({
|
|
11
|
+
provider: "anthropic",
|
|
12
|
+
modelId: "claude-sonnet-4",
|
|
13
|
+
maxSteps: 10,
|
|
14
|
+
});
|
|
15
|
+
expect(strategy).toBeInstanceOf(AgentStrategy);
|
|
16
|
+
expect(strategy.name).toBe("agent");
|
|
17
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { test, expect, describe } from "bun:test";
|
|
2
|
+
import { agent, AgentStrategy } from "@struktur/sdk";
|
|
3
|
+
|
|
4
|
+
describe("Agent Strategy Tool Labels", () => {
|
|
5
|
+
test("strategy creates properly configured agent", () => {
|
|
6
|
+
const strategy = agent<{ title: string }>({
|
|
7
|
+
provider: "anthropic",
|
|
8
|
+
modelId: "claude-sonnet-4",
|
|
9
|
+
maxSteps: 25,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
expect(strategy).toBeDefined();
|
|
13
|
+
expect(strategy.name).toBe("agent");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("label format for read tool with file path only", () => {
|
|
17
|
+
const label = "Read artifact.json";
|
|
18
|
+
expect(label).toBe("Read artifact.json");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("label format for read tool with limit", () => {
|
|
22
|
+
const label = "Read artifact.json (limit 100)";
|
|
23
|
+
expect(label).toBe("Read artifact.json (limit 100)");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("label format for read tool with offset and limit", () => {
|
|
27
|
+
const label = "Read artifact.json (offset 101, limit 100)";
|
|
28
|
+
expect(label).toBe("Read artifact.json (offset 101, limit 100)");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("label format for bash command", () => {
|
|
32
|
+
const label = "Bash: cat /artifacts/artifact.json";
|
|
33
|
+
expect(label).toBe("Bash: cat /artifacts/artifact.json");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("label format for grep", () => {
|
|
37
|
+
const label = 'Grep "pattern" in artifact.json';
|
|
38
|
+
expect(label).toBe('Grep "pattern" in artifact.json');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("label format for find", () => {
|
|
42
|
+
const label = 'Find "*.json" in /artifacts';
|
|
43
|
+
expect(label).toBe('Find "*.json" in /artifacts');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("label format for ls", () => {
|
|
47
|
+
const label = "List /artifacts (recursive)";
|
|
48
|
+
expect(label).toBe("List /artifacts (recursive)");
|
|
49
|
+
});
|
|
50
|
+
});
|
package/src/artifacts/AGENTS.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Artifacts module
|
|
2
2
|
|
|
3
|
-
- Purpose: convert external inputs into
|
|
3
|
+
- Purpose: convert external inputs into Artifacts and manage parser configuration.
|
|
4
4
|
- Key files: `fileToArtifact.ts`, `urlToArtifact.ts`, `providers.ts`, `input.ts`.
|
|
5
5
|
- Design: `ParsersConfig` is the unified configuration system for all parsers (npm packages, CLI commands, and inline handlers). The `providers` registry is deprecated — use inline parsers in `ParsersConfig` instead.
|
|
6
6
|
- `parse` accepts an optional `parserConfig: ParsersConfig` (from `src/parsers/types`) that takes priority over the deprecated providers registry. Custom parsers are resolved by MIME type.
|
package/src/index.ts
CHANGED
|
@@ -45,6 +45,9 @@ export {
|
|
|
45
45
|
|
|
46
46
|
export * from "./strategies";
|
|
47
47
|
|
|
48
|
+
// Agent strategy (re-exported from @struktur/agent-strategy)
|
|
49
|
+
export { AgentStrategy, agent, type AgentStrategyConfig } from "@struktur/agent-strategy";
|
|
50
|
+
|
|
48
51
|
// Parsers public API
|
|
49
52
|
export { collectStream } from "./parsers/collect";
|
|
50
53
|
export type { ParserDef, ParsersConfig, InlineParserDef, NpmParserDef } from "./parsers/types";
|
|
@@ -62,6 +65,7 @@ export {
|
|
|
62
65
|
listProviderModels,
|
|
63
66
|
resolveCheapestModel,
|
|
64
67
|
} from "./llm/models";
|
|
68
|
+
export { resolveModel } from "./llm/resolveModel";
|
|
65
69
|
|
|
66
70
|
// Validation
|
|
67
71
|
export { SchemaValidationError } from "./validation/validator";
|
|
@@ -194,3 +194,201 @@ test("generateStructured uses custom schema description", async () => {
|
|
|
194
194
|
|
|
195
195
|
expect(calls[0]?.output).toHaveProperty("description", "Extract data");
|
|
196
196
|
});
|
|
197
|
+
|
|
198
|
+
test("generateStructured shows friendly error when model doesn't support images", async () => {
|
|
199
|
+
calls.length = 0;
|
|
200
|
+
generateTextImpl = async () => {
|
|
201
|
+
throw {
|
|
202
|
+
responseBody: '{"error":{"message":"No endpoints found that support image input","code":404}}',
|
|
203
|
+
statusCode: 404,
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
expect(
|
|
208
|
+
async () =>
|
|
209
|
+
await generateStructured({
|
|
210
|
+
model: { modelId: "meta-llama/llama-3.1-8b-instruct" },
|
|
211
|
+
schema: { type: "object" },
|
|
212
|
+
system: "sys",
|
|
213
|
+
user: [{ type: "text", text: "prompt" }, { type: "image", image: "base64data" }],
|
|
214
|
+
}),
|
|
215
|
+
).toThrow(
|
|
216
|
+
'Model "meta-llama/llama-3.1-8b-instruct" does not support image input. Please use a model that supports images (e.g., gpt-4o, claude-3-5-sonnet, gemini-1.5-pro) or remove the --images and --screenshots flags.',
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("generateStructured rethrows other API errors", async () => {
|
|
221
|
+
calls.length = 0;
|
|
222
|
+
const originalError = new Error("Some other error");
|
|
223
|
+
generateTextImpl = async () => {
|
|
224
|
+
throw originalError;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
expect(
|
|
228
|
+
async () =>
|
|
229
|
+
await generateStructured({
|
|
230
|
+
model: {},
|
|
231
|
+
schema: { type: "object" },
|
|
232
|
+
system: "sys",
|
|
233
|
+
user: "prompt",
|
|
234
|
+
}),
|
|
235
|
+
).toThrow("Some other error");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("generateStructured shows friendly error for internal server error", async () => {
|
|
239
|
+
calls.length = 0;
|
|
240
|
+
generateTextImpl = async () => {
|
|
241
|
+
throw {
|
|
242
|
+
statusCode: 200,
|
|
243
|
+
responseBody: undefined,
|
|
244
|
+
data: {
|
|
245
|
+
code: 500,
|
|
246
|
+
message: "Internal Server Error",
|
|
247
|
+
type: null,
|
|
248
|
+
param: null,
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
expect(
|
|
254
|
+
async () =>
|
|
255
|
+
await generateStructured({
|
|
256
|
+
model: { modelId: "openai/gpt-5-mini" },
|
|
257
|
+
schema: { type: "object" },
|
|
258
|
+
system: "sys",
|
|
259
|
+
user: "prompt",
|
|
260
|
+
}),
|
|
261
|
+
).toThrow(
|
|
262
|
+
'Provider error for model "openai/gpt-5-mini": Internal server error. The model or provider may be experiencing issues. Please try again or use a different model.',
|
|
263
|
+
);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("generateStructured shows friendly error for authentication failure", async () => {
|
|
267
|
+
calls.length = 0;
|
|
268
|
+
generateTextImpl = async () => {
|
|
269
|
+
throw {
|
|
270
|
+
statusCode: 401,
|
|
271
|
+
responseBody: '{"error":{"message":"Invalid API key"}}',
|
|
272
|
+
data: {
|
|
273
|
+
code: 401,
|
|
274
|
+
message: "Invalid API key",
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
expect(
|
|
280
|
+
async () =>
|
|
281
|
+
await generateStructured({
|
|
282
|
+
model: { modelId: "gpt-4o" },
|
|
283
|
+
schema: { type: "object" },
|
|
284
|
+
system: "sys",
|
|
285
|
+
user: "prompt",
|
|
286
|
+
}),
|
|
287
|
+
).toThrow(
|
|
288
|
+
'Authentication failed for model "gpt-4o". Please check your API key is valid and has the necessary permissions.',
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("generateStructured shows friendly error for rate limit", async () => {
|
|
293
|
+
calls.length = 0;
|
|
294
|
+
generateTextImpl = async () => {
|
|
295
|
+
throw {
|
|
296
|
+
statusCode: 429,
|
|
297
|
+
responseBody: '{"error":{"message":"Rate limit exceeded"}}',
|
|
298
|
+
data: {
|
|
299
|
+
code: 429,
|
|
300
|
+
message: "Rate limit exceeded",
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
expect(
|
|
306
|
+
async () =>
|
|
307
|
+
await generateStructured({
|
|
308
|
+
model: { modelId: "claude-3-5-sonnet" },
|
|
309
|
+
schema: { type: "object" },
|
|
310
|
+
system: "sys",
|
|
311
|
+
user: "prompt",
|
|
312
|
+
}),
|
|
313
|
+
).toThrow(
|
|
314
|
+
'Rate limit exceeded for model "claude-3-5-sonnet". Please wait a moment and try again, or use a different model.',
|
|
315
|
+
);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test("generateStructured shows friendly error for model not found", async () => {
|
|
319
|
+
calls.length = 0;
|
|
320
|
+
generateTextImpl = async () => {
|
|
321
|
+
throw {
|
|
322
|
+
statusCode: 404,
|
|
323
|
+
responseBody: '{"error":{"message":"Model not found"}}',
|
|
324
|
+
data: {
|
|
325
|
+
code: 404,
|
|
326
|
+
message: "Model not found",
|
|
327
|
+
},
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
expect(
|
|
332
|
+
async () =>
|
|
333
|
+
await generateStructured({
|
|
334
|
+
model: { modelId: "nonexistent-model" },
|
|
335
|
+
schema: { type: "object" },
|
|
336
|
+
system: "sys",
|
|
337
|
+
user: "prompt",
|
|
338
|
+
}),
|
|
339
|
+
).toThrow(
|
|
340
|
+
'Model "nonexistent-model" not found or unavailable. Model not found Please check the model name or try a different model.',
|
|
341
|
+
);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("generateStructured shows friendly error for access denied", async () => {
|
|
345
|
+
calls.length = 0;
|
|
346
|
+
generateTextImpl = async () => {
|
|
347
|
+
throw {
|
|
348
|
+
statusCode: 403,
|
|
349
|
+
responseBody: '{"error":{"message":"Access denied"}}',
|
|
350
|
+
data: {
|
|
351
|
+
code: 403,
|
|
352
|
+
message: "Access denied",
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
expect(
|
|
358
|
+
async () =>
|
|
359
|
+
await generateStructured({
|
|
360
|
+
model: { modelId: "gpt-4-turbo" },
|
|
361
|
+
schema: { type: "object" },
|
|
362
|
+
system: "sys",
|
|
363
|
+
user: "prompt",
|
|
364
|
+
}),
|
|
365
|
+
).toThrow(
|
|
366
|
+
'Access denied for model "gpt-4-turbo". Your API key may not have access to this model. Please check your subscription or try a different model.',
|
|
367
|
+
);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test("generateStructured shows generic provider error message", async () => {
|
|
371
|
+
calls.length = 0;
|
|
372
|
+
generateTextImpl = async () => {
|
|
373
|
+
throw {
|
|
374
|
+
statusCode: 400,
|
|
375
|
+
responseBody: '{"error":{"message":"Context length exceeded"}}',
|
|
376
|
+
data: {
|
|
377
|
+
code: 400,
|
|
378
|
+
message: "Context length exceeded",
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
expect(
|
|
384
|
+
async () =>
|
|
385
|
+
await generateStructured({
|
|
386
|
+
model: { modelId: "gpt-3.5-turbo" },
|
|
387
|
+
schema: { type: "object" },
|
|
388
|
+
system: "sys",
|
|
389
|
+
user: "prompt",
|
|
390
|
+
}),
|
|
391
|
+
).toThrow(
|
|
392
|
+
'Provider error for model "gpt-3.5-turbo": Context length exceeded',
|
|
393
|
+
);
|
|
394
|
+
});
|
package/src/llm/LLMClient.ts
CHANGED
|
@@ -61,26 +61,103 @@ export const generateStructured = async <T>(
|
|
|
61
61
|
}
|
|
62
62
|
: undefined;
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
64
|
+
let result;
|
|
65
|
+
try {
|
|
66
|
+
result = await generateText({
|
|
67
|
+
model: request.model as ModelType,
|
|
68
|
+
output: Output.object({
|
|
69
|
+
schema: schema as GenerateTextParams extends { schema: infer S }
|
|
70
|
+
? S
|
|
71
|
+
: never,
|
|
72
|
+
name: request.schemaName ?? "extract",
|
|
73
|
+
description: request.schemaDescription,
|
|
74
|
+
}),
|
|
75
|
+
providerOptions: {
|
|
76
|
+
openai: {
|
|
77
|
+
strictJsonSchema: request.strict ?? false,
|
|
78
|
+
},
|
|
76
79
|
},
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
})
|
|
80
|
+
system: request.system,
|
|
81
|
+
messages: (request.messages ?? [
|
|
82
|
+
{ role: "user", content: request.user },
|
|
83
|
+
]) as MessageType,
|
|
84
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
85
|
+
});
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (
|
|
88
|
+
error &&
|
|
89
|
+
typeof error === "object" &&
|
|
90
|
+
"responseBody" in error &&
|
|
91
|
+
"statusCode" in error
|
|
92
|
+
) {
|
|
93
|
+
const apiError = error as {
|
|
94
|
+
responseBody: unknown;
|
|
95
|
+
statusCode: number;
|
|
96
|
+
data?: {
|
|
97
|
+
code?: number;
|
|
98
|
+
message?: string;
|
|
99
|
+
type?: string | null;
|
|
100
|
+
param?: string | null;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const modelId =
|
|
105
|
+
typeof request.model === "object" && request.model !== null
|
|
106
|
+
? (request.model as { modelId?: string }).modelId ??
|
|
107
|
+
JSON.stringify(request.model)
|
|
108
|
+
: String(request.model);
|
|
109
|
+
|
|
110
|
+
const responseBody = apiError.responseBody;
|
|
111
|
+
const errorData = apiError.data;
|
|
112
|
+
|
|
113
|
+
if (
|
|
114
|
+
typeof responseBody === "string" &&
|
|
115
|
+
responseBody.includes("No endpoints found that support image input")
|
|
116
|
+
) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`Model "${modelId}" does not support image input. Please use a model that supports images (e.g., gpt-4o, claude-3-5-sonnet, gemini-1.5-pro) or remove the --images and --screenshots flags.`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (errorData?.code === 500 || errorData?.message?.includes("Internal Server Error")) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`Provider error for model "${modelId}": Internal server error. The model or provider may be experiencing issues. Please try again or use a different model.`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (apiError.statusCode === 401 || errorData?.code === 401) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Authentication failed for model "${modelId}". Please check your API key is valid and has the necessary permissions.`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (apiError.statusCode === 403 || errorData?.code === 403) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`Access denied for model "${modelId}". Your API key may not have access to this model. Please check your subscription or try a different model.`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (apiError.statusCode === 429 || errorData?.code === 429) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Rate limit exceeded for model "${modelId}". Please wait a moment and try again, or use a different model.`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (apiError.statusCode === 404 || errorData?.code === 404) {
|
|
147
|
+
const errorMsg = errorData?.message || "Model not found";
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Model "${modelId}" not found or unavailable. ${errorMsg} Please check the model name or try a different model.`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (errorData?.message) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`Provider error for model "${modelId}": ${errorData.message}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
84
161
|
|
|
85
162
|
const usageRaw = result.usage ?? {};
|
|
86
163
|
const inputTokens =
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { resolveProviderEnvVar, resolveProviderToken } from "../auth/tokens";
|
|
2
|
+
|
|
3
|
+
export const resolveModel = async (model: string) => {
|
|
4
|
+
(globalThis as { AI_SDK_LOG_WARNINGS?: boolean }).AI_SDK_LOG_WARNINGS ??= false;
|
|
5
|
+
process.env.AI_SDK_LOG_WARNINGS ??= "false";
|
|
6
|
+
const [provider, ...rest] = model.split("/");
|
|
7
|
+
const modelName = rest.join("/");
|
|
8
|
+
|
|
9
|
+
if (!provider || !modelName) {
|
|
10
|
+
throw new Error(`Invalid model format: ${model}. Expected format: provider/model (e.g., openai/gpt-4)`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const envVar = resolveProviderEnvVar(provider);
|
|
14
|
+
if (envVar && !process.env[envVar]) {
|
|
15
|
+
const storedToken = await resolveProviderToken(provider);
|
|
16
|
+
if (storedToken) {
|
|
17
|
+
process.env[envVar] = storedToken;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
switch (provider) {
|
|
22
|
+
case "openai": {
|
|
23
|
+
const { openai } = await import("@ai-sdk/openai");
|
|
24
|
+
return openai(modelName);
|
|
25
|
+
}
|
|
26
|
+
case "anthropic": {
|
|
27
|
+
const { anthropic } = await import("@ai-sdk/anthropic");
|
|
28
|
+
return anthropic(modelName);
|
|
29
|
+
}
|
|
30
|
+
case "google": {
|
|
31
|
+
const { google } = await import("@ai-sdk/google");
|
|
32
|
+
return google(modelName);
|
|
33
|
+
}
|
|
34
|
+
case "opencode": {
|
|
35
|
+
const envVar = resolveProviderEnvVar("opencode");
|
|
36
|
+
let apiKey = envVar ? process.env[envVar] : undefined;
|
|
37
|
+
if (!apiKey) {
|
|
38
|
+
apiKey = await resolveProviderToken("opencode");
|
|
39
|
+
}
|
|
40
|
+
if (!apiKey) {
|
|
41
|
+
throw new Error("OpenCode API key is required. Set OPENCODE_API_KEY environment variable or run 'struktur auth set --provider opencode --token <token>'");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (modelName.startsWith("claude-")) {
|
|
45
|
+
const { createAnthropic } = await import("@ai-sdk/anthropic");
|
|
46
|
+
return createAnthropic({
|
|
47
|
+
apiKey,
|
|
48
|
+
baseURL: "https://opencode.ai/zen/v1",
|
|
49
|
+
})(modelName);
|
|
50
|
+
} else if (modelName.startsWith("gemini-")) {
|
|
51
|
+
const { createGoogleGenerativeAI } = await import("@ai-sdk/google");
|
|
52
|
+
return createGoogleGenerativeAI({
|
|
53
|
+
apiKey,
|
|
54
|
+
baseURL: "https://opencode.ai/zen/v1",
|
|
55
|
+
})(modelName);
|
|
56
|
+
} else {
|
|
57
|
+
const { createOpenAI } = await import("@ai-sdk/openai");
|
|
58
|
+
return createOpenAI({
|
|
59
|
+
apiKey,
|
|
60
|
+
baseURL: "https://opencode.ai/zen/v1",
|
|
61
|
+
})(modelName);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
case "openrouter": {
|
|
65
|
+
const { openrouter } = await import("@openrouter/ai-sdk-provider");
|
|
66
|
+
const hashIndex = modelName.indexOf("#");
|
|
67
|
+
const actualModelName = hashIndex >= 0 ? modelName.slice(0, hashIndex) : modelName;
|
|
68
|
+
const preferredProvider = hashIndex >= 0 ? modelName.slice(hashIndex + 1) : undefined;
|
|
69
|
+
|
|
70
|
+
const modelInstance = openrouter(actualModelName);
|
|
71
|
+
|
|
72
|
+
if (preferredProvider) {
|
|
73
|
+
Object.defineProperty(modelInstance, "__openrouter_provider", {
|
|
74
|
+
value: preferredProvider,
|
|
75
|
+
writable: false,
|
|
76
|
+
enumerable: false,
|
|
77
|
+
configurable: false,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return modelInstance;
|
|
82
|
+
}
|
|
83
|
+
default:
|
|
84
|
+
throw new Error(`Unsupported model provider: ${provider}. Supported providers: openai, anthropic, google, opencode, openrouter`);
|
|
85
|
+
}
|
|
86
|
+
};
|
package/src/types.ts
CHANGED
package/src/validation/AGENTS.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
Validation module
|
|
2
2
|
|
|
3
|
-
- Purpose:
|
|
3
|
+
- Purpose: Schema validation and error shaping.
|
|
4
4
|
- Key files: `validator.ts`.
|
|
5
|
-
- Design: `validateOrThrow` compiles schemas and throws `SchemaValidationError` on failure; `createAjv` registers `ajv-formats` for common schema formats.
|
|
5
|
+
- Design: `validateOrThrow` compiles schemas and throws `SchemaValidationError` on failure; `createAjv` registers `ajv-formats` for common schema formats and adds custom `artifact-id` format for referencing images in artifacts.
|
|
6
|
+
- Custom formats: `artifact-id` validates strings matching pattern `artifact:ID/images/imageNUM.EXT` (e.g., `artifact:123456/images/image1.jpg`).
|
|
6
7
|
- Tests: `validator.test.ts`.
|
|
@@ -82,6 +82,38 @@ test("createAjv supports common formats", () => {
|
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
+
test("createAjv supports artifact-id format", () => {
|
|
86
|
+
const ajv = createAjv();
|
|
87
|
+
const schema: JSONSchemaType<string> = { type: "string", format: "artifact-id" };
|
|
88
|
+
|
|
89
|
+
const validData = validateOrThrow<string>(ajv, schema, "artifact:123456/images/image1.jpg");
|
|
90
|
+
expect(validData).toBe("artifact:123456/images/image1.jpg");
|
|
91
|
+
|
|
92
|
+
const validData2 = validateOrThrow<string>(ajv, schema, "artifact:abc-xyz/images/image10.png");
|
|
93
|
+
expect(validData2).toBe("artifact:abc-xyz/images/image10.png");
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
validateOrThrow<string>(ajv, schema, "not-an-artifact-id");
|
|
97
|
+
throw new Error("Expected validation error");
|
|
98
|
+
} catch (error) {
|
|
99
|
+
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
validateOrThrow<string>(ajv, schema, "artifact:123/images/image");
|
|
104
|
+
throw new Error("Expected validation error");
|
|
105
|
+
} catch (error) {
|
|
106
|
+
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
validateOrThrow<string>(ajv, schema, "https://example.com/image.jpg");
|
|
111
|
+
throw new Error("Expected validation error");
|
|
112
|
+
} catch (error) {
|
|
113
|
+
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
85
117
|
test("isRequiredError identifies required constraint violations", () => {
|
|
86
118
|
const requiredError = {
|
|
87
119
|
keyword: "required",
|
|
@@ -19,6 +19,8 @@ export class SchemaValidationError extends Error {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
const ARTIFACT_ID_PATTERN = /^artifact:[^/]+\/images\/image\d+\.\w+$/;
|
|
23
|
+
|
|
22
24
|
export const createAjv = () => {
|
|
23
25
|
const ajv = new Ajv({
|
|
24
26
|
allErrors: true,
|
|
@@ -26,6 +28,12 @@ export const createAjv = () => {
|
|
|
26
28
|
allowUnionTypes: true,
|
|
27
29
|
});
|
|
28
30
|
addFormats(ajv);
|
|
31
|
+
|
|
32
|
+
ajv.addFormat("artifact-id", {
|
|
33
|
+
type: "string",
|
|
34
|
+
validate: (data: string) => ARTIFACT_ID_PATTERN.test(data),
|
|
35
|
+
});
|
|
36
|
+
|
|
29
37
|
return ajv;
|
|
30
38
|
};
|
|
31
39
|
|