@struktur/sdk 1.2.1 → 2.1.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 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, simple, urlToArtifact } from "@struktur/sdk";
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: simple({ model: openai("gpt-4o-mini") }),
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": "1.2.1",
3
+ "version": "2.1.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": "2.1.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
+ });
@@ -1,6 +1,6 @@
1
1
  Artifacts module
2
2
 
3
- - Purpose: convert external inputs into Artifact DTOs and manage parser configuration.
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.
@@ -3,11 +3,24 @@ import os from "node:os";
3
3
  import { chmod, mkdir } from "node:fs/promises";
4
4
  import type { ParserDef, ParsersConfig } from "@struktur/sdk";
5
5
 
6
+ type TelemetryConfig = {
7
+ enabled: boolean;
8
+ provider: string;
9
+ url?: string;
10
+ apiKey?: string;
11
+ projectName?: string;
12
+ publicKey?: string; // For Langfuse
13
+ secretKey?: string; // For Langfuse
14
+ baseUrl?: string; // For Langfuse
15
+ sampleRate?: number;
16
+ };
17
+
6
18
  type ConfigStore = {
7
19
  version: 1;
8
20
  defaultModel?: string;
9
21
  aliases?: Record<string, string>;
10
22
  parsers?: ParsersConfig;
23
+ telemetry?: TelemetryConfig;
11
24
  };
12
25
 
13
26
  const CONFIG_DIR_ENV = "STRUKTUR_CONFIG_DIR";
@@ -127,3 +140,47 @@ export const deleteParser = async (mimeType: string): Promise<boolean> => {
127
140
  await writeConfigStore(store);
128
141
  return true;
129
142
  };
