extrait 0.1.0 → 0.1.2

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 CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  Structured JSON extraction from LLMs with validation, repair, and streaming.
4
4
 
5
- Features:
5
+ <p align="left">
6
+ <a href="https://www.npmjs.com/package/extrait">
7
+ <img src="https://img.shields.io/npm/v/extrait?logo=npm&style=plastic" alt="npm package">
8
+ </a>
9
+ </p>
10
+
11
+ **Features:**
6
12
  - Multi-candidate JSON extraction from LLM responses
7
13
  - Automatic repair with jsonrepair
8
14
  - Zod schema validation and coercion
@@ -13,7 +19,11 @@ Features:
13
19
  ## Installation
14
20
 
15
21
  ```bash
16
- bun install
22
+ bun add extrait
23
+ # or
24
+ npm install extrait
25
+ # or
26
+ deno add npm:extrait
17
27
  ```
18
28
 
19
29
  ## Quick Start
@@ -205,6 +215,10 @@ const result = await llm.structured(
205
215
  onToolExecution: (execution) => {
206
216
  console.log(execution.name, execution.durationMs);
207
217
  },
218
+ // Optional: transform tool output before it is sent back to the LLM
219
+ transformToolOutput: (output, execution) => {
220
+ return { ...output, source: execution.name };
221
+ },
208
222
  },
209
223
  }
210
224
  );
package/dist/format.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { z } from "zod";
2
2
  export interface WithFormatOptions {
3
3
  schemaInstruction?: string;
4
4
  }
5
- export declare const DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this schema:";
5
+ export declare const DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this JSON schema:";
6
6
  export declare function withFormat(schema: z.ZodTypeAny, options?: WithFormatOptions): string;
7
7
  export declare function formatPrompt(schema: z.ZodTypeAny, task: string, options?: WithFormatOptions): string;
8
8
  export declare function resolveSchemaInstruction(instruction: string | undefined): string;
package/dist/index.cjs CHANGED
@@ -976,7 +976,7 @@ function sanitizeDescription(value) {
976
976
  }
977
977
 
978
978
  // src/format.ts
979
- var DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this schema:";
979
+ var DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this JSON schema:";
980
980
  function withFormat(schema, options = {}) {
981
981
  const schemaType = formatZodSchemaLikeTypeScript(schema);
982
982
  const instruction = resolveSchemaInstruction(options.schemaInstruction);
@@ -1301,20 +1301,25 @@ async function executeMCPToolCalls(calls, toolset, context) {
1301
1301
  name: tool.remoteName,
1302
1302
  arguments: args
1303
1303
  });
1304
- metadata.output = output;
1305
- const execution = {
1304
+ const executionContext = {
1306
1305
  callId,
1307
- type: metadata.type,
1306
+ type: call.type ?? "function",
1308
1307
  name: toolName,
1309
1308
  clientId: tool.clientId,
1310
1309
  remoteName: tool.remoteName,
1311
1310
  arguments: parsedArguments,
1312
- output,
1313
1311
  round: context.round,
1314
1312
  provider: context.provider,
1315
1313
  model: context.model,
1316
1314
  handledLocally: true,
1317
1315
  startedAt,
1316
+ error: undefined
1317
+ };
1318
+ const transformedOutput = context.request.transformToolOutput ? await context.request.transformToolOutput(output, executionContext) : output;
1319
+ metadata.output = transformedOutput;
1320
+ const execution = {
1321
+ ...executionContext,
1322
+ output: transformedOutput,
1318
1323
  durationMs: Date.now() - startedAtMs
1319
1324
  };
1320
1325
  emitToolExecution(context.request, execution);
package/dist/index.d.ts CHANGED
@@ -12,4 +12,4 @@ export { createOpenAICompatibleAdapter, type OpenAICompatibleAdapterOptions, } f
12
12
  export { createAnthropicCompatibleAdapter, DEFAULT_ANTHROPIC_MAX_TOKENS, DEFAULT_ANTHROPIC_VERSION, type AnthropicCompatibleAdapterOptions, } from "./providers/anthropic-compatible";
13
13
  export { DEFAULT_MAX_TOOL_ROUNDS } from "./providers/mcp-runtime";
14
14
  export { createDefaultProviderRegistry, createModelAdapter, createProviderRegistry, registerBuiltinProviders, type BuiltinProviderKind, type ModelAdapterConfig, type ProviderFactory, type ProviderRegistry, type ProviderTransportConfig, } from "./providers/registry";
15
- export type { CandidateDiagnostics, ExtractJsonCandidatesOptions, ExtractionCandidate, ExtractionHeuristicsOptions, ExtractionParseHint, HTTPHeaders, LLMAdapter, LLMRequest, LLMResponse, LLMStreamCallbacks, LLMStreamChunk, LLMToolCall, LLMToolDebugOptions, LLMToolExecution, LLMToolChoice, MCPCallToolParams, MCPListToolsResult, MCPToolClient, MCPToolDescriptor, MCPToolSchema, LLMUsage, MarkdownCodeBlock, MarkdownCodeOptions, ParseLLMOutputOptions, ParseLLMOutputResult, ParseTraceEvent, PipelineError, StructuredAttempt, StructuredCallOptions, StructuredDebugOptions, StructuredError, StructuredMode, StructuredOptions, StructuredPromptBuilder, StructuredPromptContext, StructuredPromptPayload, StructuredPromptResolver, StructuredPromptValue, StructuredResult, StructuredStreamData, StructuredStreamEvent, StructuredStreamInput, StructuredStreamOptions, StructuredSelfHealInput, ThinkDiagnostics, ThinkBlock, StructuredTraceEvent, } from "./types";
15
+ export type { CandidateDiagnostics, ExtractJsonCandidatesOptions, ExtractionCandidate, ExtractionHeuristicsOptions, ExtractionParseHint, HTTPHeaders, LLMAdapter, LLMRequest, LLMResponse, LLMStreamCallbacks, LLMStreamChunk, LLMToolCall, LLMToolDebugOptions, LLMToolExecution, LLMToolOutputTransformer, LLMToolChoice, MCPCallToolParams, MCPListToolsResult, MCPToolClient, MCPToolDescriptor, MCPToolSchema, LLMUsage, MarkdownCodeBlock, MarkdownCodeOptions, ParseLLMOutputOptions, ParseLLMOutputResult, ParseTraceEvent, PipelineError, StructuredAttempt, StructuredCallOptions, StructuredDebugOptions, StructuredError, StructuredMode, StructuredOptions, StructuredPromptBuilder, StructuredPromptContext, StructuredPromptPayload, StructuredPromptResolver, StructuredPromptValue, StructuredResult, StructuredStreamData, StructuredStreamEvent, StructuredStreamInput, StructuredStreamOptions, StructuredSelfHealInput, ThinkDiagnostics, ThinkBlock, StructuredTraceEvent, } from "./types";
package/dist/index.js CHANGED
@@ -897,7 +897,7 @@ function sanitizeDescription(value) {
897
897
  }
898
898
 
899
899
  // src/format.ts
900
- var DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this schema:";
900
+ var DEFAULT_SCHEMA_INSTRUCTION = "Strictly follow this JSON schema:";
901
901
  function withFormat(schema, options = {}) {
902
902
  const schemaType = formatZodSchemaLikeTypeScript(schema);
903
903
  const instruction = resolveSchemaInstruction(options.schemaInstruction);
@@ -1222,20 +1222,25 @@ async function executeMCPToolCalls(calls, toolset, context) {
1222
1222
  name: tool.remoteName,
1223
1223
  arguments: args
1224
1224
  });
1225
- metadata.output = output;
1226
- const execution = {
1225
+ const executionContext = {
1227
1226
  callId,
1228
- type: metadata.type,
1227
+ type: call.type ?? "function",
1229
1228
  name: toolName,
1230
1229
  clientId: tool.clientId,
1231
1230
  remoteName: tool.remoteName,
1232
1231
  arguments: parsedArguments,
1233
- output,
1234
1232
  round: context.round,
1235
1233
  provider: context.provider,
1236
1234
  model: context.model,
1237
1235
  handledLocally: true,
1238
1236
  startedAt,
1237
+ error: undefined
1238
+ };
1239
+ const transformedOutput = context.request.transformToolOutput ? await context.request.transformToolOutput(output, executionContext) : output;
1240
+ metadata.output = transformedOutput;
1241
+ const execution = {
1242
+ ...executionContext,
1243
+ output: transformedOutput,
1239
1244
  durationMs: Date.now() - startedAtMs
1240
1245
  };
1241
1246
  emitToolExecution(context.request, execution);
package/dist/types.d.ts CHANGED
@@ -129,6 +129,7 @@ export interface LLMRequest {
129
129
  parallelToolCalls?: boolean;
130
130
  maxToolRounds?: number;
131
131
  onToolExecution?: (execution: LLMToolExecution) => void;
132
+ transformToolOutput?: LLMToolOutputTransformer;
132
133
  toolDebug?: boolean | LLMToolDebugOptions;
133
134
  body?: Record<string, unknown>;
134
135
  }
@@ -189,6 +190,7 @@ export interface LLMToolExecution {
189
190
  startedAt: string;
190
191
  durationMs?: number;
191
192
  }
193
+ export type LLMToolOutputTransformer = (output: unknown, execution: Omit<LLMToolExecution, "output" | "durationMs">) => unknown | Promise<unknown>;
192
194
  export interface LLMToolDebugOptions {
193
195
  enabled?: boolean;
194
196
  logger?: (line: string) => void;
package/package.json CHANGED
@@ -1,7 +1,14 @@
1
1
  {
2
2
  "name": "extrait",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/tterrasson/extrait.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/tterrasson/extrait/issues"
11
+ },
5
12
  "main": "./dist/index.cjs",
6
13
  "module": "./dist/index.js",
7
14
  "types": "./dist/index.d.ts",
@@ -27,16 +34,16 @@
27
34
  "build:types": "bunx tsc -p tsconfig.build.json",
28
35
  "lint": "bunx tsc -p tsconfig.lint.json",
29
36
  "prepublishOnly": "bun run lint && bun run build && bun run build:types",
30
- "test": "bun test",
37
+ "test": "bun test tests/ --reporter=dots --only-failures",
31
38
  "typecheck": "bunx tsc --noEmit"
32
39
  },
33
40
  "dependencies": {
34
41
  "@modelcontextprotocol/sdk": "^1.26.0",
35
- "jsonrepair": "^3.13.1",
36
- "zod": "^3.24.2"
42
+ "jsonrepair": "^3.13.2",
43
+ "zod": "^3.25.76"
37
44
  },
38
45
  "devDependencies": {
39
46
  "@types/bun": "latest",
40
- "typescript": "^5"
47
+ "typescript": "^5.9.3"
41
48
  }
42
49
  }