@shrkcrft/context 0.1.0-alpha.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SharkCraft contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @shrkcrft/context
2
+
3
+ SharkCraft AI context builder: token-budgeted relevance retrieval for tasks.
4
+
5
+ Part of [SharkCraft](https://github.com/shrkcrft/sharkcraft) — a deterministic, local-first toolkit that gives AI coding agents durable project context. See the main repo for documentation, examples, and the `shrk` CLI.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ bun add @shrkcrft/context
11
+ ```
12
+
13
+ ## License
14
+
15
+ MIT — see [LICENSE](./LICENSE).
@@ -0,0 +1,8 @@
1
+ import type { IKnowledgeEntry } from '@shrkcrft/knowledge';
2
+ export interface FormatEntryOptions {
3
+ includeExamples?: boolean;
4
+ maxContentChars?: number;
5
+ }
6
+ export declare function formatEntryForContext(entry: IKnowledgeEntry, options?: FormatEntryOptions): string;
7
+ export declare function formatSectionBody(entries: readonly IKnowledgeEntry[], options?: FormatEntryOptions): string;
8
+ //# sourceMappingURL=ai-context-formatter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-context-formatter.d.ts","sourceRoot":"","sources":["../src/ai-context-formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,MAAM,WAAW,kBAAkB;IACjC,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,eAAe,EACtB,OAAO,GAAE,kBAAuB,GAC/B,MAAM,CAoCR;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAE/G"}
@@ -0,0 +1,37 @@
1
+ export function formatEntryForContext(entry, options = {}) {
2
+ const lines = [];
3
+ const meta = [
4
+ `id:${entry.id}`,
5
+ `priority:${entry.priority}`,
6
+ entry.scope.length ? `scope:[${entry.scope.join(',')}]` : '',
7
+ entry.tags.length ? `tags:[${entry.tags.join(',')}]` : '',
8
+ ]
9
+ .filter(Boolean)
10
+ .join(' ');
11
+ lines.push(`### ${entry.title} (${meta})`);
12
+ if (entry.summary) {
13
+ lines.push(entry.summary.trim());
14
+ }
15
+ let content = entry.content.trim();
16
+ if (options.maxContentChars && content.length > options.maxContentChars) {
17
+ content = content.slice(0, options.maxContentChars) + '…';
18
+ }
19
+ lines.push(content);
20
+ if (options.includeExamples && entry.examples?.length) {
21
+ lines.push('Examples:');
22
+ for (const ex of entry.examples) {
23
+ if (ex.title)
24
+ lines.push(`- ${ex.title}`);
25
+ if (ex.code) {
26
+ const lang = ex.language ?? '';
27
+ lines.push('```' + lang);
28
+ lines.push(ex.code.trim());
29
+ lines.push('```');
30
+ }
31
+ }
32
+ }
33
+ return lines.join('\n');
34
+ }
35
+ export function formatSectionBody(entries, options = {}) {
36
+ return entries.map((e) => formatEntryForContext(e, options)).join('\n\n');
37
+ }
@@ -0,0 +1,7 @@
1
+ import type { IKnowledgeEntry } from '@shrkcrft/knowledge';
2
+ import { type IContextRequest } from './context-request.js';
3
+ import type { IContextResult } from './context-result.js';
4
+ import { formatEntryForContext } from './ai-context-formatter.js';
5
+ export declare function buildContext(allEntries: readonly IKnowledgeEntry[], request: IContextRequest): IContextResult;
6
+ export { formatEntryForContext };
7
+ //# sourceMappingURL=context-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-builder.d.ts","sourceRoot":"","sources":["../src/context-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,OAAO,EAA2B,KAAK,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG1D,OAAO,EAAE,qBAAqB,EAAqB,MAAM,2BAA2B,CAAC;AASrF,wBAAgB,YAAY,CAC1B,UAAU,EAAE,SAAS,eAAe,EAAE,EACtC,OAAO,EAAE,eAAe,GACvB,cAAc,CAiGhB;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
@@ -0,0 +1,97 @@
1
+ import { aggregateActionHints, formatAggregatedHints } from '@shrkcrft/knowledge';
2
+ import { DEFAULT_CONTEXT_REQUEST } from "./context-request.js";
3
+ import { selectRelevantEntries } from "./relevance-selector.js";
4
+ import { formatEntryForContext, formatSectionBody } from "./ai-context-formatter.js";
5
+ import { estimateTokens } from "./token-estimator.js";
6
+ export function buildContext(allEntries, request) {
7
+ const r = { ...DEFAULT_CONTEXT_REQUEST, ...request };
8
+ const maxTokens = r.maxTokens ?? DEFAULT_CONTEXT_REQUEST.maxTokens;
9
+ const buckets = selectRelevantEntries(allEntries, r);
10
+ const sectionPlans = [];
11
+ if (r.includeOverview && r.projectOverview) {
12
+ sectionPlans.push({
13
+ title: 'Project Overview',
14
+ priority: 100,
15
+ entries: [],
16
+ });
17
+ }
18
+ if (r.includeWarnings && buckets.warnings.length) {
19
+ sectionPlans.push({ title: 'Important Warnings', priority: 95, entries: buckets.warnings });
20
+ }
21
+ if (r.includeRules) {
22
+ sectionPlans.push({ title: 'Relevant Rules', priority: 90, entries: buckets.rules });
23
+ }
24
+ sectionPlans.push({ title: 'Architecture Constraints', priority: 80, entries: buckets.architecture });
25
+ if (r.includePaths) {
26
+ sectionPlans.push({ title: 'Relevant Path Conventions', priority: 70, entries: buckets.paths });
27
+ }
28
+ if (r.includeTemplates) {
29
+ sectionPlans.push({ title: 'Relevant Templates', priority: 65, entries: buckets.templates });
30
+ }
31
+ sectionPlans.push({ title: 'Technical Stack', priority: 50, entries: buckets.technical });
32
+ sectionPlans.push({ title: 'Testing Guidelines', priority: 45, entries: buckets.testing });
33
+ sectionPlans.push({ title: 'Security Guidelines', priority: 44, entries: buckets.security });
34
+ if (r.includeCommands) {
35
+ sectionPlans.push({ title: 'Commands', priority: 40, entries: buckets.commands });
36
+ }
37
+ sectionPlans.push({ title: 'Current Tasks', priority: 30, entries: buckets.tasks });
38
+ if (r.includeDocs) {
39
+ sectionPlans.push({ title: 'Reference Docs', priority: 10, entries: buckets.docs });
40
+ }
41
+ sectionPlans.sort((a, b) => b.priority - a.priority);
42
+ const sections = [];
43
+ const omitted = [];
44
+ let used = 0;
45
+ function tryAddSection(title, body, entryIds) {
46
+ const tokens = estimateTokens(body);
47
+ if (used + tokens > maxTokens && sections.length > 0) {
48
+ omitted.push(title);
49
+ return;
50
+ }
51
+ if (used + tokens > maxTokens) {
52
+ // Still emit, but mark truncated.
53
+ const ratio = (maxTokens - used) / tokens;
54
+ const truncatedBody = body.slice(0, Math.max(0, Math.floor(body.length * ratio))) + '\n…[truncated]';
55
+ const truncTokens = estimateTokens(truncatedBody);
56
+ sections.push({ title, body: truncatedBody, entryIds, tokens: truncTokens, truncated: true });
57
+ used += truncTokens;
58
+ return;
59
+ }
60
+ sections.push({ title, body, entryIds, tokens });
61
+ used += tokens;
62
+ }
63
+ for (const plan of sectionPlans) {
64
+ if (plan.title === 'Project Overview' && r.projectOverview) {
65
+ tryAddSection('Project Overview', r.projectOverview.trim(), []);
66
+ continue;
67
+ }
68
+ if (plan.entries.length === 0)
69
+ continue;
70
+ const body = formatSectionBody(plan.entries, {
71
+ includeExamples: r.includeExamples,
72
+ maxContentChars: 1500,
73
+ });
74
+ const ids = plan.entries.map((e) => e.id);
75
+ tryAddSection(plan.title, body, ids);
76
+ }
77
+ // Action hints: aggregate from every included entry and emit a single
78
+ // composite "Agent Actions" section. Skipped when no entry contributes.
79
+ const allIncludedEntries = sectionPlans.flatMap((p) => p.entries);
80
+ const aggregated = aggregateActionHints(allIncludedEntries);
81
+ const hintsBody = formatAggregatedHints(aggregated, { level: '###', compact: true });
82
+ if (hintsBody && hintsBody.length > 0) {
83
+ tryAddSection('Agent Actions', hintsBody, aggregated.contributingEntries);
84
+ }
85
+ const fullBody = sections
86
+ .map((s) => `## ${s.title}${s.truncated ? ' (truncated)' : ''}\n\n${s.body}`)
87
+ .join('\n\n');
88
+ return {
89
+ request: r,
90
+ sections,
91
+ totalTokens: used,
92
+ maxTokens,
93
+ omittedSections: omitted,
94
+ body: fullBody,
95
+ };
96
+ }
97
+ export { formatEntryForContext };
@@ -0,0 +1,20 @@
1
+ export interface IContextRequest {
2
+ task: string;
3
+ framework?: string;
4
+ area?: string;
5
+ tags?: readonly string[];
6
+ scope?: readonly string[];
7
+ appliesWhen?: readonly string[];
8
+ maxTokens?: number;
9
+ includeExamples?: boolean;
10
+ includeTemplates?: boolean;
11
+ includeRules?: boolean;
12
+ includePaths?: boolean;
13
+ includeDocs?: boolean;
14
+ includeOverview?: boolean;
15
+ includeWarnings?: boolean;
16
+ includeCommands?: boolean;
17
+ projectOverview?: string;
18
+ }
19
+ export declare const DEFAULT_CONTEXT_REQUEST: Required<Pick<IContextRequest, 'maxTokens' | 'includeExamples' | 'includeTemplates' | 'includeRules' | 'includePaths' | 'includeDocs' | 'includeOverview' | 'includeWarnings' | 'includeCommands'>>;
20
+ //# sourceMappingURL=context-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-request.d.ts","sourceRoot":"","sources":["../src/context-request.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzB,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAChC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAC5C,IAAI,CACF,eAAe,EACb,WAAW,GACX,iBAAiB,GACjB,kBAAkB,GAClB,cAAc,GACd,cAAc,GACd,aAAa,GACb,iBAAiB,GACjB,iBAAiB,GACjB,iBAAiB,CACpB,CAWF,CAAC"}
@@ -0,0 +1,11 @@
1
+ export const DEFAULT_CONTEXT_REQUEST = {
2
+ maxTokens: 4000,
3
+ includeExamples: true,
4
+ includeTemplates: true,
5
+ includeRules: true,
6
+ includePaths: true,
7
+ includeDocs: false,
8
+ includeOverview: true,
9
+ includeWarnings: true,
10
+ includeCommands: false,
11
+ };
@@ -0,0 +1,12 @@
1
+ import type { IContextSection } from './context-section.js';
2
+ import type { IContextRequest } from './context-request.js';
3
+ export interface IContextResult {
4
+ request: IContextRequest;
5
+ sections: readonly IContextSection[];
6
+ totalTokens: number;
7
+ maxTokens: number;
8
+ omittedSections: readonly string[];
9
+ /** Combined render of all sections (in order). */
10
+ body: string;
11
+ }
12
+ //# sourceMappingURL=context-result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-result.d.ts","sourceRoot":"","sources":["../src/context-result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,eAAe,CAAC;IACzB,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,kDAAkD;IAClD,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ export interface IContextSection {
2
+ title: string;
3
+ /** Entry IDs included (for traceability). */
4
+ entryIds: readonly string[];
5
+ /** Compact, AI-ready body. */
6
+ body: string;
7
+ /** Approximate token cost. */
8
+ tokens: number;
9
+ /** Marker if section was truncated due to budget. */
10
+ truncated?: boolean;
11
+ }
12
+ //# sourceMappingURL=context-section.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-section.d.ts","sourceRoot":"","sources":["../src/context-section.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,6CAA6C;IAC7C,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,qDAAqD;IACrD,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export * from './context-request.js';
2
+ export * from './context-result.js';
3
+ export * from './context-section.js';
4
+ export * from './context-builder.js';
5
+ export * from './token-estimator.js';
6
+ export * from './relevance-selector.js';
7
+ export * from './ai-context-formatter.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./context-request.js";
2
+ export * from "./context-result.js";
3
+ export * from "./context-section.js";
4
+ export * from "./context-builder.js";
5
+ export * from "./token-estimator.js";
6
+ export * from "./relevance-selector.js";
7
+ export * from "./ai-context-formatter.js";
@@ -0,0 +1,17 @@
1
+ import type { IKnowledgeEntry } from '@shrkcrft/knowledge';
2
+ import type { IContextRequest } from './context-request.js';
3
+ export interface SelectedEntries {
4
+ rules: IKnowledgeEntry[];
5
+ paths: IKnowledgeEntry[];
6
+ templates: IKnowledgeEntry[];
7
+ architecture: IKnowledgeEntry[];
8
+ technical: IKnowledgeEntry[];
9
+ warnings: IKnowledgeEntry[];
10
+ commands: IKnowledgeEntry[];
11
+ testing: IKnowledgeEntry[];
12
+ security: IKnowledgeEntry[];
13
+ docs: IKnowledgeEntry[];
14
+ tasks: IKnowledgeEntry[];
15
+ }
16
+ export declare function selectRelevantEntries(allEntries: readonly IKnowledgeEntry[], request: IContextRequest, limitPerSection?: number): SelectedEntries;
17
+ //# sourceMappingURL=relevance-selector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relevance-selector.d.ts","sourceRoot":"","sources":["../src/relevance-selector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,eAAe,EAAE,CAAC;IACzB,KAAK,EAAE,eAAe,EAAE,CAAC;IACzB,SAAS,EAAE,eAAe,EAAE,CAAC;IAC7B,YAAY,EAAE,eAAe,EAAE,CAAC;IAChC,SAAS,EAAE,eAAe,EAAE,CAAC;IAC7B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,OAAO,EAAE,eAAe,EAAE,CAAC;IAC3B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,IAAI,EAAE,eAAe,EAAE,CAAC;IACxB,KAAK,EAAE,eAAe,EAAE,CAAC;CAC1B;AAeD,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,SAAS,eAAe,EAAE,EACtC,OAAO,EAAE,eAAe,EACxB,eAAe,SAAI,GAClB,eAAe,CAuCjB"}
@@ -0,0 +1,54 @@
1
+ import { KnowledgeIndex } from '@shrkcrft/knowledge';
2
+ const TYPE_BUCKETS = {
3
+ rule: 'rules',
4
+ path: 'paths',
5
+ template: 'templates',
6
+ architecture: 'architecture',
7
+ technical: 'technical',
8
+ warning: 'warnings',
9
+ command: 'commands',
10
+ testing: 'testing',
11
+ security: 'security',
12
+ task: 'tasks',
13
+ };
14
+ export function selectRelevantEntries(allEntries, request, limitPerSection = 5) {
15
+ const index = new KnowledgeIndex(allEntries);
16
+ const tags = [...(request.tags ?? [])];
17
+ const scope = [...(request.scope ?? [])];
18
+ if (request.framework)
19
+ scope.push(request.framework);
20
+ if (request.area)
21
+ scope.push(request.area);
22
+ const searchAll = index.search({
23
+ query: request.task,
24
+ scope,
25
+ tags,
26
+ appliesWhen: request.appliesWhen,
27
+ });
28
+ const buckets = {
29
+ rules: [],
30
+ paths: [],
31
+ templates: [],
32
+ architecture: [],
33
+ technical: [],
34
+ warnings: [],
35
+ commands: [],
36
+ testing: [],
37
+ security: [],
38
+ docs: [],
39
+ tasks: [],
40
+ };
41
+ for (const r of searchAll) {
42
+ const typeKey = String(r.entry.type).toLowerCase();
43
+ const bucketKey = TYPE_BUCKETS[typeKey];
44
+ if (bucketKey) {
45
+ if (buckets[bucketKey].length < limitPerSection)
46
+ buckets[bucketKey].push(r.entry);
47
+ }
48
+ else if (request.includeDocs) {
49
+ if (buckets.docs.length < limitPerSection)
50
+ buckets.docs.push(r.entry);
51
+ }
52
+ }
53
+ return buckets;
54
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Approximate token count. Average English token ≈ 4 chars.
3
+ * Good enough for a v1 budget gate; not a substitute for a real tokenizer.
4
+ */
5
+ export declare function estimateTokens(text: string): number;
6
+ export declare function fitsBudget(currentTokens: number, addTokens: number, max: number): boolean;
7
+ //# sourceMappingURL=token-estimator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-estimator.d.ts","sourceRoot":"","sources":["../src/token-estimator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMnD;AAED,wBAAgB,UAAU,CAAC,aAAa,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAEzF"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Approximate token count. Average English token ≈ 4 chars.
3
+ * Good enough for a v1 budget gate; not a substitute for a real tokenizer.
4
+ */
5
+ export function estimateTokens(text) {
6
+ if (!text)
7
+ return 0;
8
+ // Use a slightly conservative ratio so we stay under budget.
9
+ const chars = text.length;
10
+ const words = text.trim().split(/\s+/).length;
11
+ return Math.max(Math.ceil(chars / 4), Math.ceil(words * 1.3));
12
+ }
13
+ export function fitsBudget(currentTokens, addTokens, max) {
14
+ return currentTokens + addTokens <= max;
15
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@shrkcrft/context",
3
+ "version": "0.1.0-alpha.2",
4
+ "description": "SharkCraft AI context builder: token-budgeted relevance retrieval for tasks.",
5
+ "license": "MIT",
6
+ "author": "SharkCraft contributors",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "bun": "./src/index.ts",
14
+ "import": "./dist/index.js",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/shrkcrft/sharkcraft.git",
26
+ "directory": "packages/context"
27
+ },
28
+ "homepage": "https://github.com/shrkcrft/sharkcraft",
29
+ "bugs": {
30
+ "url": "https://github.com/shrkcrft/sharkcraft/issues"
31
+ },
32
+ "keywords": [
33
+ "sharkcraft",
34
+ "context",
35
+ "ai",
36
+ "token-budget",
37
+ "mcp"
38
+ ],
39
+ "engines": {
40
+ "bun": ">=1.1.0",
41
+ "node": ">=18"
42
+ },
43
+ "scripts": {
44
+ "typecheck": "tsc --noEmit -p tsconfig.json"
45
+ },
46
+ "dependencies": {
47
+ "@shrkcrft/core": "^0.1.0-alpha.2",
48
+ "@shrkcrft/knowledge": "^0.1.0-alpha.2",
49
+ "@shrkcrft/rules": "^0.1.0-alpha.2",
50
+ "@shrkcrft/paths": "^0.1.0-alpha.2",
51
+ "@shrkcrft/templates": "^0.1.0-alpha.2"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }