@hellohelen-ai/goliath 0.0.1

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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +142 -0
  3. package/dist/budget.d.ts +16 -0
  4. package/dist/budget.d.ts.map +1 -0
  5. package/dist/budget.js +36 -0
  6. package/dist/budget.js.map +1 -0
  7. package/dist/compress/structural.d.ts +4 -0
  8. package/dist/compress/structural.d.ts.map +1 -0
  9. package/dist/compress/structural.js +68 -0
  10. package/dist/compress/structural.js.map +1 -0
  11. package/dist/conductor.d.ts +75 -0
  12. package/dist/conductor.d.ts.map +1 -0
  13. package/dist/conductor.js +102 -0
  14. package/dist/conductor.js.map +1 -0
  15. package/dist/create-goliath.d.ts +22 -0
  16. package/dist/create-goliath.d.ts.map +1 -0
  17. package/dist/create-goliath.js +68 -0
  18. package/dist/create-goliath.js.map +1 -0
  19. package/dist/fallback/http-fallback.d.ts +20 -0
  20. package/dist/fallback/http-fallback.d.ts.map +1 -0
  21. package/dist/fallback/http-fallback.js +34 -0
  22. package/dist/fallback/http-fallback.js.map +1 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +9 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/judge.d.ts +22 -0
  28. package/dist/judge.d.ts.map +1 -0
  29. package/dist/judge.js +23 -0
  30. package/dist/judge.js.map +1 -0
  31. package/dist/memory/in-memory.d.ts +6 -0
  32. package/dist/memory/in-memory.d.ts.map +1 -0
  33. package/dist/memory/in-memory.js +13 -0
  34. package/dist/memory/in-memory.js.map +1 -0
  35. package/dist/memory/key-value.d.ts +14 -0
  36. package/dist/memory/key-value.d.ts.map +1 -0
  37. package/dist/memory/key-value.js +25 -0
  38. package/dist/memory/key-value.js.map +1 -0
  39. package/dist/prompts.d.ts +41 -0
  40. package/dist/prompts.d.ts.map +1 -0
  41. package/dist/prompts.js +104 -0
  42. package/dist/prompts.js.map +1 -0
  43. package/dist/run-turn.d.ts +26 -0
  44. package/dist/run-turn.d.ts.map +1 -0
  45. package/dist/run-turn.js +260 -0
  46. package/dist/run-turn.js.map +1 -0
  47. package/dist/scribe.d.ts +17 -0
  48. package/dist/scribe.d.ts.map +1 -0
  49. package/dist/scribe.js +44 -0
  50. package/dist/scribe.js.map +1 -0
  51. package/dist/testing/fake-model.d.ts +27 -0
  52. package/dist/testing/fake-model.d.ts.map +1 -0
  53. package/dist/testing/fake-model.js +57 -0
  54. package/dist/testing/fake-model.js.map +1 -0
  55. package/dist/testing/index.d.ts +3 -0
  56. package/dist/testing/index.d.ts.map +1 -0
  57. package/dist/testing/index.js +2 -0
  58. package/dist/testing/index.js.map +1 -0
  59. package/dist/tools/define-tool.d.ts +21 -0
  60. package/dist/tools/define-tool.d.ts.map +1 -0
  61. package/dist/tools/define-tool.js +10 -0
  62. package/dist/tools/define-tool.js.map +1 -0
  63. package/dist/types.d.ts +182 -0
  64. package/dist/types.d.ts.map +1 -0
  65. package/dist/types.js +2 -0
  66. package/dist/types.js.map +1 -0
  67. package/dist/worker.d.ts +53 -0
  68. package/dist/worker.d.ts.map +1 -0
  69. package/dist/worker.js +124 -0
  70. package/dist/worker.js.map +1 -0
  71. package/package.json +69 -0