143
+
144
+ // --- Telemetry config management ---
145
+
146
+ export const getTelemetryConfig = async (): Promise<TelemetryConfig | undefined> => {
147
+ const store = await readConfigStore();
148
+ return store.telemetry;
149
+ };
150
+
151
+ export const setTelemetryConfig = async (config: TelemetryConfig): Promise<void> => {
152
+ const store = await readConfigStore();
153
+ store.telemetry = config;
154
+ await writeConfigStore(store);
155
+ };
156
+
157
+ export const enableTelemetry = async (
158
+ provider: string,
159
+ options: Omit<TelemetryConfig, "enabled" | "provider">
160
+ ): Promise<void> => {
161
+ const store = await readConfigStore();
162
+ store.telemetry = {
163
+ enabled: true,
164
+ provider,
165
+ ...options,
166
+ };
167
+ await writeConfigStore(store);
168
+ };
169
+
170
+ export const disableTelemetry = async (): Promise<void> => {
171
+ const store = await readConfigStore();
172
+ if (store.telemetry) {
173
+ store.telemetry.enabled = false;
174
+ }
175
+ await writeConfigStore(store);
176
+ };
177
+
178
+ export const deleteTelemetryConfig = async (): Promise<boolean> => {
179
+ const store = await readConfigStore();
180
+ if (!store.telemetry) {
181
+ return false;
182
+ }
183
+ delete store.telemetry;
184
+ await writeConfigStore(store);
185
+ return true;
186
+ };
package/src/extract.ts CHANGED
@@ -34,29 +34,52 @@ export const extract = async <T>(
34
34
  options: ExtractionOptions<T>,
35
35
  ): Promise<ExtractionResult<T>> => {
36
36
  const debug = options.debug;
37
+ const telemetry = options.telemetry;
37
38
 
38
- // Validate mutual exclusion and resolve the concrete schema early so that
39
- // every strategy receives a fully-populated options object.
40
- let resolvedOptions: ExtractionOptions<T>;
41
- try {
42
- const schema = resolveSchema(options);
43
- resolvedOptions = { ...options, schema };
44
- } catch (error) {
45
- debug?.extractionComplete({
46
- success: false,
47
- totalInputTokens: 0,
48
- totalOutputTokens: 0,
49
- totalTokens: 0,
50
- error: (error as Error).message,
51
- });
52
- return {
53
- data: null as unknown as T,
54
- usage: emptyUsage,
55
- error: error as Error,
56
- };
39
+ // Initialize telemetry if provided
40
+ if (telemetry) {
41
+ await telemetry.initialize();
57
42
  }
58
43
 
44
+ // Start root extraction span
45
+ const rootSpan = telemetry?.startSpan({
46
+ name: "struktur.extract",
47
+ kind: "CHAIN",
48
+ attributes: {
49
+ "extraction.strategy": options.strategy?.name ?? "default",
50
+ "extraction.artifacts.count": options.artifacts.length,
51
+ },
52
+ });
53
+
59
54
  try {
55
+ // Validate mutual exclusion and resolve the concrete schema early so that
56
+ // every strategy receives a fully-populated options object.
57
+ let resolvedOptions: ExtractionOptions<T>;
58
+ try {
59
+ const schema = resolveSchema(options);
60
+ resolvedOptions = { ...options, schema };
61
+ } catch (error) {
62
+ debug?.extractionComplete({
63
+ success: false,
64
+ totalInputTokens: 0,
65
+ totalOutputTokens: 0,
66
+ totalTokens: 0,
67
+ error: (error as Error).message,
68
+ });
69
+
70
+ telemetry?.endSpan(rootSpan!, {
71
+ status: "error",
72
+ error: error as Error,
73
+ });
74
+ await telemetry?.shutdown();
75
+
76
+ return {
77
+ data: null as unknown as T,
78
+ usage: emptyUsage,
79
+ error: error as Error,
80
+ };
81
+ }
82
+
60
83
  const total = resolvedOptions.strategy.getEstimatedSteps?.(resolvedOptions.artifacts);
61
84
 
62
85
  debug?.strategyRunStart({
@@ -95,6 +118,13 @@ export const extract = async <T>(
95
118
  error: result.error?.message,
96
119
  });
97
120
 
121
+ telemetry?.endSpan(rootSpan!, {
122
+ status: result.error ? "error" : "ok",
123
+ output: result.data,
124
+ error: result.error,
125
+ });
126
+ await telemetry?.shutdown();
127
+
98
128
  return result;
99
129
  } catch (error) {
100
130
  debug?.extractionComplete({
@@ -105,6 +135,12 @@ export const extract = async <T>(
105
135
  error: (error as Error).message,
106
136
  });
107
137
 
138
+ telemetry?.endSpan(rootSpan!, {
139
+ status: "error",
140
+ error: error as Error,
141
+ });
142
+ await telemetry?.shutdown();
143
+
108
144
  return {
109
145
  data: null as unknown as T,
110
146
  usage: emptyUsage,
package/src/index.ts CHANGED
@@ -10,6 +10,14 @@ export type {
10
10
  Usage,
11
11
  AnyJSONSchema,
12
12
  TypedJSONSchema,
13
+ // Agent event types
14
+ AgentEvents,
15
+ AgentToolStartInfo,
16
+ AgentToolEndInfo,
17
+ AgentMessageInfo,
18
+ AgentReasoningInfo,
19
+ // Telemetry
20
+ TelemetryAdapter,
13
21
  } from "./types";
14
22
 
15
23
  export { extract } from "./extract";
@@ -45,6 +53,9 @@ export {
45
53
 
46
54
  export * from "./strategies";
47
55
 
56
+ // Agent strategy (re-exported from @struktur/agent-strategy)
57
+ export { AgentStrategy, agent, type AgentStrategyConfig } from "@struktur/agent-strategy";
58
+
48
59
  // Parsers public API
49
60
  export { collectStream } from "./parsers/collect";
50
61
  export type { ParserDef, ParsersConfig, InlineParserDef, NpmParserDef } from "./parsers/types";
@@ -62,6 +73,7 @@ export {
62
73
  listProviderModels,
63
74
  resolveCheapestModel,
64
75
  } from "./llm/models";
76
+ export { resolveModel } from "./llm/resolveModel";
65
77
 
66
78
  // Validation
67
79
  export { SchemaValidationError } from "./validation/validator";
@@ -79,6 +91,11 @@ export {
79
91
  getParser,
80
92
  setParser,
81
93
  deleteParser,
94
+ getTelemetryConfig,
95
+ setTelemetryConfig,
96
+ enableTelemetry,
97
+ disableTelemetry,
98
+ deleteTelemetryConfig,
82
99
  } from "./auth/config";
83
100
  export {
84
101
  listStoredProviders,