@gixcopilot/context 0.1.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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +131 -0
  3. package/dist/context-compressor.d.ts +28 -0
  4. package/dist/context-compressor.d.ts.map +1 -0
  5. package/dist/context-compressor.js +35 -0
  6. package/dist/context-compressor.js.map +1 -0
  7. package/dist/context-engine.d.ts +36 -0
  8. package/dist/context-engine.d.ts.map +1 -0
  9. package/dist/context-engine.js +191 -0
  10. package/dist/context-engine.js.map +1 -0
  11. package/dist/context-format.d.ts +10 -0
  12. package/dist/context-format.d.ts.map +1 -0
  13. package/dist/context-format.js +16 -0
  14. package/dist/context-format.js.map +1 -0
  15. package/dist/context-item.d.ts +44 -0
  16. package/dist/context-item.d.ts.map +1 -0
  17. package/dist/context-item.js +2 -0
  18. package/dist/context-item.js.map +1 -0
  19. package/dist/context-priority.d.ts +13 -0
  20. package/dist/context-priority.d.ts.map +1 -0
  21. package/dist/context-priority.js +16 -0
  22. package/dist/context-priority.js.map +1 -0
  23. package/dist/context-registry.d.ts +47 -0
  24. package/dist/context-registry.d.ts.map +1 -0
  25. package/dist/context-registry.js +130 -0
  26. package/dist/context-registry.js.map +1 -0
  27. package/dist/context-scope.d.ts +19 -0
  28. package/dist/context-scope.d.ts.map +1 -0
  29. package/dist/context-scope.js +13 -0
  30. package/dist/context-scope.js.map +1 -0
  31. package/dist/context-sensitivity.d.ts +11 -0
  32. package/dist/context-sensitivity.d.ts.map +1 -0
  33. package/dist/context-sensitivity.js +11 -0
  34. package/dist/context-sensitivity.js.map +1 -0
  35. package/dist/context-serializer.d.ts +33 -0
  36. package/dist/context-serializer.d.ts.map +1 -0
  37. package/dist/context-serializer.js +112 -0
  38. package/dist/context-serializer.js.map +1 -0
  39. package/dist/index.d.ts +23 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +11 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/resolved-context.d.ts +64 -0
  44. package/dist/resolved-context.d.ts.map +1 -0
  45. package/dist/resolved-context.js +2 -0
  46. package/dist/resolved-context.js.map +1 -0
  47. package/dist/state-patch.d.ts +35 -0
  48. package/dist/state-patch.d.ts.map +1 -0
  49. package/dist/state-patch.js +2 -0
  50. package/dist/state-patch.js.map +1 -0
  51. package/dist/state-store.d.ts +80 -0
  52. package/dist/state-store.d.ts.map +1 -0
  53. package/dist/state-store.js +129 -0
  54. package/dist/state-store.js.map +1 -0
  55. package/dist/token-estimator.d.ts +17 -0
  56. package/dist/token-estimator.d.ts.map +1 -0
  57. package/dist/token-estimator.js +16 -0
  58. package/dist/token-estimator.js.map +1 -0
  59. package/package.json +51 -0
