@modular-prompt/extract 1.0.0 → 1.2.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 (81) hide show
  1. package/README.md +83 -24
  2. package/dist/cache-lifecycle.d.ts +5 -1
  3. package/dist/cache-lifecycle.d.ts.map +1 -1
  4. package/dist/cache-lifecycle.js +22 -0
  5. package/dist/cache-lifecycle.js.map +1 -1
  6. package/dist/cli/add-command.d.ts +2 -0
  7. package/dist/cli/add-command.d.ts.map +1 -1
  8. package/dist/cli/add-command.js +9 -3
  9. package/dist/cli/add-command.js.map +1 -1
  10. package/dist/cli/args.d.ts +3 -0
  11. package/dist/cli/args.d.ts.map +1 -1
  12. package/dist/cli/args.js +29 -0
  13. package/dist/cli/args.js.map +1 -1
  14. package/dist/cli/constants.d.ts +4 -0
  15. package/dist/cli/constants.d.ts.map +1 -1
  16. package/dist/cli/constants.js +19 -0
  17. package/dist/cli/constants.js.map +1 -1
  18. package/dist/cli/create-command.d.ts +2 -0
  19. package/dist/cli/create-command.d.ts.map +1 -1
  20. package/dist/cli/create-command.js +11 -4
  21. package/dist/cli/create-command.js.map +1 -1
  22. package/dist/cli/extract-command.d.ts +2 -0
  23. package/dist/cli/extract-command.d.ts.map +1 -1
  24. package/dist/cli/extract-command.js +22 -5
  25. package/dist/cli/extract-command.js.map +1 -1
  26. package/dist/cli/list-command.d.ts +1 -0
  27. package/dist/cli/list-command.d.ts.map +1 -1
  28. package/dist/cli/list-command.js +6 -7
  29. package/dist/cli/list-command.js.map +1 -1
  30. package/dist/cli/manifest.d.ts +11 -0
  31. package/dist/cli/manifest.d.ts.map +1 -1
  32. package/dist/cli/manifest.js +16 -0
  33. package/dist/cli/manifest.js.map +1 -1
  34. package/dist/cli/store.d.ts +21 -1
  35. package/dist/cli/store.d.ts.map +1 -1
  36. package/dist/cli/store.js +189 -4
  37. package/dist/cli/store.js.map +1 -1
  38. package/dist/cli.js +18 -10
  39. package/dist/cli.js.map +1 -1
  40. package/dist/create-extract-runtime.d.ts +22 -0
  41. package/dist/create-extract-runtime.d.ts.map +1 -0
  42. package/dist/create-extract-runtime.js +27 -0
  43. package/dist/create-extract-runtime.js.map +1 -0
  44. package/dist/create-extract-session.d.ts.map +1 -1
  45. package/dist/create-extract-session.js +8 -2
  46. package/dist/create-extract-session.js.map +1 -1
  47. package/dist/create-mlx-extract-runtime.d.ts +18 -7
  48. package/dist/create-mlx-extract-runtime.d.ts.map +1 -1
  49. package/dist/create-mlx-extract-runtime.js +11 -1
  50. package/dist/create-mlx-extract-runtime.js.map +1 -1
  51. package/dist/create-pytorch-extract-runtime.d.ts +13 -0
  52. package/dist/create-pytorch-extract-runtime.d.ts.map +1 -0
  53. package/dist/create-pytorch-extract-runtime.js +52 -0
  54. package/dist/create-pytorch-extract-runtime.js.map +1 -0
  55. package/dist/default-models.d.ts +3 -8
  56. package/dist/default-models.d.ts.map +1 -1
  57. package/dist/default-models.js +4 -16
  58. package/dist/default-models.js.map +1 -1
  59. package/dist/extract-runtime-types.d.ts +16 -0
  60. package/dist/extract-runtime-types.d.ts.map +1 -0
  61. package/dist/extract-runtime-types.js +2 -0
  62. package/dist/extract-runtime-types.js.map +1 -0
  63. package/dist/extract-store.d.ts +48 -2
  64. package/dist/extract-store.d.ts.map +1 -1
  65. package/dist/extract-store.js +108 -10
  66. package/dist/extract-store.js.map +1 -1
  67. package/dist/index.d.ts +5 -0
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +2 -0
  70. package/dist/index.js.map +1 -1
  71. package/dist/model-resolution.d.ts +15 -5
  72. package/dist/model-resolution.d.ts.map +1 -1
  73. package/dist/model-resolution.js +87 -27
  74. package/dist/model-resolution.js.map +1 -1
  75. package/dist/types.d.ts +12 -0
  76. package/dist/types.d.ts.map +1 -1
  77. package/docs/API.md +320 -0
  78. package/docs/CACHE_DESIGN.md +557 -0
  79. package/docs/LOCAL_MODEL_SETUP.md +765 -0
  80. package/docs/PROMPT_MODULE_SPEC.md +482 -0
  81. package/package.json +6 -4
