@modular-prompt/extract 0.2.0 → 0.3.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.
Files changed (69) hide show
  1. package/README.md +99 -23
  2. package/dist/cache-lifecycle.d.ts +5 -2
  3. package/dist/cache-lifecycle.d.ts.map +1 -1
  4. package/dist/cache-lifecycle.js +11 -7
  5. package/dist/cache-lifecycle.js.map +1 -1
  6. package/dist/cli/add-command.d.ts +11 -0
  7. package/dist/cli/add-command.d.ts.map +1 -0
  8. package/dist/cli/add-command.js +29 -0
  9. package/dist/cli/add-command.js.map +1 -0
  10. package/dist/cli/args.d.ts +14 -0
  11. package/dist/cli/args.d.ts.map +1 -0
  12. package/dist/cli/args.js +125 -0
  13. package/dist/cli/args.js.map +1 -0
  14. package/dist/cli/clean-command.d.ts +10 -0
  15. package/dist/cli/clean-command.d.ts.map +1 -0
  16. package/dist/cli/clean-command.js +26 -0
  17. package/dist/cli/clean-command.js.map +1 -0
  18. package/dist/cli/constants.d.ts +4 -4
  19. package/dist/cli/constants.d.ts.map +1 -1
  20. package/dist/cli/constants.js +8 -4
  21. package/dist/cli/constants.js.map +1 -1
  22. package/dist/cli/create-command.d.ts +3 -1
  23. package/dist/cli/create-command.d.ts.map +1 -1
  24. package/dist/cli/create-command.js +29 -28
  25. package/dist/cli/create-command.js.map +1 -1
  26. package/dist/cli/extract-command.d.ts +2 -0
  27. package/dist/cli/extract-command.d.ts.map +1 -1
  28. package/dist/cli/extract-command.js +5 -3
  29. package/dist/cli/extract-command.js.map +1 -1
  30. package/dist/cli/list-command.d.ts +15 -0
  31. package/dist/cli/list-command.d.ts.map +1 -0
  32. package/dist/cli/list-command.js +69 -0
  33. package/dist/cli/list-command.js.map +1 -0
  34. package/dist/cli/manifest.d.ts +3 -0
  35. package/dist/cli/manifest.d.ts.map +1 -1
  36. package/dist/cli/manifest.js +4 -1
  37. package/dist/cli/manifest.js.map +1 -1
  38. package/dist/cli/store.d.ts +9 -0
  39. package/dist/cli/store.d.ts.map +1 -0
  40. package/dist/cli/store.js +36 -0
  41. package/dist/cli/store.js.map +1 -0
  42. package/dist/cli.js +62 -73
  43. package/dist/cli.js.map +1 -1
  44. package/dist/create-extract-session.d.ts.map +1 -1
  45. package/dist/create-extract-session.js +3 -3
  46. package/dist/create-extract-session.js.map +1 -1
  47. package/dist/create-mlx-extract-runtime.d.ts +2 -1
  48. package/dist/create-mlx-extract-runtime.d.ts.map +1 -1
  49. package/dist/create-mlx-extract-runtime.js +47 -17
  50. package/dist/create-mlx-extract-runtime.js.map +1 -1
  51. package/dist/default-models.d.ts +14 -0
  52. package/dist/default-models.d.ts.map +1 -0
  53. package/dist/default-models.js +20 -0
  54. package/dist/default-models.js.map +1 -0
  55. package/dist/extract-store.d.ts +53 -0
  56. package/dist/extract-store.d.ts.map +1 -0
  57. package/dist/extract-store.js +174 -0
  58. package/dist/extract-store.js.map +1 -0
  59. package/dist/index.d.ts +4 -0
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +3 -0
  62. package/dist/index.js.map +1 -1
  63. package/dist/model-resolution.d.ts +25 -0
  64. package/dist/model-resolution.d.ts.map +1 -0
  65. package/dist/model-resolution.js +57 -0
  66. package/dist/model-resolution.js.map +1 -0
  67. package/dist/types.d.ts +5 -0
  68. package/dist/types.d.ts.map +1 -1
  69. package/package.json +2 -2
@@ -1,22 +1,52 @@
1
- import { MlxCacheController, MlxDriver, } from '@modular-prompt/driver';
1
+ import { MlxCacheController, } from '@modular-prompt/driver';
2
+ import { createDriver } from './model-resolution.js';
3
+ /**
4
+ * Runtime construction failed after resources started being created.
5
+ * Cleanup must not replace the error that explains why construction failed.
6
+ */
7
+ async function closeFailedRuntime(driver, cacheController) {
8
+ try {
9
+ if (driver) {
10
+ await driver.close();
11
+ }
12
+ }
13
+ catch {
14
+ // Preserve the original runtime construction/capabilities error.
15
+ }
16
+ try {
17
+ await cacheController.close();
18
+ }
19
+ catch {
20
+ // Preserve the original runtime construction/capabilities error.
21
+ }
22
+ }
2
23
  export async function createMlxExtractRuntime(options) {
3
24
  const cacheController = new MlxCacheController(options.cacheDir ? { cacheDir: options.cacheDir } : undefined);
4
- const driver = new MlxDriver({
5
- model: options.model,
6
- backend: 'lm',
7
- cacheController,
8
- });
9
- if ('getCapabilities' in driver && typeof driver.getCapabilities === 'function') {
10
- await driver.getCapabilities();
25
+ let driverForCleanup;
26
+ try {
27
+ const resolved = await createDriver(options.model, { cacheController });
28
+ const driver = resolved.driver;
29
+ driverForCleanup = driver;
30
+ if ('getCapabilities' in driver && typeof driver.getCapabilities === 'function') {
31
+ await driver.getCapabilities();
32
+ }
33
+ return {
34
+ driver,
35
+ cacheController,
36
+ model: resolved.spec.model,
37
+ async close() {
38
+ try {
39
+ await driver.close();
40
+ }
41
+ finally {
42
+ await cacheController.close();
43
+ }
44
+ },
45
+ };
46
+ }
47
+ catch (error) {
48
+ await closeFailedRuntime(driverForCleanup, cacheController);
49
+ throw error;
11
50
  }
12
- return {
13
- driver,
14
- cacheController,
15
- model: options.model,
16
- async close() {
17
- await driver.close();
18
- await cacheController.close();
19
- },
20
- };
21
51
  }
22
52
  //# sourceMappingURL=create-mlx-extract-runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-mlx-extract-runtime.js","sourceRoot":"","sources":["../src/create-mlx-extract-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,SAAS,GAGV,MAAM,wBAAwB,CAAC;AAqBhC,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAAiC;IAEjC,MAAM,eAAe,GAAG,IAAI,kBAAkB,CAC5C,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAC9D,CAAC;IACF,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QAC3B,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,OAAO,EAAE,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,OAAO,CAAC,KAAK,EAAE,EAAE,eAAe,EAAE,CAAC,CAAC;QACxE,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;QAC/B,gBAAgB,GAAG,MAAM,CAAC;QAE1B,IAAI,iBAAiB,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,eAAe,KAAK,UAAU,EAAE,CAAC;YAChF,MAAM,MAAM,CAAC,eAAe,EAAE,CAAC;QACjC,CAAC;QAED,OAAO;YACL,MAAM;YACN,eAAe;YACf,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,KAAK;YAC1B,KAAK,CAAC,KAAK;gBACT,IAAI,CAAC;oBACH,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;gBACvB,CAAC;wBAAS,CAAC;oBACT,MAAM,eAAe,CAAC,KAAK,EAAE,CAAC;gBAChC,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,kBAAkB,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;QAC5D,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * extract 同梱の models 設定
3
+ *
4
+ * user の ~/.modular-prompt/models.yaml から alias や default を上書きできる。
5
+ */
6
+ import type { ModelsConfig } from '@modular-prompt/driver';
7
+ /**
8
+ * extract が -m なしで使用する同梱デフォルトモデル。
9
+ * MLX_MODEL は既存利用者向けに後方互換でサポートし、user yaml が優先される。
10
+ */
11
+ export declare const BUNDLED_DEFAULT_MODEL: string;
12
+ /** 同梱モデル設定(user models.yaml より低い優先度) */
13
+ export declare const BUNDLED_MODELS_CONFIG: ModelsConfig;
14
+ //# sourceMappingURL=default-models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-models.d.ts","sourceRoot":"","sources":["../src/default-models.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QACgC,CAAC;AAEnE,wCAAwC;AACxC,eAAO,MAAM,qBAAqB,EAAE,YAOnC,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * extract 同梱の models 設定
3
+ *
4
+ * user の ~/.modular-prompt/models.yaml から alias や default を上書きできる。
5
+ */
6
+ /**
7
+ * extract が -m なしで使用する同梱デフォルトモデル。
8
+ * MLX_MODEL は既存利用者向けに後方互換でサポートし、user yaml が優先される。
9
+ */
10
+ export const BUNDLED_DEFAULT_MODEL = process.env.MLX_MODEL ?? 'prism-ml/Ternary-Bonsai-1.7B-mlx-2bit';
11
+ /** 同梱モデル設定(user models.yaml より低い優先度) */
12
+ export const BUNDLED_MODELS_CONFIG = {
13
+ models: {
14
+ default: {
15
+ provider: 'mlx',
16
+ model: BUNDLED_DEFAULT_MODEL,
17
+ },
18
+ },
19
+ };
20
+ //# sourceMappingURL=default-models.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-models.js","sourceRoot":"","sources":["../src/default-models.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAChC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,uCAAuC,CAAC;AAEnE,wCAAwC;AACxC,MAAM,CAAC,MAAM,qBAAqB,GAAiB;IACjD,MAAM,EAAE;QACN,OAAO,EAAE;YACP,QAAQ,EAAE,KAAK;YACf,KAAK,EAAE,qBAAqB;SAC7B;KACF;CACF,CAAC"}
@@ -0,0 +1,53 @@
1
+ import type { MaterialInput } from './extract-elements.js';
2
+ import { type ExtractCacheManifest } from './cli/manifest.js';
3
+ export interface PrepareExtractCacheOptions {
4
+ /** Directory containing the persistent cache files. */
5
+ cacheDir: string;
6
+ /** MLX model alias or resolved model ID. */
7
+ model?: string;
8
+ /** Corpus to prefill into the cache. */
9
+ materials: readonly MaterialInput[];
10
+ }
11
+ /**
12
+ * Prepare the persistent corpus cache and release all runtime resources.
13
+ *
14
+ * The caller owns the manifest transaction. This operation only creates or
15
+ * extends the cache in `cacheDir` and returns the model ID resolved by MLX.
16
+ */
17
+ export declare function prepareExtractCache(options: PrepareExtractCacheOptions): Promise<string>;
18
+ /**
19
+ * Merge newly loaded materials into an existing corpus.
20
+ *
21
+ * File-based materials use their absolute path as `id`. Re-adding the same
22
+ * content is idempotent; changing the content behind an existing id is
23
+ * rejected because the persisted KV cache can no longer be trusted.
24
+ */
25
+ export declare function mergeMaterials(existing: readonly MaterialInput[], incoming: readonly MaterialInput[]): MaterialInput[];
26
+ /** Read and validate a named store manifest with a user-facing missing-store error. */
27
+ export declare function readExtractStoreManifest(storeDir: string, storename: string): Promise<ExtractCacheManifest>;
28
+ export interface AppendToExtractStoreOptions {
29
+ /** Existing named store directory. */
30
+ storeDir: string;
31
+ /** Store name used in actionable error messages. */
32
+ storename: string;
33
+ /** Materials loaded from the files supplied to `add`. */
34
+ incomingMaterials: readonly MaterialInput[];
35
+ /** Manifest read by the CLI before loading files, when available. */
36
+ existingManifest?: ExtractCacheManifest;
37
+ /** Injectable clock for deterministic callers and tests. */
38
+ now?: () => string;
39
+ }
40
+ export interface AppendToExtractStoreResult {
41
+ manifest: ExtractCacheManifest;
42
+ model: string;
43
+ addedMaterials: number;
44
+ }
45
+ /**
46
+ * Append materials to a persistent extract store transactionally.
47
+ *
48
+ * Prefill happens in a copied staging directory. The original store is only
49
+ * swapped after the new cache and manifest are both ready, so failures leave
50
+ * the old corpus/KV pair intact.
51
+ */
52
+ export declare function appendToExtractStore(options: AppendToExtractStoreOptions): Promise<AppendToExtractStoreResult>;
53
+ //# sourceMappingURL=extract-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-store.d.ts","sourceRoot":"","sources":["../src/extract-store.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAIL,KAAK,oBAAoB,EAC1B,MAAM,mBAAmB,CAAC;AAG3B,MAAM,WAAW,0BAA0B;IACzC,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,SAAS,EAAE,SAAS,aAAa,EAAE,CAAC;CACrC;AAeD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,0BAA0B,GAClC,OAAO,CAAC,MAAM,CAAC,CA8BjB;AAYD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,SAAS,aAAa,EAAE,EAClC,QAAQ,EAAE,SAAS,aAAa,EAAE,GACjC,aAAa,EAAE,CA6BjB;AASD,uFAAuF;AACvF,wBAAsB,wBAAwB,CAC5C,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,oBAAoB,CAAC,CAS/B;AAED,MAAM,WAAW,2BAA2B;IAC1C,sCAAsC;IACtC,QAAQ,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAC;IAClB,yDAAyD;IACzD,iBAAiB,EAAE,SAAS,aAAa,EAAE,CAAC;IAC5C,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,oBAAoB,CAAC;IACxC,4DAA4D;IAC5D,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,0BAA0B;IACzC,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;CACxB;AA+BD;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,2BAA2B,GACnC,OAAO,CAAC,0BAA0B,CAAC,CAuCrC"}
@@ -0,0 +1,174 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { cp, mkdtemp, rename, rm } from 'node:fs/promises';
3
+ import { basename, dirname, join, resolve } from 'node:path';
4
+ import { isDeepStrictEqual } from 'node:util';
5
+ import { createExtractSession } from './create-extract-session.js';
6
+ import { createMlxExtractRuntime } from './create-mlx-extract-runtime.js';
7
+ import { CACHE_PREPARE_CUE } from './cli/constants.js';
8
+ import { manifestExists, readManifest, writeManifest, } from './cli/manifest.js';
9
+ import { storeExists } from './cli/store.js';
10
+ async function closeRuntimePreservingError(runtime, operationError) {
11
+ try {
12
+ await runtime.close();
13
+ }
14
+ catch (closeError) {
15
+ if (operationError === undefined) {
16
+ throw closeError;
17
+ }
18
+ }
19
+ }
20
+ /**
21
+ * Prepare the persistent corpus cache and release all runtime resources.
22
+ *
23
+ * The caller owns the manifest transaction. This operation only creates or
24
+ * extends the cache in `cacheDir` and returns the model ID resolved by MLX.
25
+ */
26
+ export async function prepareExtractCache(options) {
27
+ const runtime = await createMlxExtractRuntime({
28
+ model: options.model,
29
+ cacheDir: options.cacheDir,
30
+ });
31
+ let operationError;
32
+ try {
33
+ const session = createExtractSession({
34
+ driver: runtime.driver,
35
+ cacheController: runtime.cacheController,
36
+ model: runtime.model,
37
+ corpus: { materials: options.materials },
38
+ cachePreparation: 'required',
39
+ });
40
+ await session.extract({
41
+ cue: CACHE_PREPARE_CUE,
42
+ options: { maxTokens: 1, temperature: 0 },
43
+ });
44
+ await session.close({ releaseCache: false });
45
+ return runtime.model;
46
+ }
47
+ catch (error) {
48
+ operationError = error;
49
+ throw error;
50
+ }
51
+ finally {
52
+ // Keep the prefill/session error as the primary failure. A close error
53
+ // is still surfaced when the operation itself succeeded.
54
+ await closeRuntimePreservingError(runtime, operationError);
55
+ }
56
+ }
57
+ function materialKey(material) {
58
+ return material.id ?? material.title;
59
+ }
60
+ function hasSameContent(left, right) {
61
+ return left.title === right.title
62
+ && left.usage === right.usage
63
+ && isDeepStrictEqual(left.content, right.content);
64
+ }
65
+ /**
66
+ * Merge newly loaded materials into an existing corpus.
67
+ *
68
+ * File-based materials use their absolute path as `id`. Re-adding the same
69
+ * content is idempotent; changing the content behind an existing id is
70
+ * rejected because the persisted KV cache can no longer be trusted.
71
+ */
72
+ export function mergeMaterials(existing, incoming) {
73
+ const merged = [...existing];
74
+ const indexById = new Map();
75
+ for (const [index, material] of existing.entries()) {
76
+ if (!indexById.has(materialKey(material))) {
77
+ indexById.set(materialKey(material), index);
78
+ }
79
+ }
80
+ for (const material of incoming) {
81
+ const id = materialKey(material);
82
+ const existingIndex = indexById.get(id);
83
+ if (existingIndex === undefined) {
84
+ indexById.set(id, merged.length);
85
+ merged.push(material);
86
+ continue;
87
+ }
88
+ const previous = merged[existingIndex];
89
+ if (previous && !hasSameContent(previous, material)) {
90
+ throw new Error(`Material content differs for duplicate id '${id}'. `
91
+ + 'Run `modular-extract clean <storename>` and create the store again.');
92
+ }
93
+ }
94
+ return merged;
95
+ }
96
+ function missingStoreError(storeDir, storename) {
97
+ return new Error(`Store not found: ${storeDir}\n`
98
+ + `Run \`modular-extract create ${storename} <files...>\` first.`);
99
+ }
100
+ /** Read and validate a named store manifest with a user-facing missing-store error. */
101
+ export async function readExtractStoreManifest(storeDir, storename) {
102
+ const resolvedStoreDir = resolve(storeDir);
103
+ if (!(await storeExists(resolvedStoreDir))) {
104
+ throw missingStoreError(resolvedStoreDir, storename);
105
+ }
106
+ if (!(await manifestExists(resolvedStoreDir))) {
107
+ throw new Error(`Store manifest not found: ${resolvedStoreDir}`);
108
+ }
109
+ return readManifest(resolvedStoreDir);
110
+ }
111
+ async function replaceStoreWithStaging(storeDir, stagingDir) {
112
+ const backupDir = join(dirname(storeDir), `.${basename(storeDir)}.backup-${randomUUID()}`);
113
+ await rename(storeDir, backupDir);
114
+ try {
115
+ await rename(stagingDir, storeDir);
116
+ }
117
+ catch (error) {
118
+ try {
119
+ await rename(backupDir, storeDir);
120
+ }
121
+ catch (rollbackError) {
122
+ throw new AggregateError([error, rollbackError], `Failed to replace store and roll back ${storeDir}`);
123
+ }
124
+ throw error;
125
+ }
126
+ // The new store is already committed. A failure to remove the old snapshot
127
+ // must not turn a successful add into a reported failure.
128
+ await rm(backupDir, { recursive: true, force: true }).catch(() => { });
129
+ }
130
+ /**
131
+ * Append materials to a persistent extract store transactionally.
132
+ *
133
+ * Prefill happens in a copied staging directory. The original store is only
134
+ * swapped after the new cache and manifest are both ready, so failures leave
135
+ * the old corpus/KV pair intact.
136
+ */
137
+ export async function appendToExtractStore(options) {
138
+ const storeDir = resolve(options.storeDir);
139
+ if (!(await storeExists(storeDir))) {
140
+ throw missingStoreError(storeDir, options.storename);
141
+ }
142
+ const previousManifest = options.existingManifest
143
+ ?? await readExtractStoreManifest(storeDir, options.storename);
144
+ const materials = mergeMaterials(previousManifest.materials, options.incomingMaterials);
145
+ const stagingDir = await mkdtemp(join(dirname(storeDir), `.${basename(storeDir)}.add-`));
146
+ let committed = false;
147
+ try {
148
+ await cp(storeDir, stagingDir, { recursive: true, force: true });
149
+ const model = await prepareExtractCache({
150
+ cacheDir: stagingDir,
151
+ model: previousManifest.model,
152
+ materials,
153
+ });
154
+ const nextManifest = {
155
+ ...previousManifest,
156
+ materials,
157
+ updatedAt: (options.now ?? (() => new Date().toISOString()))(),
158
+ };
159
+ await writeManifest(stagingDir, nextManifest);
160
+ await replaceStoreWithStaging(storeDir, stagingDir);
161
+ committed = true;
162
+ return {
163
+ manifest: nextManifest,
164
+ model,
165
+ addedMaterials: materials.length - previousManifest.materials.length,
166
+ };
167
+ }
168
+ finally {
169
+ if (!committed) {
170
+ await rm(stagingDir, { recursive: true, force: true });
171
+ }
172
+ }
173
+ }
174
+ //# sourceMappingURL=extract-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-store.js","sourceRoot":"","sources":["../src/extract-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE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HANGED
@@ -1,6 +1,9 @@
1
1
  export { createExtractSession } from './create-extract-session.js';
2
2
  export { createMlxExtractRuntime } from './create-mlx-extract-runtime.js';
3
+ export { createDriver, resolveMergedModels, resolveModelSpec, } from './model-resolution.js';
3
4
  export { resolveSessionModules } from './resolve-session-modules.js';
5
+ export { resolveDefaultContainerDir, } from './cli/constants.js';
6
+ export { resolveStoreDir, validateStorename } from './cli/store.js';
4
7
  export { compileExtractPrompt } from './compile-extract-prompt.js';
5
8
  export { buildExtractContext } from './extract-context.js';
6
9
  export { inputChunk, inputChunksFromJson, normalizeChunkInput, normalizeInputs, normalizeMaterial, normalizeMaterials, normalizeMessage, normalizeMessages, } from './extract-elements.js';
@@ -10,4 +13,5 @@ export type { ExtractContext } from './extract-context.js';
10
13
  export type { ChunkInput, ChunkInputValue, InputsInput, MaterialInput, MaterialsInput, MessageInput, MessagesInput, StandardMessageInput, ToolResultMessageInput, } from './extract-elements.js';
11
14
  export type { ExtractCorpus, ExtractRequest, ExtractResult, ExtractSession, ExtractSessionCloseOptions, ExtractSessionOptions, } from './types.js';
12
15
  export type { MlxExtractRuntime, MlxExtractRuntimeOptions, } from './create-mlx-extract-runtime.js';
16
+ export type { ExtractDriverOptions, ExtractDriverResult, } from './model-resolution.js';
13
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,EACf,iBAA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,GAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AACtF,OAAO,EACL,8BAA8B,EAC9B,yBAAyB,GAC1B,MAAM,2BAA2B,CAAC;AACnC,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,YAAY,EACV,UAAU,EACV,eAAe,EACf,WAAW,EACX,aAAa,EACb,cAAc,EACd,YAAY,EACZ,aAAa,EACb,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EACV,aAAa,EACb,cAAc,EACd,aAAa,EACb,cAAc,EACd,0BAA0B,EAC1B,qBAAqB,GACtB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,iBAAiB,EACjB,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AACzC,YAAY,EACV,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  export { createExtractSession } from './create-extract-session.js';
2
2
  export { createMlxExtractRuntime } from './create-mlx-extract-runtime.js';
3
+ export { createDriver, resolveMergedModels, resolveModelSpec, } from './model-resolution.js';
3
4
  export { resolveSessionModules } from './resolve-session-modules.js';
5
+ export { resolveDefaultContainerDir, } from './cli/constants.js';
6
+ export { resolveStoreDir, validateStorename } from './cli/store.js';
4
7
  export { compileExtractPrompt } from './compile-extract-prompt.js';
5
8
  export { buildExtractContext } from './extract-context.js';
6
9
  export { inputChunk, inputChunksFromJson, normalizeChunkInput, normalizeInputs, normalizeMaterial, normalizeMaterials, normalizeMessage, normalizeMessages, } from './extract-elements.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAA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C9B,yBAAyB,GAC1B,MAAM,2BAA2B,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { type AIDriver, type ModelSpec, type ModelsConfig, type PromptCacheController } from '@modular-prompt/driver';
2
+ /** createDriver に渡す extract 固有の runtime オプション */
3
+ export interface ExtractDriverOptions {
4
+ /** セッションと共有する KV cache controller */
5
+ cacheController?: PromptCacheController;
6
+ }
7
+ export interface ExtractDriverResult {
8
+ driver: AIDriver;
9
+ /** alias 解決後の生 model ID */
10
+ spec: ModelSpec;
11
+ }
12
+ /** bundled + user models.yaml を解決する(user の default/alias が優先)。 */
13
+ export declare function resolveMergedModels(): ModelsConfig;
14
+ /**
15
+ * extract で使用する ModelSpec を解決する。
16
+ *
17
+ * 優先順位は明示 model(alias または生 ID)→ models.default → models の先頭。
18
+ */
19
+ export declare function resolveModelSpec(model: string | undefined, models?: ModelsConfig): ModelSpec;
20
+ /**
21
+ * ModelSpec を AIService 経由で MLX driver に変換する。
22
+ * runtime が作成した cache controller は driver と共有する。
23
+ */
24
+ export declare function createDriver(model: string | undefined, options?: ExtractDriverOptions): Promise<ExtractDriverResult>;
25
+ //# sourceMappingURL=model-resolution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-resolution.d.ts","sourceRoot":"","sources":["../src/model-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,QAAQ,EAEb,KAAK,SAAS,EACd,KAAK,YAAY,EAEjB,KAAK,qBAAqB,EAC3B,MAAM,wBAAwB,CAAC;AAGhC,iDAAiD;AACjD,MAAM,WAAW,oBAAoB;IACnC,qCAAqC;IACrC,eAAe,CAAC,EAAE,qBAAqB,CAAC;CACzC;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,QAAQ,CAAC;IACjB,2BAA2B;IAC3B,IAAI,EAAE,SAAS,CAAC;CACjB;AAWD,kEAAkE;AAClE,wBAAgB,mBAAmB,IAAI,YAAY,CAElD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,MAAM,GAAE,YAAoC,GAC3C,SAAS,CAgBX;AAyBD;;;GAGG;AACH,wBAAsB,YAAY,CAChC,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAK9B"}
@@ -0,0 +1,57 @@
1
+ import { AIService, resolveDefaultModelFromConfig, resolveModelName, resolveModelReference, } from '@modular-prompt/driver';
2
+ import { BUNDLED_MODELS_CONFIG } from './default-models.js';
3
+ function inferProvider(_model) {
4
+ // extract runtime は MLX 専用。生の model ID は MLX model として扱う。
5
+ return 'mlx';
6
+ }
7
+ function createAIService() {
8
+ return AIService.fromMergedConfig(BUNDLED_MODELS_CONFIG, undefined, { mode: 'merge' });
9
+ }
10
+ /** bundled + user models.yaml を解決する(user の default/alias が優先)。 */
11
+ export function resolveMergedModels() {
12
+ return createAIService().modelsConfig;
13
+ }
14
+ /**
15
+ * extract で使用する ModelSpec を解決する。
16
+ *
17
+ * 優先順位は明示 model(alias または生 ID)→ models.default → models の先頭。
18
+ */
19
+ export function resolveModelSpec(model, models = resolveMergedModels()) {
20
+ const explicitModel = model?.trim();
21
+ if (explicitModel) {
22
+ return resolveModelReference({ ref: explicitModel }, models)
23
+ ?? resolveModelName(explicitModel, models, inferProvider);
24
+ }
25
+ const fallback = resolveDefaultModelFromConfig(models);
26
+ if (fallback) {
27
+ return fallback;
28
+ }
29
+ throw new Error('No extract model configured: specify -m <model-id-or-alias> '
30
+ + 'or define models.default in ~/.modular-prompt/models.yaml');
31
+ }
32
+ function withExtractDriverOptions(spec, options) {
33
+ if (spec.provider !== 'mlx') {
34
+ throw new Error(`Extract requires an MLX model, but '${spec.model}' uses provider '${spec.provider}'`);
35
+ }
36
+ const driverOptions = {
37
+ ...spec.driverOptions,
38
+ backend: 'lm',
39
+ ...(options.cacheController ? { cacheController: options.cacheController } : {}),
40
+ };
41
+ return {
42
+ ...spec,
43
+ backend: 'lm',
44
+ driverOptions,
45
+ };
46
+ }
47
+ /**
48
+ * ModelSpec を AIService 経由で MLX driver に変換する。
49
+ * runtime が作成した cache controller は driver と共有する。
50
+ */
51
+ export async function createDriver(model, options = {}) {
52
+ const ai = createAIService();
53
+ const spec = withExtractDriverOptions(resolveModelSpec(model, ai.modelsConfig), options);
54
+ const driver = await ai.createDriver(spec);
55
+ return { driver, spec };
56
+ }
57
+ //# sourceMappingURL=model-resolution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-resolution.js","sourceRoot":"","sources":["../src/model-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,6BAA6B,EAC7B,gBAAgB,EAChB,qBAAqB,GAOtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAc5D,SAAS,aAAa,CAAC,MAAc;IACnC,0DAA0D;IAC1D,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,eAAe;IACtB,OAAO,SAAS,CAAC,gBAAgB,CAAC,qBAAqB,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AACzF,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,mBAAmB;IACjC,OAAO,eAAe,EAAE,CAAC,YAAY,CAAC;AACxC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAyB,EACzB,SAAuB,mBAAmB,EAAE;IAE5C,MAAM,aAAa,GAAG,KAAK,EAAE,IAAI,EAAE,CAAC;IACpC,IAAI,aAAa,EAAE,CAAC;QAClB,OAAO,qBAAqB,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,MAAM,CAAC;eACvD,gBAAgB,CAAC,aAAa,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,QAAQ,GAAG,6BAA6B,CAAC,MAAM,CAAC,CAAC;IACvD,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,MAAM,IAAI,KAAK,CACb,8DAA8D;UAC5D,2DAA2D,CAC9D,CAAC;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,IAAe,EACf,OAA6B;IAE7B,IAAI,IAAI,CAAC,QAAQ,KAAK,KAAK,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CACb,uCAAuC,IAAI,CAAC,KAAK,oBAAoB,IAAI,CAAC,QAAQ,GAAG,CACtF,CAAC;IACJ,CAAC;IAED,MAAM,aAAa,GAA0B;QAC3C,GAAI,IAAI,CAAC,aAAmD;QAC5D,OAAO,EAAE,IAAI;QACb,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACjF,CAAC;IAEF,OAAO;QACL,GAAG,IAAI;QACP,OAAO,EAAE,IAAI;QACb,aAAa;KACd,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,KAAyB,EACzB,UAAgC,EAAE;IAElC,MAAM,EAAE,GAAG,eAAe,EAAE,CAAC;IAC7B,MAAM,IAAI,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;IACzF,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAC3C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC1B,CAAC"}
package/dist/types.d.ts CHANGED
@@ -30,6 +30,11 @@ export interface ExtractSessionOptions<TContext = ExtractContext> {
30
30
  corpus: ExtractCorpus;
31
31
  /** Output schema (Phase 3: structured output). Accepted at session creation. */
32
32
  schema?: object;
33
+ /**
34
+ * Cache preparation policy. Normal sessions are best-effort; store
35
+ * preparation uses `required` so an empty cache handle cannot be committed.
36
+ */
37
+ cachePreparation?: 'best-effort' | 'required';
33
38
  }
34
39
  export interface ExtractRequest {
35
40
  /** Extraction focus for this call (output / cue section). */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACzG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,aAAa,EACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D,oEAAoE;AACpE,MAAM,WAAW,aAAa;IAC5B,oCAAoC;IACpC,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B,iCAAiC;IACjC,QAAQ,CAAC,EAAE,aAAa,CAAC;CAC1B;AAED,MAAM,WAAW,qBAAqB,CAAC,QAAQ,GAAG,cAAc;IAC9D,MAAM,EAAE,QAAQ,CAAC;IACjB,kDAAkD;IAClD,eAAe,EAAE,qBAAqB,CAAC;IACvC,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,CAAC,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IACpC;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IACtC,kDAAkD;IAClD,MAAM,EAAE,aAAa,CAAC;IACtB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,6DAA6D;IAC7D,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC;IAC7B,6CAA6C;IAC7C,MAAM,CAAC,EAAE,WAAW,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AE,qBAAqB,CAAC;IACvC,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,CAAC,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IACpC;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IACtC,kDAAkD;IAClD,MAAM,EAAE,aAAa,CAAC;IACtB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,aAAa,GAAG,UAAU,CAAC;CAC/C;AAED,MAAM,WAAW,cAAc;IAC7B,6DAA6D;IAC7D,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC;IAC7B,6CAA6C;IAC7C,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,OAAO,CAAC,EAAE,YAAY,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IAC7B,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACzD,UAAU,IAAI,aAAa,CAAC,aAAa,CAAC,CAAC;IAC3C,KAAK,CAAC,OAAO,CAAC,EAAE,0BAA0B,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-prompt/extract",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Document extraction session API for modular prompt framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "dependencies": {
20
20
  "@modular-prompt/core": "0.3.0",
21
- "@modular-prompt/driver": "0.15.0"
21
+ "@modular-prompt/driver": "0.16.0"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@eslint/js": "9.39.2",