@@ -0,0 +1,80 @@
1
+ import type { StatePatch, StatePatchResult } from './state-patch.js';
2
+ /**
3
+ * State's own scope vocabulary (Section 41) - deliberately smaller than `ContextScope`
4
+ * (Section 8): state is either shared for the app's lifetime (`global`), shared for the
5
+ * current workflow (`session`), or owned by one mounted component (`component`). State does
6
+ * not have a `page`/`user`/`temporary` distinction the way context does - see
7
+ * `Phase_4_Decisions.md` ("State vs Context separation").
8
+ */
9
+ export type StateScope = 'global' | 'session' | 'component';
10
+ export interface StateValidationResult {
11
+ readonly valid: boolean;
12
+ readonly error?: string;
13
+ }
14
+ export type StateValidator<T> = (value: T) => boolean | StateValidationResult;
15
+ export interface CopilotStateDefinition<T> {
16
+ readonly id: string;
17
+ readonly name: string;
18
+ readonly initialValue: T;
19
+ readonly scope?: StateScope;
20
+ readonly validate?: StateValidator<T>;
21
+ /**
22
+ * Capability metadata (Section 38-39, added in Phase 6) - `true` allows this slot to
23
+ * accept a validated `applyPatch()` call (typically from an AI-proposed patch, via a
24
+ * reserved tool - see `@gixcopilot/generative-ui`). Defaults to `false`: a slot is
25
+ * read-only from the model's perspective unless explicitly opted in, mirroring
26
+ * `@gixcopilot/context`'s own "nothing is exposed unless explicit" convention from Phase 4.
27
+ * This is capability metadata, not enforcement of *who* may call `applyPatch()` - Phase 7
28
+ * will add real authorization.
29
+ */
30
+ readonly modelWritable?: boolean;
31
+ }
32
+ /**
33
+ * A framework-independent, typed, subscribable state slot store (Section 42). Distinct from
34
+ * `ContextRegistry`: this holds *mutable application/Copilot data*; nothing here is
35
+ * automatically model-facing (Section 44) - bridging a specific slot to context is an
36
+ * explicit, separate act performed by the caller (see `Phase_4_Decisions.md`, "State
37
+ * exposure policy").
38
+ */
39
+ export interface CopilotStateStore {
40
+ /**
41
+ * Ensures `definition.id` exists, seeding it with `initialValue` the first time only
42
+ * (later calls for the same id do not reset an already-set value - Section 18's
43
+ * "identity by explicit id" rule applies here too). Returns the slot's current value.
44
+ * Always refreshes the stored `validate`/`scope` to the latest definition passed in.
45
+ */
46
+ register<T>(definition: CopilotStateDefinition<T>): T;
47
+ has(id: string): boolean;
48
+ get<T>(id: string): T | undefined;
49
+ /** Validates (if a validator was registered) then replaces the value; throws on failure. */
50
+ set<T>(id: string, value: T): void;
51
+ /** Functional update (Section 48) - reads the previous value, validates, then replaces. */
52
+ update<T>(id: string, updater: (previous: T) => T): void;
53
+ /** Notified on every `set`/`update`/successfully-applied patch for this id. Removing the id
54
+ * does not auto-unsubscribe. */
55
+ subscribe<T>(id: string, listener: (value: T) => void): () => void;
56
+ remove(id: string): void;
57
+ list(): readonly string[];
58
+ /**
59
+ * Monotonically increases on every successful `set`/`update`/`applyPatch` for this id
60
+ * (Section 45) - not just patches, so a stale AI-proposed `baseRevision` is detected even
61
+ * when the *UI* (not another patch) moved the value in the meantime (Section 46).
62
+ * `undefined` for an unregistered id.
63
+ */
64
+ getRevision(id: string): number | undefined;
65
+ isModelWritable(id: string): boolean;
66
+ /**
67
+ * The validated state-patch pipeline (Section 43): unknown id / not writable / stale
68
+ * `baseRevision` / schema-invalid result each return a distinct, non-throwing
69
+ * `StatePatchResult` - state is left completely unchanged unless `status: 'applied'`
70
+ * (Section 43's "invalid patch must not mutate state", satisfied structurally: nothing is
71
+ * written until every check has already passed).
72
+ */
73
+ applyPatch(id: string, patch: StatePatch, baseRevision: number): StatePatchResult;
74
+ }
75
+ /**
76
+ * Creates an isolated store - no mandatory global singleton (Section 42); a host creates
77
+ * one per independent Copilot instance, mirroring `createContextRegistry()`.
78
+ */
79
+ export declare function createCopilotStateStore(): CopilotStateStore;
80
+ //# sourceMappingURL=state-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-store.d.ts","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAErE;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;AAE5D,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAAG,qBAAqB,CAAC;AAE9E,MAAM,WAAW,sBAAsB,CAAC,CAAC;IACvC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;IACtC;;;;;;;;OAQG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,QAAQ,CAAC,CAAC,EAAE,UAAU,EAAE,sBAAsB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACtD,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;IAClC,4FAA4F;IAC5F,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IACnC,2FAA2F;IAC3F,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IACzD;oCACgC;IAChC,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACnE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,IAAI,SAAS,MAAM,EAAE,CAAC;IAC1B;;;;;OAKG;IACH,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC5C,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;IACrC;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,GAAG,gBAAgB,CAAC;CACnF;AAgCD;;;GAGG;AACH,wBAAgB,uBAAuB,IAAI,iBAAiB,CAoG3D"}
@@ -0,0 +1,129 @@
1
+ import { CopilotError } from '@gixcopilot/protocol';
2
+ function validateForPatch(validate, value) {
3
+ if (!validate)
4
+ return { valid: true };
5
+ const result = validate(value);
6
+ if (typeof result === 'boolean')
7
+ return { valid: result };
8
+ return result;
9
+ }
10
+ function assertValid(validate, value) {
11
+ if (!validate)
12
+ return;
13
+ const result = validate(value);
14
+ const valid = typeof result === 'boolean' ? result : result.valid;
15
+ if (!valid) {
16
+ const message = typeof result === 'object' && result.error ? result.error : 'Rejected state update.';
17
+ throw CopilotError.validation(message);
18
+ }
19
+ }
20
+ /**
21
+ * Creates an isolated store - no mandatory global singleton (Section 42); a host creates
22
+ * one per independent Copilot instance, mirroring `createContextRegistry()`.
23
+ */
24
+ export function createCopilotStateStore() {
25
+ const slots = new Map();
26
+ function requireSlot(id) {
27
+ const slot = slots.get(id);
28
+ if (!slot) {
29
+ throw CopilotError.validation('No state registered with that id.', { id });
30
+ }
31
+ return slot;
32
+ }
33
+ function notify(slot) {
34
+ for (const listener of slot.listeners)
35
+ listener(slot.value);
36
+ }
37
+ return {
38
+ register(definition) {
39
+ const existing = slots.get(definition.id);
40
+ if (existing) {
41
+ existing.scope = definition.scope ?? existing.scope;
42
+ existing.validate = definition.validate;
43
+ existing.modelWritable = definition.modelWritable ?? existing.modelWritable;
44
+ return existing.value;
45
+ }
46
+ const slot = {
47
+ value: definition.initialValue,
48
+ scope: definition.scope ?? 'component',
49
+ validate: definition.validate,
50
+ modelWritable: definition.modelWritable ?? false,
51
+ revision: 0,
52
+ listeners: new Set(),
53
+ };
54
+ slots.set(definition.id, slot);
55
+ return definition.initialValue;
56
+ },
57
+ has: (id) => slots.has(id),
58
+ get: (id) => slots.get(id)?.value,
59
+ set(id, value) {
60
+ const slot = requireSlot(id);
61
+ assertValid(slot.validate, value);
62
+ slot.value = value;
63
+ slot.revision += 1;
64
+ notify(slot);
65
+ },
66
+ update(id, updater) {
67
+ const slot = requireSlot(id);
68
+ const next = updater(slot.value);
69
+ assertValid(slot.validate, next);
70
+ slot.value = next;
71
+ slot.revision += 1;
72
+ notify(slot);
73
+ },
74
+ subscribe(id, listener) {
75
+ const slot = requireSlot(id);
76
+ const wrapped = listener;
77
+ slot.listeners.add(wrapped);
78
+ return () => {
79
+ slot.listeners.delete(wrapped);
80
+ };
81
+ },
82
+ remove(id) {
83
+ slots.delete(id);
84
+ },
85
+ list: () => Array.from(slots.keys()),
86
+ getRevision: (id) => slots.get(id)?.revision,
87
+ isModelWritable: (id) => slots.get(id)?.modelWritable ?? false,
88
+ applyPatch(id, patch, baseRevision) {
89
+ const slot = slots.get(id);
90
+ if (!slot)
91
+ return { status: 'rejected', reason: 'unknown-state' };
92
+ if (!slot.modelWritable)
93
+ return { status: 'rejected', reason: 'not-writable' };
94
+ if (slot.revision !== baseRevision) {
95
+ return { status: 'conflict', currentRevision: slot.revision };
96
+ }
97
+ let nextValue;
98
+ if (patch.op === 'set') {
99
+ nextValue = patch.value;
100
+ }
101
+ else {
102
+ const current = slot.value;
103
+ if (!isPlainObject(current) || !isPlainObject(patch.value)) {
104
+ return {
105
+ status: 'rejected',
106
+ reason: 'invalid-patch',
107
+ detail: 'A "merge" patch requires both the current and patched value to be plain objects.',
108
+ };
109
+ }
110
+ nextValue = { ...current, ...patch.value };
111
+ }
112
+ const validation = validateForPatch(slot.validate, nextValue);
113
+ if (!validation.valid) {
114
+ return { status: 'rejected', reason: 'invalid-value', detail: validation.error };
115
+ }
116
+ slot.value = nextValue;
117
+ slot.revision += 1;
118
+ notify(slot);
119
+ return { status: 'applied', revision: slot.revision, value: nextValue };
120
+ },
121
+ };
122
+ }
123
+ function isPlainObject(value) {
124
+ if (typeof value !== 'object' || value === null || Array.isArray(value))
125
+ return false;
126
+ const proto = Object.getPrototypeOf(value);
127
+ return proto === Object.prototype || proto === null;
128
+ }
129
+ //# sourceMappingURL=state-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-store.js","sourceRoot":"","sources":["../src/state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AA0FpD,SAAS,gBAAgB,CACvB,QAAuC,EACvC,KAAQ;IAER,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACtC,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC/B,IAAI,OAAO,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;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AQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACtF,MAAM,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAkB,CAAC;IAC5D,OAAO,KAAK,KAAK,MAAM,CAAC,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC;AACtD,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A provider-neutral abstraction over "how many tokens will this text cost" (Section 26).
3
+ * The context package must not couple itself to any one provider's tokenizer - a host
4
+ * application can supply a more accurate estimator (e.g. backed by a real tokenizer) via
5
+ * `ContextEngineOptions.estimator`; the default below is a documented approximation only.
6
+ */
7
+ export interface TokenEstimator {
8
+ estimate(text: string): number;
9
+ }
10
+ /**
11
+ * A simple, dependency-free heuristic (~4 characters per token, English-text average),
12
+ * rounded up. This is intentionally approximate - see Section 26 - and good enough for
13
+ * deterministic budgeting decisions; it is not a substitute for a real tokenizer when
14
+ * precise accounting matters.
15
+ */
16
+ export declare function createDefaultTokenEstimator(): TokenEstimator;
17
+ //# 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;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,IAAI,cAAc,CAO5D"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A simple, dependency-free heuristic (~4 characters per token, English-text average),
3
+ * rounded up. This is intentionally approximate - see Section 26 - and good enough for
4
+ * deterministic budgeting decisions; it is not a substitute for a real tokenizer when
5
+ * precise accounting matters.
6
+ */
7
+ export function createDefaultTokenEstimator() {
8
+ return {
9
+ estimate(text) {
10
+ if (!text)
11
+ return 0;
12
+ return Math.max(1, Math.ceil(text.length / 4));
13
+ },
14
+ };
15
+ }
16
+ //# sourceMappingURL=token-estimator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-estimator.js","sourceRoot":"","sources":["../src/token-estimator.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B;IACzC,OAAO;QACL,QAAQ,CAAC,IAAY;YACnB,IAAI,CAAC,IAAI;gBAAE,OAAO,CAAC,CAAC;YACpB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@gixcopilot/context",
3
+ "version": "0.1.0",
4
+ "description": "Framework-independent application context and shared state engine for the AI Copilot SDK: registration, scopes, priority, serialization, token budgeting, and resolution.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AfoudaSWE/gix_ai_copilot_nx.git",
9
+ "directory": "packages/context"
10
+ },
11
+ "homepage": "https://github.com/AfoudaSWE/gix_ai_copilot_nx/tree/main/packages/context#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/AfoudaSWE/gix_ai_copilot_nx/issues"
14
+ },
15
+ "type": "module",
16
+ "sideEffects": false,
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "!dist/**/*.spec.*",
29
+ "!dist/**/*.spec-helper.*",
30
+ "!dist/**/*.integration.spec.*",
31
+ "!dist/**/*.tsbuildinfo"
32
+ ],
33
+ "engines": {
34
+ "node": ">=22.12.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@gixcopilot/protocol": "0.1.0"
41
+ },
42
+ "devDependencies": {
43
+ "vitest": "5.0.1"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -b tsconfig.json",
47
+ "typecheck": "tsc -b tsconfig.json",
48
+ "test": "vitest run",
49
+ "lint": "eslint src"
50
+ }
51
+ }