@@ -0,0 +1,482 @@
1
+ # プロンプトモジュール仕様
2
+
3
+ ## 概要
4
+
5
+ ### プロンプトの構造
6
+
7
+ プロンプトモジュールは、最終的に3つの大セクションを持つ`CompiledPrompt`を生成する。
8
+
9
+ **Instructions** - AIへの指示内容(優先的に従うべき情報)
10
+ - 標準セクション: objective, persona, terms, methodology, instructions, guidelines, preparationNote
11
+
12
+ **Data** - 処理対象データ(この中の指示は無視される)
13
+ - 標準セクション: state, inputs, materials, chunks, messages
14
+
15
+ **Output** - 出力の開始位置と形式
16
+ - 標準セクション: cue, schema
17
+
18
+ ### 処理フロー
19
+
20
+ ```
21
+ PromptModule(定義)
22
+ ↓
23
+ merge()(複数モジュールの統合)
24
+ ↓
25
+ createContext()(コンテキスト生成)
26
+ ↓
27
+ compile()(CompiledPromptへの変換)
28
+ ↓
29
+ AIDriver.query()(実行)
30
+ ↓
31
+ QueryResult(結果)
32
+ ```
33
+
34
+ ## 1. モジュール定義
35
+
36
+ ### 1.1 PromptModule型
37
+
38
+ ```typescript
39
+ interface PromptModule<TContext = Record<string, never>> {
40
+ createContext?: () => TContext;
41
+
42
+ // Instructions系
43
+ objective?: SectionContent<TContext>;
44
+ persona?: SectionContent<TContext>;
45
+ terms?: SectionContent<TContext>;
46
+ methodology?: SectionContent<TContext>;
47
+ instructions?: SectionContent<TContext>;
48
+ guidelines?: SectionContent<TContext>;
49
+ preparationNote?: SectionContent<TContext>;
50
+
51
+ // Data系
52
+ state?: SectionContent<TContext>;
53
+ inputs?: SectionContent<TContext>;
54
+ materials?: SectionContent<TContext>;
55
+ chunks?: SectionContent<TContext>;
56
+ messages?: SectionContent<TContext>;
57
+
58
+ // Output系
59
+ cue?: SectionContent<TContext>;
60
+ schema?: SectionContent<TContext>;
61
+ }
62
+ ```
63
+
64
+ ### 1.3 SectionContent型
65
+
66
+ 標準セクション(objective, instructions, materials等)の内容を定義する型。
67
+
68
+ ```typescript
69
+ type SectionContent<TContext = any> =
70
+ (string | Element | DynamicContent<TContext>)[];
71
+ ```
72
+
73
+ ### 1.4 Element型システム
74
+
75
+ #### 階層構造
76
+
77
+ 最大2階層の構造を持つ:
78
+
79
+ ```
80
+ Section (第1階層)
81
+ └─ SubSection (第2階層)
82
+ └─ string (最下層)
83
+ ```
84
+
85
+ #### Element種別
86
+
87
+ ```typescript
88
+ type Element =
89
+ | TextElement // プレーンテキスト
90
+ | MessageElement // role付きメッセージ(system/assistant/user)
91
+ | MaterialElement // 資料(id, title, content)
92
+ | ChunkElement // データチャンク(partOf, index, total)
93
+ | JSONElement // JSONスキーマ(構造化出力用)
94
+ | SectionElement // セクション(第1階層)
95
+ | SubSectionElement; // サブセクション(第2階層)
96
+ ```
97
+
98
+ **SectionElement:**
99
+ ```typescript
100
+ interface SectionElement {
101
+ type: 'section';
102
+ title: string;
103
+ items: (string | SubSectionElement)[];
104
+ }
105
+ ```
106
+
107
+ **SubSectionElement:**
108
+ ```typescript
109
+ interface SubSectionElement {
110
+ type: 'subsection';
111
+ title: string;
112
+ items: (string | SimpleDynamicContent<TContext>)[];
113
+ }
114
+ ```
115
+
116
+ ### 1.4 DynamicContent
117
+
118
+ #### DynamicContent<TContext>
119
+
120
+ 実行時にコンテキストベースでコンテンツを生成する関数。
121
+
122
+ ```typescript
123
+ type DynamicContent<TContext> = (context: TContext) =>
124
+ | string
125
+ | string[]
126
+ | DynamicElement
127
+ | DynamicElement[]
128
+ | null
129
+ | undefined;
130
+ ```
131
+
132
+ **DynamicElement:** Section/SubSectionを除く全Element型
133
+
134
+ ```typescript
135
+ type DynamicElement =
136
+ | TextElement
137
+ | MessageElement
138
+ | MaterialElement
139
+ | ChunkElement
140
+ | JSONElement;
141
+ ```
142
+
143
+ **制約:**
144
+ - Section/SubSectionは生成不可(静的構造のみ)
145
+ - null/undefinedは空配列として扱われる
146
+
147
+ #### SimpleDynamicContent<TContext>
148
+
149
+ SubSection内のitemsで使用する簡易版。
150
+
151
+ ```typescript
152
+ type SimpleDynamicContent<TContext> = (context: TContext) =>
153
+ | string
154
+ | string[]
155
+ | null
156
+ | undefined;
157
+ ```
158
+
159
+ **制約:**
160
+ - 文字列または文字列配列のみ生成可能
161
+ - Elementは生成不可
162
+
163
+ ## 2. マージ処理
164
+
165
+ ### 2.1 基本動作
166
+
167
+ ```typescript
168
+ // 2つのモジュール
169
+ merge<T1, T2>(
170
+ module1: PromptModule<T1>,
171
+ module2: PromptModule<T2>
172
+ ): PromptModule<T1 & T2>
173
+
174
+ // 3つ以上(6つまでオーバーロード対応)
175
+ merge<T1, T2, T3>(
176
+ ...modules: [PromptModule<T1>, PromptModule<T2>, PromptModule<T3>]
177
+ ): PromptModule<T1 & T2 & T3>
178
+ ```
179
+
180
+ 複数のモジュールを1つに統合する。各モジュールのコンテキスト型は交差型(&)として結合される。
181
+
182
+ ### 2.2 マージルール
183
+
184
+ #### 標準セクションの結合
185
+
186
+ 同名セクションの内容を配列として結合:
187
+
188
+ ```typescript
189
+ // 入力
190
+ module1.instructions = ['指示1', '指示2'];
191
+ module2.instructions = ['指示3'];
192
+
193
+ // 結果
194
+ merged.instructions = ['指示1', '指示2', '指示3'];
195
+ ```
196
+
197
+ #### SubSectionのitemsマージ
198
+
199
+ 同名SubSectionのitemsを結合:
200
+
201
+ ```typescript
202
+ // 入力
203
+ module1.instructions = [
204
+ { type: 'subsection', title: 'ルール', items: ['ルール1'] }
205
+ ];
206
+ module2.instructions = [
207
+ { type: 'subsection', title: 'ルール', items: ['ルール2'] }
208
+ ];
209
+
210
+ // 結果
211
+ merged.instructions = [
212
+ { type: 'subsection', title: 'ルール', items: ['ルール1', 'ルール2'] }
213
+ ];
214
+ ```
215
+
216
+ #### createContextの処理
217
+
218
+ 全てのcreateContextを実行し、結果をオブジェクトマージ(後の値で上書き):
219
+
220
+ ```typescript
221
+ // 入力
222
+ module1.createContext = () => ({ a: 1, b: 2 });
223
+ module2.createContext = () => ({ b: 3, c: 4 });
224
+
225
+ // 結果
226
+ merged.createContext = () => ({ a: 1, b: 3, c: 4 });
227
+ ```
228
+
229
+ ### 2.3 順序制御
230
+
231
+ #### セクション内要素の順序
232
+
233
+ 1. 通常要素(文字列、DynamicContent、他のElement)
234
+ 2. SubSectionElement
235
+
236
+ ```typescript
237
+ // 入力
238
+ instructions: [
239
+ { type: 'subsection', title: 'ルール', items: ['...'] },
240
+ '基本指示',
241
+ { type: 'subsection', title: '注意', items: ['...'] }
242
+ ];
243
+
244
+ // 結果(コンパイル後)
245
+ instructions: [
246
+ '基本指示',
247
+ { type: 'subsection', title: 'ルール', items: ['...'] },
248
+ { type: 'subsection', title: '注意', items: ['...'] }
249
+ ];
250
+ ```
251
+
252
+ #### 重複の許容
253
+
254
+ 意図的な重複(セパレータ、強調マーカー等)を許容。
255
+
256
+ ## 3. コンテキスト生成
257
+
258
+ ### 3.1 基本仕様
259
+
260
+ ```typescript
261
+ createContext<TContext>(module: PromptModule<TContext>): TContext
262
+ ```
263
+
264
+ マージ済みモジュールから型安全なコンテキストを生成。
265
+
266
+ **動作:**
267
+ - `module.createContext()`を実行して初期値を取得
268
+ - 戻り値の型がTContext型として推論される
269
+
270
+ ### 3.2 型推論
271
+
272
+ TypeScriptの型推論により、コンテキストのフィールドが型安全に:
273
+
274
+ ```typescript
275
+ const module: PromptModule<{ items: string[] }> = {
276
+ createContext: () => ({ items: [] }),
277
+ // ...
278
+ };
279
+
280
+ const context = createContext(module);
281
+ // contextの型: { items: string[] }
282
+ context.items = ['a', 'b']; // OK
283
+ context.invalid = 1; // エラー
284
+ ```
285
+
286
+ ## 4. コンパイル処理
287
+
288
+ ### 4.1 基本動作
289
+
290
+ ```typescript
291
+ compile<TContext>(
292
+ module: PromptModule<TContext>,
293
+ context?: TContext
294
+ ): CompiledPrompt
295
+ ```
296
+
297
+ **役割:**
298
+
299
+ 動的コンテンツ(DynamicContent)をコンテキストで解決し、静的な構造(CompiledPrompt)に変換する。
300
+
301
+ 1. DynamicContentを評価して具体的な値に変換
302
+ 2. 標準セクションをSectionElementに変換
303
+ 3. instructions/data/outputの3つの大セクションに分類
304
+
305
+ **context未指定時の動作:**
306
+ - 自動的に`module.createContext()`を実行
307
+ - 生成されたcontextを使用してコンパイル
308
+
309
+ ### 4.2 DynamicContent評価
310
+
311
+ #### 評価タイミング
312
+
313
+ コンパイル時にすべてのDynamicContentを即座に評価。
314
+
315
+ #### 変換ルール
316
+
317
+ ```typescript
318
+ // 文字列を返す場合
319
+ (ctx) => 'text' → 'text'
320
+
321
+ // 文字列配列を返す場合(展開される)
322
+ (ctx) => ['a', 'b'] → 'a', 'b'
323
+
324
+ // Elementを返す場合
325
+ (ctx) => ({ type: 'text', content: 'x' }) → TextElement
326
+
327
+ // Element配列を返す場合(展開される)
328
+ (ctx) => [elem1, elem2] → elem1, elem2
329
+
330
+ // null/undefinedを返す場合
331
+ (ctx) => null → (空、何も追加されない)
332
+ (ctx) => undefined → (空、何も追加されない)
333
+ ```
334
+
335
+ #### SimpleDynamicContent変換ルール
336
+
337
+ SubSection内のitemsで使用される場合:
338
+
339
+ ```typescript
340
+ // 文字列を返す場合
341
+ (ctx) => 'text' → 'text'
342
+
343
+ // 文字列配列を返す場合(展開される)
344
+ (ctx) => ['a', 'b'] → 'a', 'b'
345
+
346
+ // null/undefinedを返す場合
347
+ (ctx) => null → (空、何も追加されない)
348
+ ```
349
+
350
+ ### 4.3 CompiledPrompt構造
351
+
352
+ compile()の戻り値:
353
+
354
+ ```typescript
355
+ interface CompiledPrompt {
356
+ instructions: Element[]; // Instructions系セクション
357
+ data: Element[]; // Data系セクション
358
+ output: Element[]; // Output系セクション
359
+ metadata?: {
360
+ outputSchema?: object; // 構造化出力スキーマ
361
+ };
362
+ }
363
+ ```
364
+
365
+ このCompiledPromptをAIDriverに渡して実行する。
366
+
367
+ ### 4.4 構造化出力
368
+
369
+ schemaセクションにJSONElementが含まれる場合、自動的に`metadata.outputSchema`に設定される:
370
+
371
+ ```typescript
372
+ // 入力
373
+ const module = {
374
+ schema: [
375
+ {
376
+ type: 'json',
377
+ content: {
378
+ type: 'object',
379
+ properties: {
380
+ answer: { type: 'string' }
381
+ }
382
+ }
383
+ }
384
+ ]
385
+ };
386
+
387
+ // コンパイル後
388
+ const compiled = compile(module);
389
+ // compiled.metadata.outputSchema = { type: 'object', properties: {...} }
390
+ ```
391
+
392
+ **動作:**
393
+ - 複数のJSONElementがある場合、最後のものが使用される
394
+ - ドライバーはこのスキーマに基づいて構造化出力を生成
395
+
396
+ ## 5. 実行
397
+
398
+ CompiledPromptをAIDriverに渡して実行する。
399
+
400
+ **driver.query()** - 通常のクエリ実行。結果を一度に返す。
401
+
402
+ **driver.streamQuery()** - ストリーミングクエリ実行。結果を逐次的に返す。
403
+
404
+ ### 5.1 QueryResult(driver.query()の戻り値)
405
+
406
+ ```typescript
407
+ interface QueryResult {
408
+ content: string; // 生成されたテキスト
409
+ structuredOutput?: unknown; // 構造化出力(outputSchema指定時)
410
+ finishReason?: 'stop' | 'length' | 'error';
411
+ usage?: {
412
+ promptTokens: number;
413
+ completionTokens: number;
414
+ totalTokens: number;
415
+ };
416
+ }
417
+ ```
418
+
419
+ **structuredOutputの値:**
420
+ - `undefined`: スキーマ未指定、または有効なJSONが生成されなかった
421
+ - `object/array`: 抽出されたJSON(スキーマに準拠)
422
+
423
+ ### 5.2 StreamResult(driver.streamQuery()の戻り値)
424
+
425
+ ```typescript
426
+ interface StreamResult {
427
+ stream: AsyncIterable<string>; // ストリーミングチャンク
428
+ result: Promise<QueryResult>; // 最終結果
429
+ }
430
+ ```
431
+
432
+ **使用方法:**
433
+ ```typescript
434
+ const { stream, result } = await driver.streamQuery(compiled);
435
+
436
+ // ストリームを処理
437
+ for await (const chunk of stream) {
438
+ process.stdout.write(chunk);
439
+ }
440
+
441
+ // 最終結果を取得
442
+ const finalResult = await result;
443
+ ```
444
+
445
+ ## 制約
446
+
447
+ ### 階層構造の制約
448
+
449
+ - 最大2階層: Section → SubSection → string
450
+ - SubSectionの入れ子は不可
451
+
452
+ ### DynamicContentの制約
453
+
454
+ - DynamicContent: Section/SubSectionを生成不可
455
+ - SimpleDynamicContent: Elementを生成不可(文字列のみ)
456
+
457
+ ### コンパイル時の制約
458
+
459
+ - DynamicContentはコンパイル時に即座に評価される
460
+ - 評価後は静的な構造となり、再評価されない
461
+ - context未指定時は自動的にcreateContext()が実行される
462
+
463
+ ### マージ時の制約
464
+
465
+ - createContextは全て実行される(選択的実行は不可)
466
+ - 同名SubSectionは必ずマージされる(個別保持は不可)
467
+
468
+ ## ベストプラクティス
469
+
470
+ ### 箇条書きスタイルの使用
471
+
472
+ **原則**:
473
+ - 一行の指示項目には `- ` を先頭に付けて箇条書きにする
474
+
475
+ **理由**:
476
+ - 長い説明文(段落)と短い指示項目(箇条書き)を意味的に分けることができる
477
+ - AIが指示を明確に識別しやすくなる
478
+ - プロンプトの可読性が向上する
479
+
480
+ **注意**:
481
+ - 長い説明文や段落的な内容には箇条書き記号を付けない
482
+ - 番号付きリスト(`1. 2. 3.`)が必要な場合はそちらを使用
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-prompt/extract",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Document extraction session API for modular prompt framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -14,11 +14,12 @@
14
14
  "files": [
15
15
  "bin",
16
16
  "dist",
17
- "README.md"
17
+ "README.md",
18
+ "docs"
18
19
  ],
19
20
  "dependencies": {
20
21
  "@modular-prompt/core": "0.3.0",
21
- "@modular-prompt/driver": "0.16.0"
22
+ "@modular-prompt/driver": "0.17.1"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@eslint/js": "9.39.2",
@@ -62,6 +63,7 @@
62
63
  "test:run": "vitest run",
63
64
  "clean": "rm -rf dist tsconfig.tsbuildinfo",
64
65
  "lint": "eslint src",
65
- "typecheck": "tsc --noEmit"
66
+ "typecheck": "tsc --noEmit",
67
+ "copy-docs": "node ../../scripts/copy-package-docs.mjs extract"
66
68
  }
67
69
  }