@hasna/knowledge 0.2.9 → 0.2.11

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/src/service.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { createArtifactStore } from './artifact-store';
2
+ import { consumeOpenFilesOutbox } from './outbox-consume';
3
+ import { getKnowledgeDbStats, migrateKnowledgeDb } from './knowledge-db';
4
+ import { ingestOpenFilesManifest } from './manifest-ingest';
5
+ import { ingestSourceRef } from './source-ingest';
6
+ import { resolveOpenFilesSource } from './source-resolver';
7
+ import { providerStatus, listModelRegistry, type ProviderStatusResult, type ModelRegistryEntry } from './providers';
8
+ import { resolveSafetyPolicy } from './safety';
9
+ import { initializeWikiLayout } from './wiki-layout';
10
+ import {
11
+ ensureKnowledgeWorkspace,
12
+ readKnowledgeConfig,
13
+ resolveScopedWorkspace,
14
+ type KnowledgeConfig,
15
+ type KnowledgeWorkspace,
16
+ } from './workspace';
17
+
18
+ export interface KnowledgeServiceOptions {
19
+ scope?: string;
20
+ cwd?: string;
21
+ }
22
+
23
+ export interface KnowledgePathsResult {
24
+ ok: true;
25
+ scope: string;
26
+ home: string;
27
+ config_path: string;
28
+ json_store_path: string;
29
+ knowledge_db_path: string;
30
+ artifacts_dir: string;
31
+ indexes_dir: string;
32
+ logs_dir: string;
33
+ runs_dir: string;
34
+ schemas_dir: string;
35
+ wiki_dir: string;
36
+ config: KnowledgeConfig;
37
+ message: string;
38
+ }
39
+
40
+ export class KnowledgeService {
41
+ private ensuredWorkspace?: KnowledgeWorkspace;
42
+ private cachedConfig?: KnowledgeConfig;
43
+
44
+ constructor(private readonly options: KnowledgeServiceOptions = {}) {}
45
+
46
+ get scope(): string {
47
+ return this.options.scope ?? 'global';
48
+ }
49
+
50
+ get workspace(): KnowledgeWorkspace {
51
+ return this.ensuredWorkspace ?? resolveScopedWorkspace(this.options.scope, this.options.cwd);
52
+ }
53
+
54
+ ensureWorkspace(): KnowledgeWorkspace {
55
+ if (!this.ensuredWorkspace) this.ensuredWorkspace = ensureKnowledgeWorkspace(this.workspace.home);
56
+ return this.ensuredWorkspace;
57
+ }
58
+
59
+ jsonStorePath(): string {
60
+ return this.ensureWorkspace().jsonStorePath;
61
+ }
62
+
63
+ config(): KnowledgeConfig {
64
+ if (!this.cachedConfig) {
65
+ const workspace = this.ensureWorkspace();
66
+ this.cachedConfig = readKnowledgeConfig(workspace.configPath);
67
+ }
68
+ return this.cachedConfig;
69
+ }
70
+
71
+ safetyPolicy() {
72
+ return resolveSafetyPolicy(this.config(), this.ensureWorkspace());
73
+ }
74
+
75
+ artifactStore() {
76
+ return createArtifactStore(this.config(), this.ensureWorkspace());
77
+ }
78
+
79
+ paths(): KnowledgePathsResult {
80
+ const workspace = this.ensureWorkspace();
81
+ return {
82
+ ok: true,
83
+ scope: this.scope,
84
+ home: workspace.home,
85
+ config_path: workspace.configPath,
86
+ json_store_path: workspace.jsonStorePath,
87
+ knowledge_db_path: workspace.knowledgeDbPath,
88
+ artifacts_dir: workspace.artifactsDir,
89
+ indexes_dir: workspace.indexesDir,
90
+ logs_dir: workspace.logsDir,
91
+ runs_dir: workspace.runsDir,
92
+ schemas_dir: workspace.schemasDir,
93
+ wiki_dir: workspace.wikiDir,
94
+ config: this.config(),
95
+ message: workspace.home,
96
+ };
97
+ }
98
+
99
+ initDb() {
100
+ return migrateKnowledgeDb(this.ensureWorkspace().knowledgeDbPath);
101
+ }
102
+
103
+ dbStats() {
104
+ const workspace = this.ensureWorkspace();
105
+ migrateKnowledgeDb(workspace.knowledgeDbPath);
106
+ return getKnowledgeDbStats(workspace.knowledgeDbPath);
107
+ }
108
+
109
+ async initWiki() {
110
+ return initializeWikiLayout(this.artifactStore());
111
+ }
112
+
113
+ async ingestManifest(input: string) {
114
+ const workspace = this.ensureWorkspace();
115
+ return ingestOpenFilesManifest({
116
+ dbPath: workspace.knowledgeDbPath,
117
+ input,
118
+ config: this.config(),
119
+ safetyPolicy: this.safetyPolicy(),
120
+ });
121
+ }
122
+
123
+ async ingestSource(sourceRef: string, purpose?: string) {
124
+ const workspace = this.ensureWorkspace();
125
+ return ingestSourceRef({
126
+ dbPath: workspace.knowledgeDbPath,
127
+ sourceRef,
128
+ purpose,
129
+ config: this.config(),
130
+ safetyPolicy: this.safetyPolicy(),
131
+ });
132
+ }
133
+
134
+ async resolveSource(sourceRef: string, options: { purpose?: string; limit?: number } = {}) {
135
+ const workspace = this.ensureWorkspace();
136
+ return resolveOpenFilesSource({
137
+ dbPath: workspace.knowledgeDbPath,
138
+ sourceRef,
139
+ purpose: options.purpose,
140
+ limit: options.limit,
141
+ safetyPolicy: this.safetyPolicy(),
142
+ });
143
+ }
144
+
145
+ async consumeOutbox(input: string) {
146
+ const workspace = this.ensureWorkspace();
147
+ return consumeOpenFilesOutbox({
148
+ dbPath: workspace.knowledgeDbPath,
149
+ input,
150
+ config: this.config(),
151
+ safetyPolicy: this.safetyPolicy(),
152
+ });
153
+ }
154
+
155
+ providerStatus(env: Record<string, string | undefined> = process.env): ProviderStatusResult {
156
+ return providerStatus(this.config(), env);
157
+ }
158
+
159
+ modelRegistry(): ModelRegistryEntry[] {
160
+ return listModelRegistry(this.config());
161
+ }
162
+ }
163
+
164
+ export function createKnowledgeService(options: KnowledgeServiceOptions = {}): KnowledgeService {
165
+ return new KnowledgeService(options);
166
+ }
package/src/workspace.ts CHANGED
@@ -39,6 +39,25 @@ export interface KnowledgeConfig {
39
39
  preferred_ref: 'open-files';
40
40
  allowed_schemes: string[];
41
41
  };