@@ -0,0 +1,182 @@
1
+ import type { LanguageModel, ModelMessage } from "ai";
2
+ import type { z } from "zod";
3
+ /**
4
+ * A tool the phone's model may call. Keep the schema flat and the
5
+ * description one sentence: a 3B model reads every word of it on every step.
6
+ */
7
+ type GoliathTool<INPUT = unknown, OUTPUT = unknown> = {
8
+ name: string;
9
+ description: string;
10
+ parameters: z.ZodType<INPUT>;
11
+ /** True when the tool changes something. Goliath asks before running it. */
12
+ writes?: boolean;
13
+ execute: (input: INPUT, context: ToolContext) => Promise<OUTPUT> | OUTPUT;
14
+ /**
15
+ * What the model sees. The app keeps the full output; the model gets this
16
+ * string. Default: `key: value` lines capped at 600 characters.
17
+ */
18
+ toModelOutput?: (output: OUTPUT) => string;
19
+ /**
20
+ * Tools that must have run earlier in the turn. Rendered to the conductor
21
+ * as "Use lookupContact before sendMessage." TinyAgent's biggest plan-shape
22
+ * lever was exactly this sentence.
23
+ */
24
+ requires?: string[];
25
+ };
26
+ type ToolContext = {
27
+ signal?: AbortSignal;
28
+ };
29
+ type ToolMap = Record<string, GoliathTool<any, any>>;
30
+ /** What Goliath remembers between turns. Small on purpose. */
31
+ type MemoryState = {
32
+ /** A rolling brief of everything that came before, written by the scribe. */
33
+ summary: string;
34
+ /** The last few exchanges, verbatim, newest last. */
35
+ recent: Exchange[];
36
+ };
37
+ type Exchange = {
38
+ ask: string;
39
+ answer: string;
40
+ at: number;
41
+ };
42
+ type Memory = {
43
+ load: () => Promise<MemoryState>;
44
+ save: (state: MemoryState) => Promise<void>;
45
+ };
46
+ /** Where a turn goes when the phone cannot finish it. */
47
+ type Fallback = (request: FallbackRequest) => Promise<{
48
+ text: string;
49
+ }>;
50
+ type FallbackRequest = {
51
+ ask: string;
52
+ summary: string;
53
+ recent: Exchange[];
54
+ steps: StepRecord[];
55
+ reason: EscalationReason;
56
+ /** The model or provider error message, when `reason` is "model-error". */
57
+ error?: string;
58
+ signal?: AbortSignal;
59
+ };
60
+ type EscalationReason = "no-model" | "model-unavailable" | "too-many-steps" | "repeated-tool-call" | "empty-answer" | "plan-invalid" | "conductor-asked" | "tool-args-invalid" | "tool-error" | "guardrail" | "model-error";
61
+ /** One stone thrown: what the conductor decided and what the worker did. */
62
+ type StepRecord = {
63
+ index: number;
64
+ kind: "tool" | "answer";
65
+ brief: string;
66
+ tool?: string;
67
+ input?: unknown;
68
+ /** The compressed tool result the transcript carries forward. */
69
+ result?: string;
70
+ skipped?: boolean;
71
+ /** Served from an earlier identical step; nothing ran. */
72
+ cached?: boolean;
73
+ /** The tool threw. `result` carries the message the conductor plans around. */
74
+ failed?: boolean;
75
+ text?: string;
76
+ };
77
+ type ConfirmDecision = boolean | {
78
+ approved: boolean;
79
+ reason?: string;
80
+ };
81
+ /** Asked before a tool that writes runs. A reason on a decline reaches the conductor. */
82
+ type Confirm = (request: {
83
+ tool: string;
84
+ input: unknown;
85
+ brief: string;
86
+ }) => Promise<ConfirmDecision>;
87
+ type TraceEvent = {
88
+ type: "recall";
89
+ summary: string;
90
+ recent: number;
91
+ } | {
92
+ type: "plan";
93
+ index: number;
94
+ kind: StepRecord["kind"];
95
+ tool?: string;
96
+ /** The conductor's one-sentence rationale, when it gave one. */
97
+ why?: string;
98
+ brief: string;
99
+ } | {
100
+ type: "confirm";
101
+ tool: string;
102
+ approved: boolean;
103
+ reason?: string;
104
+ } | {
105
+ type: "tool";
106
+ tool: string;
107
+ input: unknown;
108
+ result: string;
109
+ ms: number;
110
+ } | {
111
+ type: "answer";
112
+ text: string;
113
+ } | {
114
+ type: "escalate";
115
+ reason: EscalationReason;
116
+ error?: string;
117
+ } | {
118
+ type: "remember";
119
+ summary: string;
120
+ } | {
121
+ type: "budget";
122
+ label: string;
123
+ tokens: number;
124
+ limit: number;
125
+ };
126
+ type RunResult = {
127
+ text: string;
128
+ handledBy: "device" | "cloud";
129
+ /** True when the loop stalled, no fallback was configured, and the answer is a best effort from the step log. */
130
+ bestEffort?: boolean;
131
+ steps: StepRecord[];
132
+ trace: TraceEvent[];
133
+ };
134
+ type Compressor = (input: {
135
+ messages: ModelMessage[];
136
+ ask: string;
137
+ budget: number;
138
+ }) => Promise<ModelMessage[]> | ModelMessage[];
139
+ type GoliathConfig = {
140
+ /** Any AI SDK language model. On a phone, `apple()` from `@react-native-ai/apple`. */
141
+ model: LanguageModel;
142
+ tools?: ToolMap;
143
+ memory?: Memory;
144
+ fallback?: Fallback;
145
+ /** Asked before any tool with `writes: true` runs. Default approves everything. */
146
+ confirm?: Confirm;
147
+ /** Context window in tokens. Apple Foundation Models: 4096. */
148
+ window?: number;
149
+ /** Most stones the conductor may throw in one turn. Default 5. */
150
+ maxSteps?: number;
151
+ /** Extra compressors run after the built-in structural pass. */
152
+ compressors?: Compressor[];
153
+ /** Who Goliath is, in one or two short sentences. Every prompt starts with it. */
154
+ instructions?: string;
155
+ /** Called for every trace event as it happens. */
156
+ onEvent?: (event: TraceEvent) => void;
157
+ /**
158
+ * Values the model should always have, injected as `key: value` lines
159
+ * instead of fetched by a tool (Apple TN3193: run the tool before the model
160
+ * when it always needs the result). Today's date, timezone, the user's name.
161
+ * A function is called once per turn.
162
+ */
163
+ facts?: Record<string, string> | (() => Record<string, string>);
164
+ /**
165
+ * Two or three worked plans, shown to the conductor. Format is fixed by
166
+ * guided generation; examples buy tool choice and ordering. Measure before
167
+ * keeping any: each costs ~60 tokens per step.
168
+ */
169
+ examples?: PlanExample[];
170
+ };
171
+ type PlanExample = {
172
+ ask: string;
173
+ /** The steps a good run takes, in order, with `answer` last. */
174
+ steps: Array<{
175
+ tool: string;
176
+ brief: string;
177
+ } | {
178
+ answer: string;
179
+ }>;
180
+ };
181
+ export type { Compressor, Confirm, ConfirmDecision, EscalationReason, Exchange, Fallback, FallbackRequest, GoliathConfig, GoliathTool, Memory, MemoryState, PlanExample, RunResult, StepRecord, ToolContext, ToolMap, TraceEvent, };
182
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AACtD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B;;;GAGG;AACH,KAAK,WAAW,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,IAAI;IACpD,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,4EAA4E;IAC5E,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,E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+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,53 @@
1
+ import { type LanguageModel } from "ai";
2
+ import { z } from "zod";
3
+ import type { Confirm, GoliathTool, StepRecord } from "./types.js";
4
+ type ToolStepOutcome = {
5
+ ok: true;
6
+ input: unknown;
7
+ result: string;
8
+ skipped: boolean;
9
+ failed?: boolean;
10
+ } | {
11
+ ok: false;
12
+ reason: "tool-args-invalid";
13
+ };
14
+ /**
15
+ * A worker gets a fresh context, one tool, and a one-line brief. The conductor
16
+ * already chose the tool, so the worker's whole job is the arguments: one
17
+ * structured-output call, then Goliath runs the tool itself.
18
+ *
19
+ * Goliath never hands tools to the provider. On Apple's runtime the provider
20
+ * would run its own loop with pre-registered tools and no step boundary
21
+ * (docs/research/rn-providers-and-ai-sdk.md § 1.3), and mixing tools with
22
+ * schema-constrained output suppresses tool calls on small models
23
+ * (docs/research/small-context-agent-patterns.md, rule 9). Asking for the
24
+ * arguments as a plain object sidesteps both, works on every provider, and
25
+ * keeps the confirm step in Goliath's hands.
26
+ */
27
+ declare const runToolStep: (input: {
28
+ model: LanguageModel;
29
+ instructions: string;
30
+ tool: GoliathTool<any, any>;
31
+ brief: string;
32
+ ask: string;
33
+ confirm: Confirm;
34
+ signal?: AbortSignal;
35
+ }) => Promise<ToolStepOutcome>;
36
+ /** A tool with no parameters needs no model call at all. Apple says the same: run it directly. */
37
+ declare const needsArguments: (schema: z.ZodType) => boolean;
38
+ /** The closing stone: turn the step log into two or three sentences. */
39
+ declare const runAnswerStep: (input: {
40
+ model: LanguageModel;
41
+ instructions: string;
42
+ ask: string;
43
+ summary: string;
44
+ steps: StepRecord[];
45
+ /** Appended on the one retry after an empty answer. */
46
+ nudge?: string;
47
+ /** The loop stalled; say what was found and what is open. */
48
+ bestEffort?: boolean;
49
+ signal?: AbortSignal;
50
+ }) => Promise<string>;
51
+ export { needsArguments, runAnswerStep, runToolStep };
52
+ export type { ToolStepOutcome };
53
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D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,KAAG,OAAO,CAAC,MAAM,CAWjB,CAAC;AAEF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,CAAC;AACtD,YAAY,EAAE,eAAe,EAAE,CAAC"}
package/dist/worker.js ADDED
@@ -0,0 +1,124 @@
1
+ import { NoObjectGeneratedError, Output, generateText } from "ai";
2
+ import { z } from "zod";
3
+ import { clip, summarizeToolResult } from "./compress/structural.js";
4
+ import { answerSystem, answerUser, bestEffortSystem, workerSystem } from "./prompts.js";
5
+ /**
6
+ * A worker gets a fresh context, one tool, and a one-line brief. The conductor
7
+ * already chose the tool, so the worker's whole job is the arguments: one
8
+ * structured-output call, then Goliath runs the tool itself.
9
+ *
10
+ * Goliath never hands tools to the provider. On Apple's runtime the provider
11
+ * would run its own loop with pre-registered tools and no step boundary
12
+ * (docs/research/rn-providers-and-ai-sdk.md § 1.3), and mixing tools with
13
+ * schema-constrained output suppresses tool calls on small models
14
+ * (docs/research/small-context-agent-patterns.md, rule 9). Asking for the
15
+ * arguments as a plain object sidesteps both, works on every provider, and
16
+ * keeps the confirm step in Goliath's hands.
17
+ */
18
+ const runToolStep = async (input) => {
19
+ let args = {};
20
+ if (needsArguments(input.tool.parameters)) {
21
+ try {
22
+ const result = await generateText({
23
+ model: input.model,
24
+ output: Output.object({ schema: withMissing(input.tool.parameters) }),
25
+ system: workerSystem(input.instructions, input.brief),
26
+ prompt: input.ask,
27
+ ...(input.signal ? { abortSignal: input.signal } : {}),
28
+ });
29
+ if (result.output === undefined)
30
+ return { ok: false, reason: "tool-args-invalid" };
31
+ const { missing, ...rest } = result.output;
32
+ if (missing && missing.trim()) {
33
+ // The worker said what it did not have instead of inventing it. The
34
+ // conductor reads this like any other result and can ask the user.
35
+ return {
36
+ ok: true,
37
+ input: rest,
38
+ result: `missing: ${clip(missing.trim(), 120)}. Ask the user or use another tool.`,
39
+ skipped: true,
40
+ };
41
+ }
42
+ args = rest;
43
+ }
44
+ catch (error) {
45
+ // Bad JSON or a schema miss is the model's mistake; anything else is a real error.
46
+ if (NoObjectGeneratedError.isInstance(error))
47
+ return { ok: false, reason: "tool-args-invalid" };
48
+ throw error;
49
+ }
50
+ }
51
+ if (input.tool.writes) {
52
+ const decision = await input.confirm({
53
+ tool: input.tool.name,
54
+ input: args,
55
+ brief: input.brief,
56
+ });
57
+ const approved = typeof decision === "boolean" ? decision : decision.approved;
58
+ if (!approved) {
59
+ const reason = typeof decision === "object" && decision.reason ? `: ${decision.reason}` : "";
60
+ // Named like deepagents' rejection message so the model reads it as a
61
+ // decision, not an error to work around.
62
+ return {
63
+ ok: true,
64
+ input: args,
65
+ result: `declined by the user${reason}. Do not retry unless asked.`,
66
+ skipped: true,
67
+ };
68
+ }
69
+ }
70
+ const context = input.signal ? { signal: input.signal } : {};
71
+ let output;
72
+ try {
73
+ output = await input.tool.execute(args, context);
74
+ }
75
+ catch (error) {
76
+ if (error?.name === "AbortError")
77
+ throw error;
78
+ // smolagents: "Error executing tool ... Please try again or use another tool".
79
+ // The conductor reads this and plans around it; two in a row is a real signal.
80
+ const message = error instanceof Error ? error.message : String(error);
81
+ return {
82
+ ok: true,
83
+ input: args,
84
+ result: `error: ${clip(message, 160)}. Try different arguments or another tool.`,
85
+ skipped: false,
86
+ failed: true,
87
+ };
88
+ }
89
+ const shaped = input.tool.toModelOutput
90
+ ? input.tool.toModelOutput(output)
91
+ : summarizeToolResult(output);
92
+ return { ok: true, input: args, result: shaped, skipped: false };
93
+ };
94
+ /** A tool with no parameters needs no model call at all. Apple says the same: run it directly. */
95
+ const needsArguments = (schema) => !(schema instanceof z.ZodObject) || Object.keys(schema.shape).length > 0;
96
+ /**
97
+ * The tool's schema plus a trailing optional `missing`. Last, so it cannot
98
+ * steal the argument budget; present, so a required value the brief did not
99
+ * contain is named rather than invented (the Constraint Tax's "wrong but
100
+ * valid" outputs are exactly invented values).
101
+ */
102
+ const withMissing = (schema) => schema instanceof z.ZodObject
103
+ ? schema.extend({
104
+ missing: z
105
+ .string()
106
+ .optional()
107
+ .describe("Required values the brief did not contain, if any. Otherwise leave empty."),
108
+ })
109
+ : schema;
110
+ /** The closing stone: turn the step log into two or three sentences. */
111
+ const runAnswerStep = async (input) => {
112
+ const prompt = answerUser({ ask: input.ask, summary: input.summary, steps: input.steps });
113
+ const result = await generateText({
114
+ model: input.model,
115
+ system: input.bestEffort
116
+ ? bestEffortSystem(input.instructions)
117
+ : answerSystem(input.instructions),
118
+ prompt: input.nudge ? `${prompt}\n\n${input.nudge}` : prompt,
119
+ ...(input.signal ? { abortSignal: input.signal } : {}),
120
+ });
121
+ return result.text.trim();
122
+ };
123
+ export { needsArguments, runAnswerStep, runToolStep };
124
+ //# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.js","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,EAAE,YAAY,EAAsB,MAAM,IAAI,CAAC;AACtF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAOxF;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,GAAG,KAAK,EAAE,KAQ1B,EAA4B,EAAE;IAC7B,IAAI,IAAI,GAAY,EAAE,CAAC;IACvB,IAAI,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC;gBAChC,KAAK,EAAE,KAAK,CAAC,KAAK;gBAClB,MAAM,EAAE,MAAM,CAA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U;YACtB,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,YAAY,CAAC;YACtC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,YAAY,CAAC;QACpC,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,MAAM;QAC5D,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACvD,CAAC,CAAC;IACH,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;AAC5B,CAAC,CAAC;AAEF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@hellohelen-ai/goliath",
3
+ "version": "0.0.1",
4
+ "description": "An agent harness for the phone's own model. Apple Foundation Models, a 4k window, one stone.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/hellohelen-ai/goliath.git"
9
+ },
10
+ "homepage": "https://github.com/hellohelen-ai/goliath#readme",
11
+ "bugs": "https://github.com/hellohelen-ai/goliath/issues",
12
+ "keywords": [
13
+ "on-device",
14
+ "agent",
15
+ "apple-intelligence",
16
+ "foundation-models",
17
+ "react-native",
18
+ "expo",
19
+ "ai-sdk",
20
+ "small-language-model"
21
+ ],
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "files": [
25
+ "dist",
26
+ "README.md",
27
+ "LICENSE"
28
+ ],
29
+ "main": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./testing": {
37
+ "types": "./dist/testing/index.d.ts",
38
+ "default": "./dist/testing/index.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "scripts": {
43
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
44
+ "typecheck": "tsc --noEmit",
45
+ "test": "bun test",
46
+ "format": "prettier --write .",
47
+ "format:check": "prettier --check .",
48
+ "check": "bun run typecheck && bun run format:check && bun run test && bun run build",
49
+ "prepublishOnly": "bun run check",
50
+ "evals": "bun run evals/cli.ts"
51
+ },
52
+ "peerDependencies": {
53
+ "ai": "^7.0.0",
54
+ "zod": "^4.0.0"
55
+ },
56
+ "devDependencies": {
57
+ "@types/bun": "^1.2.0",
58
+ "ai": "^7.0.91",
59
+ "prettier": "^3.6.0",
60
+ "typescript": "^5.9.0",
61
+ "zod": "^4.2.0"
62
+ },
63
+ "publishConfig": {
64
+ "access": "public"
65
+ },
66
+ "engines": {
67
+ "node": ">=20"
68
+ }
69
+ }