@zhanla/sdk-ts 0.3.2 → 0.3.4
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/bin/cli.js +5 -0
- package/bin/run.js +1 -0
- package/dist/executor.d.ts +2 -0
- package/dist/executor.js +50 -17
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +7 -2
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -41,6 +41,11 @@ if (subcommand === "discover") {
|
|
|
41
41
|
}
|
|
42
42
|
const { runDiscover } = await import("./discover.js");
|
|
43
43
|
await runDiscover(filePath);
|
|
44
|
+
} else if (subcommand === "version") {
|
|
45
|
+
const { createRequire } = await import("module");
|
|
46
|
+
const require = createRequire(import.meta.url);
|
|
47
|
+
const { version } = require("../package.json");
|
|
48
|
+
process.stdout.write(version + "\n");
|
|
44
49
|
} else if (subcommand === "run") {
|
|
45
50
|
const [target, ...rest] = args;
|
|
46
51
|
if (!target) {
|
package/bin/run.js
CHANGED
|
@@ -130,6 +130,7 @@ export async function runComponent(target, evalTarget = null) {
|
|
|
130
130
|
status: result.status,
|
|
131
131
|
output,
|
|
132
132
|
pathTaken: result.pathTaken,
|
|
133
|
+
component_llm_calls: result.componentLlmCalls,
|
|
133
134
|
eval_output: evalOutput,
|
|
134
135
|
error: result.status === "error" ? result.error : undefined,
|
|
135
136
|
eval_error: result.status === "ok" ? result.error : undefined,
|
package/dist/executor.d.ts
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
* Mirrors the Python CLI executor's component-type dispatch semantics.
|
|
4
4
|
*/
|
|
5
5
|
import { ZhanlaComponent } from "./types.js";
|
|
6
|
+
import { LLMCall } from "./trace_store.js";
|
|
6
7
|
export interface RowResult {
|
|
7
8
|
status: "ok" | "error";
|
|
8
9
|
output?: Record<string, unknown>;
|
|
9
10
|
error?: string;
|
|
10
11
|
pathTaken?: Record<string, unknown>;
|
|
12
|
+
componentLlmCalls?: LLMCall[];
|
|
11
13
|
}
|
|
12
14
|
export declare function executeComponent(comp: ZhanlaComponent, kwargs: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
13
15
|
export declare function executeRow(component: ZhanlaComponent, evalComponent: ZhanlaComponent | null, row: Record<string, unknown>): Promise<RowResult>;
|
package/dist/executor.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
* Local execution engine for TypeScript SDK components.
|
|
3
3
|
* Mirrors the Python CLI executor's component-type dispatch semantics.
|
|
4
4
|
*/
|
|
5
|
+
import { randomUUID } from "crypto";
|
|
5
6
|
import { parseJsonResponse } from "./json.js";
|
|
7
|
+
import { TraceContext, traceStorage } from "./trace_store.js";
|
|
6
8
|
const COMPONENT_ERROR_KEY = "_component_error";
|
|
7
9
|
const OUTPUT_SCHEMA_MISMATCH_REASON = "Model response doesn't match the requested output schema.";
|
|
8
10
|
const JSON_OUTPUT_ERROR_MARKERS = [
|
|
@@ -69,6 +71,12 @@ function stringifyValue(value) {
|
|
|
69
71
|
return String(value);
|
|
70
72
|
}
|
|
71
73
|
}
|
|
74
|
+
function deriveModelInput(row) {
|
|
75
|
+
if ("input" in row) {
|
|
76
|
+
return stringifyValue(row["input"]);
|
|
77
|
+
}
|
|
78
|
+
return stringifyValue(row);
|
|
79
|
+
}
|
|
72
80
|
function deriveExpectedOutput(row) {
|
|
73
81
|
if ("expected_output" in row) {
|
|
74
82
|
return stringifyValue(row["expected_output"]);
|
|
@@ -76,9 +84,12 @@ function deriveExpectedOutput(row) {
|
|
|
76
84
|
if ("output" in row) {
|
|
77
85
|
return stringifyValue(row["output"]);
|
|
78
86
|
}
|
|
79
|
-
return
|
|
87
|
+
return stringifyValue(row);
|
|
80
88
|
}
|
|
81
89
|
function deriveModelResponse(output) {
|
|
90
|
+
if (output == null) {
|
|
91
|
+
return stringifyValue(output);
|
|
92
|
+
}
|
|
82
93
|
const keys = Object.keys(output);
|
|
83
94
|
if (keys.length === 1 && keys[0] === "result") {
|
|
84
95
|
return stringifyValue(output["result"]);
|
|
@@ -88,6 +99,13 @@ function deriveModelResponse(output) {
|
|
|
88
99
|
}
|
|
89
100
|
return stringifyValue(output);
|
|
90
101
|
}
|
|
102
|
+
function buildCodeEvalKwargs(output, row) {
|
|
103
|
+
return {
|
|
104
|
+
model_input: deriveModelInput(row),
|
|
105
|
+
model_response: deriveModelResponse(output),
|
|
106
|
+
expected_output: deriveExpectedOutput(row),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
91
109
|
// ---------------------------------------------------------------------------
|
|
92
110
|
// Component execution dispatch
|
|
93
111
|
// ---------------------------------------------------------------------------
|
|
@@ -156,13 +174,11 @@ async function runTool(comp, kwargs) {
|
|
|
156
174
|
return { result };
|
|
157
175
|
}
|
|
158
176
|
async function runCodeEval(comp, kwargs) {
|
|
159
|
-
const modelResponse = kwargs["model_response"];
|
|
160
|
-
const expectedResponse = kwargs["expected_response"] ?? null;
|
|
161
177
|
const { value: result, logs } = await captureConsoleLogs(async () => {
|
|
162
178
|
if (comp.isAsync) {
|
|
163
|
-
return await comp.fn(
|
|
179
|
+
return await comp.fn(kwargs);
|
|
164
180
|
}
|
|
165
|
-
return comp.fn(
|
|
181
|
+
return comp.fn(kwargs);
|
|
166
182
|
});
|
|
167
183
|
if (result !== null && typeof result === "object" && !Array.isArray(result)) {
|
|
168
184
|
return logs.length > 0
|
|
@@ -528,14 +544,19 @@ export async function executeRow(component, evalComponent, row) {
|
|
|
528
544
|
let error;
|
|
529
545
|
let demotedError;
|
|
530
546
|
let orchestrationSteps;
|
|
547
|
+
let componentLlmCalls;
|
|
548
|
+
let componentFailedWithoutEval = false;
|
|
549
|
+
const traceContext = new TraceContext(randomUUID());
|
|
531
550
|
try {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
551
|
+
await traceStorage.run(traceContext, async () => {
|
|
552
|
+
if (component.componentType === "orchestration") {
|
|
553
|
+
orchestrationSteps = [];
|
|
554
|
+
output = await runOrchestration(component, row, orchestrationSteps);
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
output = await executeComponent(component, row);
|
|
558
|
+
}
|
|
559
|
+
});
|
|
539
560
|
}
|
|
540
561
|
catch (err) {
|
|
541
562
|
error = err instanceof Error ? err.message : String(err);
|
|
@@ -545,9 +566,20 @@ export async function executeRow(component, evalComponent, row) {
|
|
|
545
566
|
error = undefined;
|
|
546
567
|
}
|
|
547
568
|
else {
|
|
548
|
-
|
|
569
|
+
componentFailedWithoutEval = true;
|
|
549
570
|
}
|
|
550
571
|
}
|
|
572
|
+
finally {
|
|
573
|
+
componentLlmCalls = traceContext.flush();
|
|
574
|
+
}
|
|
575
|
+
if (componentFailedWithoutEval) {
|
|
576
|
+
return {
|
|
577
|
+
status: "error",
|
|
578
|
+
error,
|
|
579
|
+
pathTaken: buildPathTaken(component, row, undefined, error, orchestrationSteps),
|
|
580
|
+
componentLlmCalls,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
551
583
|
if (evalComponent != null) {
|
|
552
584
|
if (output != null
|
|
553
585
|
&& typeof output[COMPONENT_ERROR_KEY] === "string"
|
|
@@ -556,18 +588,17 @@ export async function executeRow(component, evalComponent, row) {
|
|
|
556
588
|
status: "ok",
|
|
557
589
|
output: { ...output, _eval: buildSchemaMismatchEvalOutput(String(output[COMPONENT_ERROR_KEY])) },
|
|
558
590
|
pathTaken: buildPathTaken(component, row, output, demotedError, orchestrationSteps),
|
|
591
|
+
componentLlmCalls,
|
|
559
592
|
};
|
|
560
593
|
}
|
|
561
|
-
const evalKwargs =
|
|
562
|
-
model_response: deriveModelResponse(output),
|
|
563
|
-
expected_response: deriveExpectedOutput(row) || null,
|
|
564
|
-
};
|
|
594
|
+
const evalKwargs = buildCodeEvalKwargs(output, row);
|
|
565
595
|
try {
|
|
566
596
|
const evalOutput = await executeComponent(evalComponent, evalKwargs);
|
|
567
597
|
return {
|
|
568
598
|
status: "ok",
|
|
569
599
|
output: { ...output, _eval: evalOutput },
|
|
570
600
|
pathTaken: buildPathTaken(component, row, output, demotedError, orchestrationSteps),
|
|
601
|
+
componentLlmCalls,
|
|
571
602
|
};
|
|
572
603
|
}
|
|
573
604
|
catch (err) {
|
|
@@ -577,6 +608,7 @@ export async function executeRow(component, evalComponent, row) {
|
|
|
577
608
|
output,
|
|
578
609
|
error: evalError,
|
|
579
610
|
pathTaken: buildPathTaken(component, row, output, demotedError, orchestrationSteps),
|
|
611
|
+
componentLlmCalls,
|
|
580
612
|
};
|
|
581
613
|
}
|
|
582
614
|
}
|
|
@@ -584,6 +616,7 @@ export async function executeRow(component, evalComponent, row) {
|
|
|
584
616
|
status: "ok",
|
|
585
617
|
output,
|
|
586
618
|
pathTaken: buildPathTaken(component, row, output, demotedError, orchestrationSteps),
|
|
619
|
+
componentLlmCalls,
|
|
587
620
|
};
|
|
588
621
|
}
|
|
589
622
|
function buildPathTaken(component, inputData, output, error, orchestrationSteps) {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Import component classes to define zhanla components in TypeScript.
|
|
4
4
|
*/
|
|
5
5
|
export { Tool, CodeEval, Skill, Agent, LLMProcessor, LLMEval, Runner, Conditional, Step, Orchestration, Leaf, Edge, Branch, Checklist, EvalTree, ZhanlaComponent, RUNNABLE_TYPES, EVAL_TYPES, } from "./types.js";
|
|
6
|
-
export type { ComponentValue, ToolOptions, CodeEvalOptions, SkillOptions, AgentOptions, LLMProcessorOptions, LLMEvalOptions, OrchestrationOptions, ChecklistOptions, EvalTreeOptions, StepOptions, ComponentType, JsonSchema, JsonSchemaObject, JsonSchemaArray, JsonSchemaScalar, ToolCall, LLMResponse, RunnerOptions, RunnerCallOptions, RunnerMessage, } from "./types.js";
|
|
6
|
+
export type { ComponentValue, ToolOptions, CodeEvalKwargs, CodeEvalOptions, SkillOptions, AgentOptions, LLMProcessorOptions, LLMEvalOptions, OrchestrationOptions, ChecklistOptions, EvalTreeOptions, StepOptions, ComponentType, JsonSchema, JsonSchemaObject, JsonSchemaArray, JsonSchemaScalar, ToolCall, LLMResponse, RunnerOptions, RunnerCallOptions, RunnerMessage, } from "./types.js";
|
|
7
7
|
export { toManifest, collectExportedComponents } from "./manifest.js";
|
|
8
8
|
export type { ComponentManifest, StepManifest, BranchManifest, EdgeManifest, LeafManifest } from "./manifest.js";
|
|
9
9
|
export { executeComponent, executeRow } from "./executor.js";
|
package/dist/types.d.ts
CHANGED
|
@@ -103,10 +103,15 @@ export declare class Tool extends ZhanlaComponent {
|
|
|
103
103
|
readonly key: string;
|
|
104
104
|
constructor(opts: ToolOptions);
|
|
105
105
|
}
|
|
106
|
+
export interface CodeEvalKwargs extends Record<string, unknown> {
|
|
107
|
+
model_input?: string;
|
|
108
|
+
model_response: string;
|
|
109
|
+
expected_output?: string;
|
|
110
|
+
}
|
|
106
111
|
export interface CodeEvalOptions {
|
|
107
112
|
name: string;
|
|
108
113
|
description: string;
|
|
109
|
-
fn: (
|
|
114
|
+
fn: (kwargs: CodeEvalKwargs) => unknown;
|
|
110
115
|
modelResponseFormat?: "JSON" | "TEXT" | "YAML";
|
|
111
116
|
key: string;
|
|
112
117
|
}
|
|
@@ -114,7 +119,7 @@ export declare class CodeEval extends ZhanlaComponent {
|
|
|
114
119
|
readonly componentType: "code_eval";
|
|
115
120
|
readonly name: string;
|
|
116
121
|
readonly description: string;
|
|
117
|
-
readonly fn: (
|
|
122
|
+
readonly fn: (kwargs: CodeEvalKwargs) => unknown;
|
|
118
123
|
readonly isAsync: boolean;
|
|
119
124
|
readonly modelResponseFormat: "JSON" | "TEXT" | "YAML";
|
|
120
125
|
readonly key: string;
|