42
+ providers?: {
43
+ default_model?: string;
44
+ aliases?: Record<string, string>;
45
+ openai?: {
46
+ api_key_env?: string;
47
+ base_url?: string;
48
+ default_model?: string;
49
+ };
50
+ anthropic?: {
51
+ api_key_env?: string;
52
+ base_url?: string;
53
+ default_model?: string;
54
+ };
55
+ deepseek?: {
56
+ api_key_env?: string;
57
+ base_url?: string;
58
+ default_model?: string;
59
+ };
60
+ };
42
61
  safety?: {
43
62
  network?: {
44
63
  web_search_enabled?: boolean;
@@ -95,6 +114,28 @@ export function defaultKnowledgeConfig(): KnowledgeConfig {
95
114
  preferred_ref: 'open-files',
96
115
  allowed_schemes: ['open-files', 's3', 'file', 'https', 'http'],
97
116
  },
117
+ providers: {
118
+ default_model: 'openai:gpt-5.2',
119
+ aliases: {
120
+ fast: 'openai:gpt-5-mini',
121
+ reasoning: 'anthropic:claude-opus-4-6',
122
+ sonnet: 'anthropic:claude-sonnet-4-6',
123
+ deepseek: 'deepseek:deepseek-chat',
124
+ 'deepseek-reasoning': 'deepseek:deepseek-reasoner',
125
+ },
126
+ openai: {
127
+ api_key_env: 'OPENAI_API_KEY',
128
+ default_model: 'gpt-5.2',
129
+ },
130
+ anthropic: {
131
+ api_key_env: 'ANTHROPIC_API_KEY',
132
+ default_model: 'claude-sonnet-4-6',
133
+ },
134
+ deepseek: {
135
+ api_key_env: 'DEEPSEEK_API_KEY',
136
+ default_model: 'deepseek-chat',
137
+ },
138
+ },
98
139
  safety: {
99
140
  network: {
100
141
  web_search_enabled: false,