@theokit/sdk 1.8.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/CHANGELOG.md +70 -22
- package/bin/init-claude.mjs +0 -0
- package/dist/a2a/index.cjs +12592 -12884
- package/dist/a2a/index.cjs.map +1 -1
- package/dist/a2a/index.js +12592 -12884
- package/dist/a2a/index.js.map +1 -1
- package/dist/{cron-BnywDYLq.d.cts → cron-Bse1MbaE.d.cts} +52 -2
- package/dist/{cron-CtZvJD9J.d.ts → cron-Ci_NUkUj.d.ts} +52 -2
- package/dist/cron.cjs +8763 -9062
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.d.cts +1 -1
- package/dist/cron.d.ts +1 -1
- package/dist/cron.js +8763 -9062
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +8776 -9075
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +8776 -9075
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +11217 -10037
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +174 -3
- package/dist/index.d.ts +174 -3
- package/dist/index.js +11217 -10038
- package/dist/index.js.map +1 -1
- package/dist/internal/runtime/{cloud-config-serializer.d.ts → cloud/cloud-config-serializer.d.ts} +1 -1
- package/dist/internal/runtime/{real-cloud-run.d.ts → cloud/real-cloud-run.d.ts} +1 -1
- package/dist/internal/runtime/fixtures/fixture-scripts.d.ts +1 -1
- package/dist/internal/runtime/fork-agent.d.ts +1 -1
- package/dist/internal/runtime/{local-agent-bootstrap.d.ts → local-agent/local-agent-bootstrap.d.ts} +4 -4
- package/dist/internal/runtime/{local-agent-dispatch.d.ts → local-agent/local-agent-dispatch.d.ts} +7 -7
- package/dist/internal/runtime/{local-agent-memory-hooks.d.ts → local-agent/local-agent-memory-hooks.d.ts} +2 -2
- package/dist/internal/runtime/{local-agent-memory-provider.d.ts → local-agent/local-agent-memory-provider.d.ts} +2 -2
- package/dist/internal/runtime/{real-local-run.d.ts → local-agent/real-local-run.d.ts} +1 -1
- package/dist/internal/runtime/{memory-path-selector.d.ts → memory/memory-path-selector.d.ts} +1 -1
- package/dist/internal/runtime/{memory-provider.d.ts → memory/memory-provider.d.ts} +2 -2
- package/dist/internal/runtime/{memory-store.d.ts → memory/memory-store.d.ts} +1 -1
- package/dist/internal/runtime/skills/subagents-loader.d.ts +1 -0
- package/dist/types/agent.d.ts +3 -3
- package/dist/types/fork.d.ts +49 -0
- package/dist/types/goal-events.d.ts +1 -1
- package/package.json +15 -26
- package/dist/rag/index.cjs +0 -136
- package/dist/rag/index.cjs.map +0 -1
- package/dist/rag/index.d.cts +0 -11
- package/dist/rag/index.d.ts +0 -11
- package/dist/rag/index.js +0 -129
- package/dist/rag/index.js.map +0 -1
- package/dist/rag/reranker.d.cts +0 -26
- package/dist/rag/reranker.d.ts +0 -26
- package/dist/rag/retriever.d.cts +0 -25
- package/dist/rag/retriever.d.ts +0 -25
- package/dist/rag/text-splitter.d.cts +0 -12
- package/dist/rag/text-splitter.d.ts +0 -12
- package/dist/rag/types.d.cts +0 -37
- package/dist/rag/types.d.ts +0 -37
- package/dist/voice/index.d.ts +0 -7
- package/dist/voice/openai-realtime.d.ts +0 -21
- package/dist/voice/types.d.ts +0 -35
- /package/dist/internal/{runtime/cloud-agent.d.ts → agent-loop/budget-gate.d.ts} +0 -0
- /package/dist/internal/runtime/{budget-tracker.d.ts → budget/budget-tracker.d.ts} +0 -0
- /package/dist/internal/runtime/{budget.d.ts → budget/budget.d.ts} +0 -0
- /package/dist/internal/runtime/{cloud-run.d.ts → cloud/cloud-agent.d.ts} +0 -0
- /package/dist/internal/runtime/{cloud-payload-types.d.ts → cloud/cloud-payload-types.d.ts} +0 -0
- /package/dist/internal/runtime/{cloud-tool-parity.d.ts → cloud/cloud-run.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-memory.d.ts → cloud/cloud-tool-parity.d.ts} +0 -0
- /package/dist/internal/runtime/{hooks-executor.d.ts → hooks/hooks-executor.d.ts} +0 -0
- /package/dist/internal/runtime/{hooks-frontmatter.d.ts → hooks/hooks-frontmatter.d.ts} +0 -0
- /package/dist/internal/runtime/{hooks-source.d.ts → hooks/hooks-source.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-invalidate.d.ts → local-agent/local-agent-invalidate.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-memory-direct.d.ts → local-agent/local-agent-memory-direct.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-send.d.ts → local-agent/local-agent-memory.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-personality-extensions.d.ts → local-agent/local-agent-personality-extensions.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-plugins.d.ts → local-agent/local-agent-plugins.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-runtime-extensions.d.ts → local-agent/local-agent-runtime-extensions.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent.d.ts → local-agent/local-agent-send.d.ts} +0 -0
- /package/dist/internal/runtime/{local-agent-task-wrap.d.ts → local-agent/local-agent-task-wrap.d.ts} +0 -0
- /package/dist/internal/runtime/{local-run.d.ts → local-agent/local-agent.d.ts} +0 -0
- /package/dist/internal/runtime/{skills-manager.d.ts → local-agent/local-run.d.ts} +0 -0
- /package/dist/internal/runtime/{agent-session-store.d.ts → session/agent-session-store.d.ts} +0 -0
- /package/dist/internal/runtime/{agent-session.d.ts → session/agent-session.d.ts} +0 -0
- /package/dist/internal/runtime/{session-types.d.ts → session/session-types.d.ts} +0 -0
- /package/dist/internal/runtime/{skill-frontmatter.d.ts → skills/skill-frontmatter.d.ts} +0 -0
- /package/dist/internal/runtime/{subagents-loader.d.ts → skills/skills-manager.d.ts} +0 -0
package/dist/internal/runtime/{cloud-config-serializer.d.ts → cloud/cloud-config-serializer.d.ts}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { AgentOptions } from "
|
|
1
|
+
import type { AgentOptions } from "../../../types/agent.js";
|
|
2
2
|
import type { CloudAgentPayload } from "./cloud-payload-types.js";
|
|
3
3
|
export declare function serializeCloudAgentConfig(options: AgentOptions): CloudAgentPayload;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Run } from "
|
|
1
|
+
import type { Run } from "../../../types/run.js";
|
|
2
2
|
export declare function createRealCloudRun(options: CreateRealCloudRunOptions): Run;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FixtureRequest, FixtureScript } from "./fixture-types.js";
|
|
2
|
-
export { extractMemoryFact, isMemoryWritePrompt } from "../memory-store.js";
|
|
2
|
+
export { extractMemoryFact, isMemoryWritePrompt } from "../memory/memory-store.js";
|
|
3
3
|
export declare function isMemoryRecallPrompt(message: string, request: FixtureRequest): boolean;
|
|
4
4
|
export declare function memoryWriteScript(request: FixtureRequest): FixtureScript;
|
|
5
5
|
export declare function memoryRecallScript(request: FixtureRequest): FixtureScript;
|
package/dist/internal/runtime/{local-agent-bootstrap.d.ts → local-agent/local-agent-bootstrap.d.ts}
RENAMED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @internal
|
|
9
9
|
*/
|
|
10
|
-
import { FileContextManager } from "
|
|
11
|
-
import { type PluginMetadata, PluginsManager } from "
|
|
12
|
-
import { ProvidersManagerImpl } from "
|
|
13
|
-
import { type SkillMetadata, SkillsManager } from "
|
|
10
|
+
import { FileContextManager } from "../context/context-manager.js";
|
|
11
|
+
import { type PluginMetadata, PluginsManager } from "../plugins/plugins-manager.js";
|
|
12
|
+
import { ProvidersManagerImpl } from "../providers-manager.js";
|
|
13
|
+
import { type SkillMetadata, SkillsManager } from "../skills/skills-manager.js";
|
|
14
14
|
export declare function registerLocalAgent(args: {
|
|
15
15
|
agentId: string;
|
|
16
16
|
model: ModelSelection | undefined;
|
package/dist/internal/runtime/{local-agent-dispatch.d.ts → local-agent/local-agent-dispatch.d.ts}
RENAMED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @internal
|
|
6
6
|
*/
|
|
7
|
-
import type { AgentDefinition, AgentOptions, ModelSelection } from "
|
|
8
|
-
import type { Run, SDKUserMessage, SendOptions } from "
|
|
9
|
-
import type { MemoryToolSpec } from "
|
|
10
|
-
import type { PersonalityPreset } from "
|
|
11
|
-
import type { PluginManager } from "
|
|
12
|
-
import type { HooksExecutor } from "
|
|
13
|
-
import type { MemoryFact } from "
|
|
7
|
+
import type { AgentDefinition, AgentOptions, ModelSelection } from "../../../types/agent.js";
|
|
8
|
+
import type { Run, SDKUserMessage, SendOptions } from "../../../types/run.js";
|
|
9
|
+
import type { MemoryToolSpec } from "../../agent-loop/loop-types.js";
|
|
10
|
+
import type { PersonalityPreset } from "../../personality/types.js";
|
|
11
|
+
import type { PluginManager } from "../../plugins/manager.js";
|
|
12
|
+
import type { HooksExecutor } from "../hooks/hooks-executor.js";
|
|
13
|
+
import type { MemoryFact } from "../memory/memory-store.js";
|
|
14
14
|
import { createRealLocalRun } from "./real-local-run.js";
|
|
15
15
|
export interface DispatchInputs {
|
|
16
16
|
agentId: string;
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @internal
|
|
12
12
|
*/
|
|
13
|
-
import type { Run, SDKUserMessage, SendOptions } from "
|
|
14
|
-
import type { PluginManager } from "
|
|
13
|
+
import type { Run, SDKUserMessage, SendOptions } from "../../../types/run.js";
|
|
14
|
+
import type { PluginManager } from "../../plugins/manager.js";
|
|
15
15
|
/**
|
|
16
16
|
* EC-A + EC-G + EC-H: fire `pre_user_send` hooks, cap recalled context,
|
|
17
17
|
* and prepend it as a `<memory-context>` fence around the original
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
* @internal — sdk-core's bridging glue. Replaced by sdk-memory rich impl
|
|
26
26
|
* in Stage 3.
|
|
27
27
|
*/
|
|
28
|
-
import type { TelemetryHandle } from "
|
|
29
|
-
import type { MemoryProvider } from "
|
|
28
|
+
import type { TelemetryHandle } from "../../telemetry/tracer.js";
|
|
29
|
+
import type { MemoryProvider } from "../memory/memory-provider.js";
|
|
30
30
|
/**
|
|
31
31
|
* Options for `createLocalAgentMemoryProvider`. Mirrors what
|
|
32
32
|
* `LocalAgentMemory`'s constructor needs.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Run } from "
|
|
1
|
+
import type { Run } from "../../../types/run.js";
|
|
2
2
|
export declare function createRealLocalRun(options: CreateRealLocalRunOptions): Run;
|
package/dist/internal/runtime/{memory-path-selector.d.ts → memory/memory-path-selector.d.ts}
RENAMED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
*
|
|
20
20
|
* @internal
|
|
21
21
|
*/
|
|
22
|
-
import type { MemoryToolSpec } from "
|
|
22
|
+
import type { MemoryToolSpec } from "../../agent-loop/loop-types.js";
|
|
23
23
|
import type { MemoryProvider } from "./memory-provider.js";
|
|
24
24
|
/** Env var name — internal, not in public docs yet. */
|
|
25
25
|
export declare const PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
*
|
|
29
29
|
* @public — surface-level interface; impls are internal-but-replaceable.
|
|
30
30
|
*/
|
|
31
|
-
import type { CustomTool, SDKAgent } from "
|
|
32
|
-
import type { MemoryAdapter, MemoryFact } from "
|
|
31
|
+
import type { CustomTool, SDKAgent } from "../../../types/agent.js";
|
|
32
|
+
import type { MemoryAdapter, MemoryFact } from "../../../types/memory-adapter.js";
|
|
33
33
|
/** Result of `MemoryProvider.runActivePass(...)` — what the kernel injects into the LLM call. */
|
|
34
34
|
export interface ActiveMemoryPassResult {
|
|
35
35
|
/** Compressed memory facts to seed the LLM's context window for this turn. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type MemoryConfig, type MemoryFact, redactSecrets } from "
|
|
1
|
+
import { type MemoryConfig, type MemoryFact, redactSecrets } from "../../memory/types.js";
|
|
2
2
|
export { redactSecrets };
|
|
3
3
|
export declare function readMemoryFacts(cwd: string, config: MemoryConfig): Promise<MemoryFact[]>;
|
|
4
4
|
export declare function appendMemoryFact(cwd: string, config: MemoryConfig, fact: MemoryFact): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types/agent.d.ts
CHANGED
|
@@ -478,7 +478,7 @@ export interface AgentOptions {
|
|
|
478
478
|
*
|
|
479
479
|
* @public
|
|
480
480
|
*/
|
|
481
|
-
budgetTracker?: import("../internal/runtime/budget-tracker.js").BudgetTracker;
|
|
481
|
+
budgetTracker?: import("../internal/runtime/budget/budget-tracker.js").BudgetTracker;
|
|
482
482
|
/**
|
|
483
483
|
* Pluggable memory subsystem (SDK 2.0 Phase 1 / T1.3 — Hexagonal
|
|
484
484
|
* Architecture interface inversion). When provided, the agent loop
|
|
@@ -502,7 +502,7 @@ export interface AgentOptions {
|
|
|
502
502
|
*
|
|
503
503
|
* @public
|
|
504
504
|
*/
|
|
505
|
-
memoryProvider?: import("../internal/runtime/memory-provider.js").MemoryProvider;
|
|
505
|
+
memoryProvider?: import("../internal/runtime/memory/memory-provider.js").MemoryProvider;
|
|
506
506
|
}
|
|
507
507
|
/**
|
|
508
508
|
* Artifact produced inside an agent's workspace. Cloud-only.
|
|
@@ -607,7 +607,7 @@ export interface SDKAgent {
|
|
|
607
607
|
*
|
|
608
608
|
* @public
|
|
609
609
|
*/
|
|
610
|
-
fork?(options: import("
|
|
610
|
+
fork?(options: import("./fork.js").ForkOptions): Promise<import("./fork.js").ForkResult>;
|
|
611
611
|
/**
|
|
612
612
|
* Direct API to third-party memory adapter(s) registered via
|
|
613
613
|
* `plugins: [...]` (ADR D141 / D142). Returns `null` when no adapter
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fork primitive public type contracts (T1.2, ADRs D110-D114).
|
|
3
|
+
*
|
|
4
|
+
* Extracted into a leaf type module (arch-review ADR 0001) so the public
|
|
5
|
+
* `types/agent.ts` barrel can reference `ForkOptions`/`ForkResult` without
|
|
6
|
+
* importing the `internal/runtime/fork-agent.ts` implementation — which in
|
|
7
|
+
* turn imports back from `types/agent.ts`. That mutual reference created a
|
|
8
|
+
* type-only `madge` cycle. These interfaces are self-contained (no SDKAgent
|
|
9
|
+
* / AgentOptions references), so co-locating them here breaks the cycle with
|
|
10
|
+
* no runtime change. `fork-agent.ts` re-exports them for back-compat.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Caller-supplied fork configuration. See `forkAgentImpl`.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export interface ForkOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Tool subset visible to the fork. Names must match the canonical (post-repair)
|
|
20
|
+
* tool name — typically lowercase. Tools not in this set return a `tool_result`
|
|
21
|
+
* with `"Tool blocked by fork whitelist"` content (EC-H).
|
|
22
|
+
*/
|
|
23
|
+
allowedTools: Set<string>;
|
|
24
|
+
/** Task prompt sent to the fork. */
|
|
25
|
+
prompt: string;
|
|
26
|
+
/** Override system prompt. Default: byte-identical inheritance from parent (D112). */
|
|
27
|
+
systemPrompt?: string;
|
|
28
|
+
/** Memory write provenance tag (D114). Default `"fork"`. */
|
|
29
|
+
forkOrigin?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Outcome of a fork run.
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export interface ForkResult {
|
|
37
|
+
/** Final agent response text (`undefined` when the fork produced no result). */
|
|
38
|
+
result: string | undefined;
|
|
39
|
+
/** Tool calls executed inside the fork. */
|
|
40
|
+
toolCalls: ReadonlyArray<{
|
|
41
|
+
name: string;
|
|
42
|
+
input: Record<string, unknown>;
|
|
43
|
+
}>;
|
|
44
|
+
/** Aggregate token usage reported by the run. */
|
|
45
|
+
usage: {
|
|
46
|
+
inputTokens: number;
|
|
47
|
+
outputTokens: number;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -64,7 +64,7 @@ export interface GoalResult {
|
|
|
64
64
|
finalResponse: string | undefined;
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
|
-
* Return type of {@link import("../internal/runtime/local-agent.js").LocalAgent.runUntil}.
|
|
67
|
+
* Return type of {@link import("../internal/runtime/local-agent/local-agent.js").LocalAgent.runUntil}.
|
|
68
68
|
* Extracted so the LocalAgent method signature stays a single line (G8 LoC budget).
|
|
69
69
|
*
|
|
70
70
|
* @public
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theokit/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "TypeScript SDK for the Theo agent harness — same surface, local or cloud.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/usetheo/theokit-sdk#readme",
|
|
@@ -158,16 +158,6 @@
|
|
|
158
158
|
"default": "./dist/internal/security/index.cjs"
|
|
159
159
|
}
|
|
160
160
|
},
|
|
161
|
-
"./rag": {
|
|
162
|
-
"import": {
|
|
163
|
-
"types": "./dist/rag/index.d.ts",
|
|
164
|
-
"default": "./dist/rag/index.js"
|
|
165
|
-
},
|
|
166
|
-
"require": {
|
|
167
|
-
"types": "./dist/rag/index.d.cts",
|
|
168
|
-
"default": "./dist/rag/index.cjs"
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
161
|
"./a2a": {
|
|
172
162
|
"import": {
|
|
173
163
|
"types": "./dist/a2a/index.d.ts",
|
|
@@ -214,15 +204,6 @@
|
|
|
214
204
|
"theokit-migrate-memory": "./bin/theokit-migrate-memory.mjs"
|
|
215
205
|
},
|
|
216
206
|
"sideEffects": false,
|
|
217
|
-
"scripts": {
|
|
218
|
-
"build": "tsup && cp src/internal/providers/provider-catalog.json dist/provider-catalog.json",
|
|
219
|
-
"test": "vitest run --no-file-parallelism",
|
|
220
|
-
"test:watch": "vitest",
|
|
221
|
-
"typecheck": "tsc --noEmit",
|
|
222
|
-
"clean": "rm -rf dist",
|
|
223
|
-
"docs:json": "typedoc --options typedoc.json",
|
|
224
|
-
"docs:drift": "tsx scripts/check-docs-drift.ts"
|
|
225
|
-
},
|
|
226
207
|
"peerDependencies": {
|
|
227
208
|
"@lancedb/lancedb": "^0.30.0",
|
|
228
209
|
"@types/ws": ">=8.0.0",
|
|
@@ -256,8 +237,7 @@
|
|
|
256
237
|
}
|
|
257
238
|
},
|
|
258
239
|
"publishConfig": {
|
|
259
|
-
"access": "public"
|
|
260
|
-
"provenance": true
|
|
240
|
+
"access": "public"
|
|
261
241
|
},
|
|
262
242
|
"keywords": [
|
|
263
243
|
"agent",
|
|
@@ -280,8 +260,6 @@
|
|
|
280
260
|
"devDependencies": {
|
|
281
261
|
"@opentelemetry/api": "^1.9.1",
|
|
282
262
|
"@opentelemetry/sdk-trace-base": "^1.30.1",
|
|
283
|
-
"@theokit/sdk-handoff": "workspace:*",
|
|
284
|
-
"@theokit/sdk-memory": "workspace:*",
|
|
285
263
|
"@types/better-sqlite3": "^7.6.13",
|
|
286
264
|
"@types/proper-lockfile": "^4.1.4",
|
|
287
265
|
"@types/ws": "^8.18.0",
|
|
@@ -291,6 +269,17 @@
|
|
|
291
269
|
"sqlite-vec": "^0.1.9",
|
|
292
270
|
"typedoc": "^0.28.19",
|
|
293
271
|
"ws": "^8.18.0",
|
|
294
|
-
"zod": "^4.0.0"
|
|
272
|
+
"zod": "^4.0.0",
|
|
273
|
+
"@theokit/sdk-handoff": "0.1.0",
|
|
274
|
+
"@theokit/sdk-memory": "0.1.0"
|
|
275
|
+
},
|
|
276
|
+
"scripts": {
|
|
277
|
+
"build": "tsup && cp src/internal/providers/provider-catalog.json dist/provider-catalog.json",
|
|
278
|
+
"test": "vitest run --no-file-parallelism",
|
|
279
|
+
"test:watch": "vitest",
|
|
280
|
+
"typecheck": "tsc --noEmit",
|
|
281
|
+
"clean": "rm -rf dist",
|
|
282
|
+
"docs:json": "typedoc --options typedoc.json",
|
|
283
|
+
"docs:drift": "tsx scripts/check-docs-drift.ts"
|
|
295
284
|
}
|
|
296
|
-
}
|
|
285
|
+
}
|
package/dist/rag/index.cjs
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/rag/reranker.ts
|
|
4
|
-
var NoopReranker = class {
|
|
5
|
-
async rerank(_query, chunks) {
|
|
6
|
-
return chunks.map((c, i) => ({
|
|
7
|
-
text: c.text,
|
|
8
|
-
score: c.score,
|
|
9
|
-
originalIndex: i,
|
|
10
|
-
metadata: c.metadata
|
|
11
|
-
}));
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var CohereReranker = class {
|
|
15
|
-
_apiKey;
|
|
16
|
-
_model;
|
|
17
|
-
_mockRerank;
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
this._apiKey = opts.apiKey;
|
|
20
|
-
this._model = opts.model ?? "rerank-v3.5";
|
|
21
|
-
this._mockRerank = opts._mockRerank;
|
|
22
|
-
}
|
|
23
|
-
async rerank(query, chunks) {
|
|
24
|
-
if (chunks.length === 0) return [];
|
|
25
|
-
if (this._mockRerank) {
|
|
26
|
-
return this._mockRerank(query, chunks);
|
|
27
|
-
}
|
|
28
|
-
const response = await fetch("https://api.cohere.com/v2/rerank", {
|
|
29
|
-
method: "POST",
|
|
30
|
-
headers: {
|
|
31
|
-
Authorization: `Bearer ${this._apiKey}`,
|
|
32
|
-
"Content-Type": "application/json"
|
|
33
|
-
},
|
|
34
|
-
body: JSON.stringify({
|
|
35
|
-
model: this._model,
|
|
36
|
-
query,
|
|
37
|
-
documents: chunks.map((c) => c.text),
|
|
38
|
-
top_n: chunks.length
|
|
39
|
-
})
|
|
40
|
-
});
|
|
41
|
-
if (!response.ok) {
|
|
42
|
-
throw new Error(`Cohere rerank failed: ${response.status} ${response.statusText}`);
|
|
43
|
-
}
|
|
44
|
-
const data = await response.json();
|
|
45
|
-
return data.results.map((r) => {
|
|
46
|
-
const chunk = chunks[r.index];
|
|
47
|
-
return {
|
|
48
|
-
text: chunk?.text ?? "",
|
|
49
|
-
score: r.relevance_score,
|
|
50
|
-
originalIndex: r.index,
|
|
51
|
-
metadata: chunk?.metadata
|
|
52
|
-
};
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// src/rag/retriever.ts
|
|
58
|
-
var VectorRetriever = class {
|
|
59
|
-
_index;
|
|
60
|
-
_defaultTopK;
|
|
61
|
-
constructor(opts) {
|
|
62
|
-
this._index = opts.index;
|
|
63
|
-
this._defaultTopK = opts.topK ?? 5;
|
|
64
|
-
}
|
|
65
|
-
async retrieve(query, options) {
|
|
66
|
-
const topK = options?.topK ?? this._defaultTopK;
|
|
67
|
-
return this._index.search(query, topK);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// src/rag/text-splitter.ts
|
|
72
|
-
function makeChunks(texts) {
|
|
73
|
-
return texts.filter((t) => t.length > 0).map((text, index) => ({ text, index }));
|
|
74
|
-
}
|
|
75
|
-
function splitByCharacter(text, opts) {
|
|
76
|
-
if (text.length === 0) return [];
|
|
77
|
-
const { chunkSize, overlap = 0 } = opts;
|
|
78
|
-
const step = Math.max(1, chunkSize - overlap);
|
|
79
|
-
const parts = [];
|
|
80
|
-
for (let i = 0; i < text.length; i += step) {
|
|
81
|
-
parts.push(text.slice(i, i + chunkSize));
|
|
82
|
-
}
|
|
83
|
-
return makeChunks(parts);
|
|
84
|
-
}
|
|
85
|
-
var SENTENCE_RE = /(?<=[.!?])\s+/;
|
|
86
|
-
function splitBySentence(text, opts) {
|
|
87
|
-
if (text.length === 0) return [];
|
|
88
|
-
const { chunkSize } = opts;
|
|
89
|
-
const sentences = text.split(SENTENCE_RE);
|
|
90
|
-
const parts = [];
|
|
91
|
-
let current = "";
|
|
92
|
-
for (const sentence of sentences) {
|
|
93
|
-
if (current.length + sentence.length + 1 > chunkSize && current.length > 0) {
|
|
94
|
-
parts.push(current.trim());
|
|
95
|
-
current = sentence;
|
|
96
|
-
} else {
|
|
97
|
-
current = current.length > 0 ? `${current} ${sentence}` : sentence;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (current.length > 0) parts.push(current.trim());
|
|
101
|
-
return makeChunks(parts);
|
|
102
|
-
}
|
|
103
|
-
var PARAGRAPH_RE = /\n\n+/;
|
|
104
|
-
function splitRecursive(text, opts) {
|
|
105
|
-
if (text.length === 0) return [];
|
|
106
|
-
const { chunkSize, overlap = 0 } = opts;
|
|
107
|
-
const paragraphs = text.split(PARAGRAPH_RE).filter((p) => p.length > 0);
|
|
108
|
-
const result = [];
|
|
109
|
-
for (const para of paragraphs) {
|
|
110
|
-
if (para.length <= chunkSize) {
|
|
111
|
-
result.push(para);
|
|
112
|
-
} else {
|
|
113
|
-
const sentenceChunks = splitBySentence(para, { chunkSize});
|
|
114
|
-
for (const sc of sentenceChunks) {
|
|
115
|
-
if (sc.text.length <= chunkSize) {
|
|
116
|
-
result.push(sc.text);
|
|
117
|
-
} else {
|
|
118
|
-
const charChunks = splitByCharacter(sc.text, { chunkSize, overlap });
|
|
119
|
-
for (const cc of charChunks) {
|
|
120
|
-
result.push(cc.text);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return makeChunks(result);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
exports.CohereReranker = CohereReranker;
|
|
130
|
-
exports.NoopReranker = NoopReranker;
|
|
131
|
-
exports.VectorRetriever = VectorRetriever;
|
|
132
|
-
exports.splitByCharacter = splitByCharacter;
|
|
133
|
-
exports.splitBySentence = splitBySentence;
|
|
134
|
-
exports.splitRecursive = splitRecursive;
|
|
135
|
-
//# sourceMappingURL=index.cjs.map
|
|
136
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/rag/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/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A;AACnE,UAAA,KAAA,MAAW,MAAM,UAAA,EAAY;AAC3B,YAAA,MAAA,CAAO,IAAA,CAAK,GAAG,IAAI,CAAA;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAO,WAAW,MAAM,CAAA;AAC1B","file":"index.cjs","sourcesContent":["/**\n * Reranker implementations for RAG pipelines (T11.1, ADR D448).\n *\n * NoopReranker: passes through results unchanged (baseline).\n * CohereReranker: calls the Cohere Rerank API (optional peer dep).\n *\n * @public\n */\n\nimport type { RankedChunk, Reranker, RetrievalResult } from \"./types.js\";\n\nexport class NoopReranker implements Reranker {\n async rerank(_query: string, chunks: RetrievalResult[]): Promise<RankedChunk[]> {\n return chunks.map((c, i) => ({\n text: c.text,\n score: c.score,\n originalIndex: i,\n metadata: c.metadata,\n }));\n }\n}\n\ntype MockRerankFn = (query: string, docs: RetrievalResult[]) => Promise<RankedChunk[]>;\n\nexport interface CohereRerankerOptions {\n apiKey: string;\n model?: string;\n _mockRerank?: MockRerankFn;\n}\n\nexport class CohereReranker implements Reranker {\n private readonly _apiKey: string;\n private readonly _model: string;\n private readonly _mockRerank?: MockRerankFn;\n\n constructor(opts: CohereRerankerOptions) {\n this._apiKey = opts.apiKey;\n this._model = opts.model ?? \"rerank-v3.5\";\n this._mockRerank = opts._mockRerank;\n }\n\n async rerank(query: string, chunks: RetrievalResult[]): Promise<RankedChunk[]> {\n if (chunks.length === 0) return [];\n\n if (this._mockRerank) {\n return this._mockRerank(query, chunks);\n }\n\n const response = await fetch(\"https://api.cohere.com/v2/rerank\", {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this._apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n model: this._model,\n query,\n documents: chunks.map((c) => c.text),\n top_n: chunks.length,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Cohere rerank failed: ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as {\n results: Array<{ index: number; relevance_score: number }>;\n };\n\n return data.results.map((r) => {\n const chunk = chunks[r.index];\n return {\n text: chunk?.text ?? \"\",\n score: r.relevance_score,\n originalIndex: r.index,\n metadata: chunk?.metadata,\n };\n });\n }\n}\n","/**\n * Retriever implementations for RAG pipelines (T11.1, ADR D448).\n *\n * VectorRetriever wraps any index that implements `search(query, topK)`.\n * The Retriever interface is the contract — consumers depend on the interface,\n * implementations depend on the index adapter (DIP per architecture.md § 2).\n *\n * @public\n */\n\nimport type { RetrievalResult, Retriever } from \"./types.js\";\n\nexport interface VectorIndex {\n search(query: string, topK: number): Promise<RetrievalResult[]>;\n}\n\nexport interface VectorRetrieverOptions {\n index: VectorIndex;\n topK?: number;\n}\n\nexport class VectorRetriever implements Retriever {\n private readonly _index: VectorIndex;\n private readonly _defaultTopK: number;\n\n constructor(opts: VectorRetrieverOptions) {\n this._index = opts.index;\n this._defaultTopK = opts.topK ?? 5;\n }\n\n async retrieve(query: string, options?: { topK?: number }): Promise<RetrievalResult[]> {\n const topK = options?.topK ?? this._defaultTopK;\n return this._index.search(query, topK);\n }\n}\n","/**\n * Text splitting strategies for RAG pipelines (T11.1, ADR D448).\n *\n * Three strategies: character, sentence, recursive (paragraph→sentence→char).\n * All return Chunk[] with text + index. Empty input → empty array (EC-5).\n *\n * @public\n */\n\nimport type { Chunk, SplitOptions } from \"./types.js\";\n\nfunction makeChunks(texts: string[]): Chunk[] {\n return texts.filter((t) => t.length > 0).map((text, index) => ({ text, index }));\n}\n\nexport function splitByCharacter(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize, overlap = 0 } = opts;\n const step = Math.max(1, chunkSize - overlap);\n const parts: string[] = [];\n for (let i = 0; i < text.length; i += step) {\n parts.push(text.slice(i, i + chunkSize));\n }\n return makeChunks(parts);\n}\n\nconst SENTENCE_RE = /(?<=[.!?])\\s+/;\n\nexport function splitBySentence(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize } = opts;\n const sentences = text.split(SENTENCE_RE);\n const parts: string[] = [];\n let current = \"\";\n for (const sentence of sentences) {\n if (current.length + sentence.length + 1 > chunkSize && current.length > 0) {\n parts.push(current.trim());\n current = sentence;\n } else {\n current = current.length > 0 ? `${current} ${sentence}` : sentence;\n }\n }\n if (current.length > 0) parts.push(current.trim());\n return makeChunks(parts);\n}\n\nconst PARAGRAPH_RE = /\\n\\n+/;\n\n// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: recursive splitter inherently cascades through 3 levels (paragraph→sentence→char)\nexport function splitRecursive(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize, overlap = 0 } = opts;\n\n // Level 1: split by paragraph\n const paragraphs = text.split(PARAGRAPH_RE).filter((p) => p.length > 0);\n const result: string[] = [];\n\n for (const para of paragraphs) {\n if (para.length <= chunkSize) {\n result.push(para);\n } else {\n // Level 2: split by sentence\n const sentenceChunks = splitBySentence(para, { chunkSize, overlap });\n for (const sc of sentenceChunks) {\n if (sc.text.length <= chunkSize) {\n result.push(sc.text);\n } else {\n // Level 3: split by character\n const charChunks = splitByCharacter(sc.text, { chunkSize, overlap });\n for (const cc of charChunks) {\n result.push(cc.text);\n }\n }\n }\n }\n }\n\n return makeChunks(result);\n}\n"]}
|
package/dist/rag/index.d.cts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* RAG (Retrieval-Augmented Generation) sub-path barrel (T11.1, ADR D448).
|
|
3
|
-
*
|
|
4
|
-
* Exported via `@theokit/sdk/rag`.
|
|
5
|
-
*
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export { CohereReranker, type CohereRerankerOptions, NoopReranker } from "./reranker.js";
|
|
9
|
-
export { type VectorIndex, VectorRetriever, type VectorRetrieverOptions } from "./retriever.js";
|
|
10
|
-
export { splitByCharacter, splitBySentence, splitRecursive } from "./text-splitter.js";
|
|
11
|
-
export type { Chunk, Document, RankedChunk, Reranker, RetrievalResult, Retriever, SplitOptions, } from "./types.js";
|
package/dist/rag/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* RAG (Retrieval-Augmented Generation) sub-path barrel (T11.1, ADR D448).
|
|
3
|
-
*
|
|
4
|
-
* Exported via `@theokit/sdk/rag`.
|
|
5
|
-
*
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export { CohereReranker, type CohereRerankerOptions, NoopReranker } from "./reranker.js";
|
|
9
|
-
export { type VectorIndex, VectorRetriever, type VectorRetrieverOptions } from "./retriever.js";
|
|
10
|
-
export { splitByCharacter, splitBySentence, splitRecursive } from "./text-splitter.js";
|
|
11
|
-
export type { Chunk, Document, RankedChunk, Reranker, RetrievalResult, Retriever, SplitOptions, } from "./types.js";
|
package/dist/rag/index.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// src/rag/reranker.ts
|
|
2
|
-
var NoopReranker = class {
|
|
3
|
-
async rerank(_query, chunks) {
|
|
4
|
-
return chunks.map((c, i) => ({
|
|
5
|
-
text: c.text,
|
|
6
|
-
score: c.score,
|
|
7
|
-
originalIndex: i,
|
|
8
|
-
metadata: c.metadata
|
|
9
|
-
}));
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
var CohereReranker = class {
|
|
13
|
-
_apiKey;
|
|
14
|
-
_model;
|
|
15
|
-
_mockRerank;
|
|
16
|
-
constructor(opts) {
|
|
17
|
-
this._apiKey = opts.apiKey;
|
|
18
|
-
this._model = opts.model ?? "rerank-v3.5";
|
|
19
|
-
this._mockRerank = opts._mockRerank;
|
|
20
|
-
}
|
|
21
|
-
async rerank(query, chunks) {
|
|
22
|
-
if (chunks.length === 0) return [];
|
|
23
|
-
if (this._mockRerank) {
|
|
24
|
-
return this._mockRerank(query, chunks);
|
|
25
|
-
}
|
|
26
|
-
const response = await fetch("https://api.cohere.com/v2/rerank", {
|
|
27
|
-
method: "POST",
|
|
28
|
-
headers: {
|
|
29
|
-
Authorization: `Bearer ${this._apiKey}`,
|
|
30
|
-
"Content-Type": "application/json"
|
|
31
|
-
},
|
|
32
|
-
body: JSON.stringify({
|
|
33
|
-
model: this._model,
|
|
34
|
-
query,
|
|
35
|
-
documents: chunks.map((c) => c.text),
|
|
36
|
-
top_n: chunks.length
|
|
37
|
-
})
|
|
38
|
-
});
|
|
39
|
-
if (!response.ok) {
|
|
40
|
-
throw new Error(`Cohere rerank failed: ${response.status} ${response.statusText}`);
|
|
41
|
-
}
|
|
42
|
-
const data = await response.json();
|
|
43
|
-
return data.results.map((r) => {
|
|
44
|
-
const chunk = chunks[r.index];
|
|
45
|
-
return {
|
|
46
|
-
text: chunk?.text ?? "",
|
|
47
|
-
score: r.relevance_score,
|
|
48
|
-
originalIndex: r.index,
|
|
49
|
-
metadata: chunk?.metadata
|
|
50
|
-
};
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// src/rag/retriever.ts
|
|
56
|
-
var VectorRetriever = class {
|
|
57
|
-
_index;
|
|
58
|
-
_defaultTopK;
|
|
59
|
-
constructor(opts) {
|
|
60
|
-
this._index = opts.index;
|
|
61
|
-
this._defaultTopK = opts.topK ?? 5;
|
|
62
|
-
}
|
|
63
|
-
async retrieve(query, options) {
|
|
64
|
-
const topK = options?.topK ?? this._defaultTopK;
|
|
65
|
-
return this._index.search(query, topK);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
// src/rag/text-splitter.ts
|
|
70
|
-
function makeChunks(texts) {
|
|
71
|
-
return texts.filter((t) => t.length > 0).map((text, index) => ({ text, index }));
|
|
72
|
-
}
|
|
73
|
-
function splitByCharacter(text, opts) {
|
|
74
|
-
if (text.length === 0) return [];
|
|
75
|
-
const { chunkSize, overlap = 0 } = opts;
|
|
76
|
-
const step = Math.max(1, chunkSize - overlap);
|
|
77
|
-
const parts = [];
|
|
78
|
-
for (let i = 0; i < text.length; i += step) {
|
|
79
|
-
parts.push(text.slice(i, i + chunkSize));
|
|
80
|
-
}
|
|
81
|
-
return makeChunks(parts);
|
|
82
|
-
}
|
|
83
|
-
var SENTENCE_RE = /(?<=[.!?])\s+/;
|
|
84
|
-
function splitBySentence(text, opts) {
|
|
85
|
-
if (text.length === 0) return [];
|
|
86
|
-
const { chunkSize } = opts;
|
|
87
|
-
const sentences = text.split(SENTENCE_RE);
|
|
88
|
-
const parts = [];
|
|
89
|
-
let current = "";
|
|
90
|
-
for (const sentence of sentences) {
|
|
91
|
-
if (current.length + sentence.length + 1 > chunkSize && current.length > 0) {
|
|
92
|
-
parts.push(current.trim());
|
|
93
|
-
current = sentence;
|
|
94
|
-
} else {
|
|
95
|
-
current = current.length > 0 ? `${current} ${sentence}` : sentence;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (current.length > 0) parts.push(current.trim());
|
|
99
|
-
return makeChunks(parts);
|
|
100
|
-
}
|
|
101
|
-
var PARAGRAPH_RE = /\n\n+/;
|
|
102
|
-
function splitRecursive(text, opts) {
|
|
103
|
-
if (text.length === 0) return [];
|
|
104
|
-
const { chunkSize, overlap = 0 } = opts;
|
|
105
|
-
const paragraphs = text.split(PARAGRAPH_RE).filter((p) => p.length > 0);
|
|
106
|
-
const result = [];
|
|
107
|
-
for (const para of paragraphs) {
|
|
108
|
-
if (para.length <= chunkSize) {
|
|
109
|
-
result.push(para);
|
|
110
|
-
} else {
|
|
111
|
-
const sentenceChunks = splitBySentence(para, { chunkSize});
|
|
112
|
-
for (const sc of sentenceChunks) {
|
|
113
|
-
if (sc.text.length <= chunkSize) {
|
|
114
|
-
result.push(sc.text);
|
|
115
|
-
} else {
|
|
116
|
-
const charChunks = splitByCharacter(sc.text, { chunkSize, overlap });
|
|
117
|
-
for (const cc of charChunks) {
|
|
118
|
-
result.push(cc.text);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return makeChunks(result);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export { CohereReranker, NoopReranker, VectorRetriever, splitByCharacter, splitBySentence, splitRecursive };
|
|
128
|
-
//# sourceMappingURL=index.js.map
|
|
129
|
-
//# sourceMappingURL=index.js.map
|
package/dist/rag/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/rag/reranker.ts","../../src/rag/retriever.ts","../../src/rag/text-splitter.ts"],"names":[],"mappings":";AAWO,IAAM,eAAN,MAAuC;AAAA,EAC5C,MAAM,MAAA,CAAO,MAAA,EAAgB,MAAA,EAAmD;AAC9E,IAAA,OAAO,MAAA,CAAO,GAAA,CAAI,CAAC,CAAA,EAAG,CAAA,MAAO;AAAA,MAC3B,MAAM,CAAA,CAAE,IAAA;AAAA,MACR,OAAO,CAAA,CAAE,KAAA;AAAA,MACT,aAAA,EAAe,CAAA;AAAA,MACf,UAAU,CAAA,CAAE;AAAA,KACd,CAAE,CAAA;AAAA,EACJ;AACF;AAUO,IAAM,iBAAN,MAAyC;AAAA,EAC7B,OAAA;AAAA,EACA,MAAA;AAAA,EACA,WAAA;AAAA,EAEjB,YAAY,IAAA,EAA6B;AACvC,IAAA,IAAA,CAAK,UAAU,IAAA,CAAK,MAAA;AACpB,IAAA,IAAA,CAAK,MAAA,GAAS,KAAK,KAAA,IAAS,aAAA;AAC5B,IAAA,IAAA,CAAK,cAAc,IAAA,CAAK,WAAA;AAAA,EAC1B;AAAA,EAEA,MAAM,MAAA,CAAO,KAAA,EAAe,MAAA,EAAmD;AAC7E,IAAA,IAAI,MAAA,CAAO,MAAA,KAAW,CAAA,EAAG,OAAO,EAAC;AAEjC,IAAA,IAAI,KAAK,WAAA,EAAa;AACpB,MAAA,OAAO,IAAA,CAAK,WAAA,CAAY,KAAA,EAAO,MAAM,CAAA;AAAA,IACvC;AAEA,IAAA,MAAM,QAAA,GAAW,MAAM,KAAA,CAAM,kCAAA,EAAoC;AAAA,MAC/D,MAAA,EA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AgB;AAC/B,QAAA,IAAI,EAAA,CAAG,IAAA,CAAK,MAAA,IAAU,SAAA,EAAW;AAC/B,UAAA,MAAA,CAAO,IAAA,CAAK,GAAG,IAAI,CAAA;AAAA,QACrB,CAAA,MAAO;AAEL,UAAA,MAAM,aAAa,gBAAA,CAAiB,EAAA,CAAG,MAAM,EAAE,SAAA,EAAW,SAAS,CAAA;AACnE,UAAA,KAAA,MAAW,MAAM,UAAA,EAAY;AAC3B,YAAA,MAAA,CAAO,IAAA,CAAK,GAAG,IAAI,CAAA;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAO,WAAW,MAAM,CAAA;AAC1B","file":"index.js","sourcesContent":["/**\n * Reranker implementations for RAG pipelines (T11.1, ADR D448).\n *\n * NoopReranker: passes through results unchanged (baseline).\n * CohereReranker: calls the Cohere Rerank API (optional peer dep).\n *\n * @public\n */\n\nimport type { RankedChunk, Reranker, RetrievalResult } from \"./types.js\";\n\nexport class NoopReranker implements Reranker {\n async rerank(_query: string, chunks: RetrievalResult[]): Promise<RankedChunk[]> {\n return chunks.map((c, i) => ({\n text: c.text,\n score: c.score,\n originalIndex: i,\n metadata: c.metadata,\n }));\n }\n}\n\ntype MockRerankFn = (query: string, docs: RetrievalResult[]) => Promise<RankedChunk[]>;\n\nexport interface CohereRerankerOptions {\n apiKey: string;\n model?: string;\n _mockRerank?: MockRerankFn;\n}\n\nexport class CohereReranker implements Reranker {\n private readonly _apiKey: string;\n private readonly _model: string;\n private readonly _mockRerank?: MockRerankFn;\n\n constructor(opts: CohereRerankerOptions) {\n this._apiKey = opts.apiKey;\n this._model = opts.model ?? \"rerank-v3.5\";\n this._mockRerank = opts._mockRerank;\n }\n\n async rerank(query: string, chunks: RetrievalResult[]): Promise<RankedChunk[]> {\n if (chunks.length === 0) return [];\n\n if (this._mockRerank) {\n return this._mockRerank(query, chunks);\n }\n\n const response = await fetch(\"https://api.cohere.com/v2/rerank\", {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this._apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n model: this._model,\n query,\n documents: chunks.map((c) => c.text),\n top_n: chunks.length,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Cohere rerank failed: ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as {\n results: Array<{ index: number; relevance_score: number }>;\n };\n\n return data.results.map((r) => {\n const chunk = chunks[r.index];\n return {\n text: chunk?.text ?? \"\",\n score: r.relevance_score,\n originalIndex: r.index,\n metadata: chunk?.metadata,\n };\n });\n }\n}\n","/**\n * Retriever implementations for RAG pipelines (T11.1, ADR D448).\n *\n * VectorRetriever wraps any index that implements `search(query, topK)`.\n * The Retriever interface is the contract — consumers depend on the interface,\n * implementations depend on the index adapter (DIP per architecture.md § 2).\n *\n * @public\n */\n\nimport type { RetrievalResult, Retriever } from \"./types.js\";\n\nexport interface VectorIndex {\n search(query: string, topK: number): Promise<RetrievalResult[]>;\n}\n\nexport interface VectorRetrieverOptions {\n index: VectorIndex;\n topK?: number;\n}\n\nexport class VectorRetriever implements Retriever {\n private readonly _index: VectorIndex;\n private readonly _defaultTopK: number;\n\n constructor(opts: VectorRetrieverOptions) {\n this._index = opts.index;\n this._defaultTopK = opts.topK ?? 5;\n }\n\n async retrieve(query: string, options?: { topK?: number }): Promise<RetrievalResult[]> {\n const topK = options?.topK ?? this._defaultTopK;\n return this._index.search(query, topK);\n }\n}\n","/**\n * Text splitting strategies for RAG pipelines (T11.1, ADR D448).\n *\n * Three strategies: character, sentence, recursive (paragraph→sentence→char).\n * All return Chunk[] with text + index. Empty input → empty array (EC-5).\n *\n * @public\n */\n\nimport type { Chunk, SplitOptions } from \"./types.js\";\n\nfunction makeChunks(texts: string[]): Chunk[] {\n return texts.filter((t) => t.length > 0).map((text, index) => ({ text, index }));\n}\n\nexport function splitByCharacter(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize, overlap = 0 } = opts;\n const step = Math.max(1, chunkSize - overlap);\n const parts: string[] = [];\n for (let i = 0; i < text.length; i += step) {\n parts.push(text.slice(i, i + chunkSize));\n }\n return makeChunks(parts);\n}\n\nconst SENTENCE_RE = /(?<=[.!?])\\s+/;\n\nexport function splitBySentence(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize } = opts;\n const sentences = text.split(SENTENCE_RE);\n const parts: string[] = [];\n let current = \"\";\n for (const sentence of sentences) {\n if (current.length + sentence.length + 1 > chunkSize && current.length > 0) {\n parts.push(current.trim());\n current = sentence;\n } else {\n current = current.length > 0 ? `${current} ${sentence}` : sentence;\n }\n }\n if (current.length > 0) parts.push(current.trim());\n return makeChunks(parts);\n}\n\nconst PARAGRAPH_RE = /\\n\\n+/;\n\n// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: recursive splitter inherently cascades through 3 levels (paragraph→sentence→char)\nexport function splitRecursive(text: string, opts: SplitOptions): Chunk[] {\n if (text.length === 0) return [];\n const { chunkSize, overlap = 0 } = opts;\n\n // Level 1: split by paragraph\n const paragraphs = text.split(PARAGRAPH_RE).filter((p) => p.length > 0);\n const result: string[] = [];\n\n for (const para of paragraphs) {\n if (para.length <= chunkSize) {\n result.push(para);\n } else {\n // Level 2: split by sentence\n const sentenceChunks = splitBySentence(para, { chunkSize, overlap });\n for (const sc of sentenceChunks) {\n if (sc.text.length <= chunkSize) {\n result.push(sc.text);\n } else {\n // Level 3: split by character\n const charChunks = splitByCharacter(sc.text, { chunkSize, overlap });\n for (const cc of charChunks) {\n result.push(cc.text);\n }\n }\n }\n }\n }\n\n return makeChunks(result);\n}\n"]}
|