@howaboua/pi-durable-context 0.0.0-stage → 0.9.1
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 +21 -0
- package/NOTICE +63 -0
- package/README.md +168 -2
- package/dist/history-search-worker.js +85 -0
- package/dist/index.js +2628 -0
- package/dist/internal/context-visibility.d.ts +6 -0
- package/dist/node.js +124 -0
- package/dist/packages/context/src/admission.d.ts +23 -0
- package/dist/packages/context/src/history-archive.d.ts +24 -0
- package/dist/packages/context/src/history-items.d.ts +17 -0
- package/dist/packages/context/src/history-ranker.d.ts +27 -0
- package/dist/packages/context/src/history-search-worker.d.ts +15 -0
- package/dist/packages/context/src/history.d.ts +80 -0
- package/dist/packages/context/src/index.d.ts +72 -0
- package/dist/packages/context/src/instruction-run.d.ts +13 -0
- package/dist/packages/context/src/lifecycle.d.ts +40 -0
- package/dist/packages/context/src/model-roles.d.ts +26 -0
- package/dist/packages/context/src/node.d.ts +10 -0
- package/dist/packages/context/src/note-path.d.ts +2 -0
- package/dist/packages/context/src/note-store.d.ts +53 -0
- package/dist/packages/context/src/notes.d.ts +76 -0
- package/dist/packages/context/src/policy.d.ts +24 -0
- package/dist/packages/context/src/presentation.d.ts +52 -0
- package/dist/packages/context/src/query-input.d.ts +3 -0
- package/dist/packages/context/src/remote.d.ts +34 -0
- package/dist/packages/context/src/rollover-preparation.d.ts +16 -0
- package/dist/packages/context/src/rollover.d.ts +32 -0
- package/dist/packages/context/src/tool-contract.d.ts +85 -0
- package/dist/packages/context/src/tool-rollover.d.ts +41 -0
- package/dist/packages/context/src/usage.d.ts +24 -0
- package/dist/packages/context/src/window-state.d.ts +37 -0
- package/dist/presentation.js +93 -0
- package/package.json +50 -5
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Inventory and an optional bounded read result, not management's persisted state. */
|
|
2
|
+
export interface ContextNoteItem {
|
|
3
|
+
path: string;
|
|
4
|
+
bytes: number;
|
|
5
|
+
created_at: string;
|
|
6
|
+
updated_at: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ContextPresentationState {
|
|
9
|
+
files: ContextNoteItem[];
|
|
10
|
+
detail: (ContextNoteItem & {
|
|
11
|
+
content: string;
|
|
12
|
+
/** UTF-16 length of the selected lines, before character paging. */
|
|
13
|
+
total_chars?: number;
|
|
14
|
+
/** Continue with offset_chars and the same line range. */
|
|
15
|
+
next_offset_chars?: number;
|
|
16
|
+
start_line: number;
|
|
17
|
+
stop_line: number;
|
|
18
|
+
total_lines: number;
|
|
19
|
+
}) | null;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseContextPresentationState(value: unknown): ContextPresentationState;
|
|
22
|
+
export declare const contextCapability: {
|
|
23
|
+
id: string;
|
|
24
|
+
version: number;
|
|
25
|
+
parseState: typeof parseContextPresentationState;
|
|
26
|
+
actions: string[];
|
|
27
|
+
streams: never[];
|
|
28
|
+
presentations: string[];
|
|
29
|
+
};
|
|
30
|
+
export declare const contextSummaryPresentation: {
|
|
31
|
+
id: string;
|
|
32
|
+
requests: string[];
|
|
33
|
+
select(state: ContextPresentationState): {
|
|
34
|
+
count: number;
|
|
35
|
+
bytes: number;
|
|
36
|
+
files: ContextNoteItem[];
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export declare const contextDetailPresentation: {
|
|
40
|
+
id: string;
|
|
41
|
+
requests: never[];
|
|
42
|
+
select: (state: ContextPresentationState) => (ContextNoteItem & {
|
|
43
|
+
content: string;
|
|
44
|
+
/** UTF-16 length of the selected lines, before character paging. */
|
|
45
|
+
total_chars?: number;
|
|
46
|
+
/** Continue with offset_chars and the same line range. */
|
|
47
|
+
next_offset_chars?: number;
|
|
48
|
+
start_line: number;
|
|
49
|
+
stop_line: number;
|
|
50
|
+
total_lines: number;
|
|
51
|
+
}) | null;
|
|
52
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function normalizeQueryInput(namespace: "notes" | "history", input: unknown): Record<string, unknown>;
|
|
2
|
+
/** Failed write attempts still invalidate freshness even when preparation rejected their fields. */
|
|
3
|
+
export declare function isNoteWrite(input: unknown): boolean;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { ConversationId, Storage } from "@earendil-works/pi-durable";
|
|
3
|
+
/** Host-owned stable family identities. This read-only catalogue never grants visibility. */
|
|
4
|
+
export interface ContextRemoteCatalog {
|
|
5
|
+
catalog(caller: ConversationId, context: Context): Promise<{
|
|
6
|
+
familyId: string;
|
|
7
|
+
currentAgentName: string;
|
|
8
|
+
agents: readonly {
|
|
9
|
+
agentName: string;
|
|
10
|
+
conversationId: ConversationId;
|
|
11
|
+
}[];
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
export declare function resolveContextTarget(remote: ContextRemoteCatalog | undefined, args: {
|
|
15
|
+
target?: string | undefined;
|
|
16
|
+
agent_name?: string | null | undefined;
|
|
17
|
+
}, caller: ConversationId, context: Context): Promise<{
|
|
18
|
+
target: ConversationId;
|
|
19
|
+
familyTargets?: readonly ConversationId[];
|
|
20
|
+
}>;
|
|
21
|
+
export declare function remoteContextHelp(remote: ContextRemoteCatalog | undefined, caller: ConversationId, storage: Storage, context: Context): Promise<{
|
|
22
|
+
remote?: never;
|
|
23
|
+
agent_name?: never;
|
|
24
|
+
} | {
|
|
25
|
+
remote: {
|
|
26
|
+
family_id: string;
|
|
27
|
+
current_agent_name: string;
|
|
28
|
+
agents: {
|
|
29
|
+
agent_name: string;
|
|
30
|
+
target: string;
|
|
31
|
+
}[];
|
|
32
|
+
};
|
|
33
|
+
agent_name: string;
|
|
34
|
+
}>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type Conversation, type ConversationId, type EntryId, type TaskId, type Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
export type BeforeRollover = (request: {
|
|
4
|
+
conversationId: ConversationId;
|
|
5
|
+
windowId: string;
|
|
6
|
+
nextWindowId: string;
|
|
7
|
+
reason: "idle" | "manual" | "tool";
|
|
8
|
+
}, context: Context) => Promise<void>;
|
|
9
|
+
type Preparation = {
|
|
10
|
+
head?: EntryId | null;
|
|
11
|
+
error?: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function latestContextEntry(tx: Tx, id: ConversationId): Promise<EntryId | null>;
|
|
14
|
+
/** Await host effects before retiring history, but never replay an uncertain effect. */
|
|
15
|
+
export declare function prepareRollover(conversation: Conversation, taskId: TaskId, callback: BeforeRollover | undefined, request: Parameters<BeforeRollover>[0], ready: (tx: Tx) => Promise<boolean>, context: Context): Promise<Preparation>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { ConversationId, SubmissionId, TaskId, Tx } from "@earendil-works/pi-durable";
|
|
3
|
+
import { type ContextHost } from "./lifecycle.ts";
|
|
4
|
+
import { type BeforeRollover } from "./rollover-preparation.ts";
|
|
5
|
+
import { type TransitionResult, type WindowIdentity } from "./window-state.ts";
|
|
6
|
+
type RolloverInput = {
|
|
7
|
+
reason: "idle" | "manual" | "tool";
|
|
8
|
+
continue: boolean;
|
|
9
|
+
windowId: string;
|
|
10
|
+
};
|
|
11
|
+
type RolloverState = {
|
|
12
|
+
phase: "prepare";
|
|
13
|
+
} | {
|
|
14
|
+
phase: "checkpoint";
|
|
15
|
+
submission: SubmissionId;
|
|
16
|
+
} | {
|
|
17
|
+
phase: "instruction";
|
|
18
|
+
generation: TaskId;
|
|
19
|
+
} | {
|
|
20
|
+
phase: "rotate";
|
|
21
|
+
} | {
|
|
22
|
+
phase: "continue";
|
|
23
|
+
window: WindowIdentity;
|
|
24
|
+
} | {
|
|
25
|
+
phase: "continued";
|
|
26
|
+
window: WindowIdentity;
|
|
27
|
+
generation: TaskId;
|
|
28
|
+
};
|
|
29
|
+
export declare function createRollover(host: () => ContextHost, beforeRollover?: BeforeRollover): import("@earendil-works/pi-durable").Task<RolloverInput, RolloverState, TransitionResult, object>;
|
|
30
|
+
export declare function scheduleRollover(tx: Tx, id: ConversationId, task: ReturnType<typeof createRollover>, reason: RolloverInput["reason"], owner?: TaskId): Promise<TaskId<TransitionResult>>;
|
|
31
|
+
export declare function transitionResult(host: ContextHost, id: TaskId<TransitionResult>, context: Context): Promise<TransitionResult>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ConversationId, JsonObject } from "@earendil-works/pi-durable";
|
|
2
|
+
import { type Static, Type } from "typebox";
|
|
3
|
+
/** Source results are complete. Override Durable's configurable truncation defaults. */
|
|
4
|
+
export declare const contextOutputLimits: {
|
|
5
|
+
maxBytes: number;
|
|
6
|
+
maxLines: number;
|
|
7
|
+
};
|
|
8
|
+
/** Keep UTF-16 offsets without splitting a Unicode character across JSON/IPC pages. */
|
|
9
|
+
export declare function contextTextSlice(text: string, offset: number, limit: number): string;
|
|
10
|
+
/** One bounded value for model text and programs; full read metadata stays host-side. */
|
|
11
|
+
export declare function contextToolResult(result: JsonObject, details?: JsonObject): {
|
|
12
|
+
output: {
|
|
13
|
+
type: "text";
|
|
14
|
+
text: string;
|
|
15
|
+
}[];
|
|
16
|
+
structuredOutput: JsonObject;
|
|
17
|
+
details: JsonObject;
|
|
18
|
+
};
|
|
19
|
+
export declare const historyParameters: Type.TObject<{
|
|
20
|
+
action: Type.TOptional<Type.TUnion<Type.TLiteral<"help" | "list" | "list_windows" | "read" | "search">[]>>;
|
|
21
|
+
target: Type.TOptional<Type.TString>;
|
|
22
|
+
item_id: Type.TOptional<Type.TString>;
|
|
23
|
+
agent_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
24
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
25
|
+
limit_chars: Type.TOptional<Type.TInteger>;
|
|
26
|
+
max_chars_per_item: Type.TOptional<Type.TInteger>;
|
|
27
|
+
offset_chars: Type.TOptional<Type.TInteger>;
|
|
28
|
+
query: Type.TOptional<Type.TString>;
|
|
29
|
+
recent_first: Type.TOptional<Type.TBoolean>;
|
|
30
|
+
role: Type.TOptional<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"system">, Type.TLiteral<"developer">, Type.TNull]>>;
|
|
31
|
+
tool_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
32
|
+
tool_namespace: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
33
|
+
window_id: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const notesParameters: Type.TObject<{
|
|
36
|
+
action: Type.TOptional<Type.TUnion<Type.TLiteral<"append" | "help" | "list" | "read" | "search" | "write">[]>>;
|
|
37
|
+
target: Type.TOptional<Type.TString>;
|
|
38
|
+
agent_name: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
39
|
+
file_order: Type.TOptional<Type.TUnion<[Type.TLiteral<"ascending">, Type.TLiteral<"descending">]>>;
|
|
40
|
+
file_order_by: Type.TOptional<Type.TUnion<[Type.TLiteral<"name">, Type.TLiteral<"created_at">, Type.TLiteral<"updated_at">]>>;
|
|
41
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
42
|
+
max_matches_per_file: Type.TOptional<Type.TInteger>;
|
|
43
|
+
path: Type.TOptional<Type.TUnion<[Type.TString, Type.TNull]>>;
|
|
44
|
+
query: Type.TOptional<Type.TString>;
|
|
45
|
+
recent_file_first: Type.TOptional<Type.TBoolean>;
|
|
46
|
+
start_line: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
|
|
47
|
+
stop_line: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
|
|
48
|
+
limit_chars: Type.TOptional<Type.TInteger>;
|
|
49
|
+
offset_chars: Type.TOptional<Type.TInteger>;
|
|
50
|
+
text: Type.TOptional<Type.TString>;
|
|
51
|
+
}>;
|
|
52
|
+
export type NotesArguments = Static<typeof notesParameters>;
|
|
53
|
+
export type HistoryArguments = Static<typeof historyParameters>;
|
|
54
|
+
export declare const newContextParameters: Type.TObject<{
|
|
55
|
+
role: Type.TOptional<Type.TString>;
|
|
56
|
+
}>;
|
|
57
|
+
export declare function prepareNewContextArguments(input: unknown): {
|
|
58
|
+
role?: string;
|
|
59
|
+
};
|
|
60
|
+
export declare function prepareNotesArguments(input: unknown): NotesArguments;
|
|
61
|
+
export declare function prepareHistoryArguments(input: unknown): HistoryArguments;
|
|
62
|
+
export declare function contextToolHelp(namespace: "notes" | "history", args: NotesArguments | HistoryArguments, rankedHistory?: boolean): {
|
|
63
|
+
actions: {
|
|
64
|
+
[k: string]: string;
|
|
65
|
+
};
|
|
66
|
+
target: string;
|
|
67
|
+
writes: string;
|
|
68
|
+
paths: string;
|
|
69
|
+
usage: string;
|
|
70
|
+
ranges: string;
|
|
71
|
+
paging: string;
|
|
72
|
+
defaults: string;
|
|
73
|
+
} | {
|
|
74
|
+
actions: {
|
|
75
|
+
[k: string]: string;
|
|
76
|
+
};
|
|
77
|
+
target: string;
|
|
78
|
+
usage: string;
|
|
79
|
+
search: string;
|
|
80
|
+
} | undefined;
|
|
81
|
+
export declare function validateNotesArguments(args: NotesArguments): void;
|
|
82
|
+
export declare function validateHistoryArguments(args: HistoryArguments): void;
|
|
83
|
+
export declare function targetConversationId(value: string | undefined, caller: ConversationId): ConversationId;
|
|
84
|
+
export declare function requiredString(value: unknown, message: string, allowEmpty?: boolean): string;
|
|
85
|
+
export declare function boundedInteger(value: unknown, fallback: number, maximum: number, minimum?: number): number;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { type ConversationId } from "@earendil-works/pi-durable";
|
|
3
|
+
import { type ContextHost } from "./lifecycle.ts";
|
|
4
|
+
import { type ContextModelRoles } from "./model-roles.ts";
|
|
5
|
+
import { type BeforeRollover } from "./rollover-preparation.ts";
|
|
6
|
+
/** A tool-requested cut stays inside its native input, so every submission waiter sees the final answer. */
|
|
7
|
+
export declare function createToolRollover(host: () => ContextHost, now: () => number, policy: {
|
|
8
|
+
newContextRequests: "off" | "pi-limits" | "unrestricted";
|
|
9
|
+
modelRoles?: ContextModelRoles | undefined;
|
|
10
|
+
beforeRollover?: BeforeRollover | undefined;
|
|
11
|
+
hasEligibleContextHistory?: (conversationId: ConversationId, context: Context) => boolean | Promise<boolean>;
|
|
12
|
+
}): {
|
|
13
|
+
tool: {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
parameters: import("typebox").TObject<{
|
|
17
|
+
role: import("typebox").TOptional<import("typebox").TString>;
|
|
18
|
+
}>;
|
|
19
|
+
constrainedSampling?: false | import("@earendil-works/pi-ai").ConstrainedSamplingConfig;
|
|
20
|
+
replay?: "safe" | "unsafe";
|
|
21
|
+
executionMode?: import("@earendil-works/pi-durable").ToolExecutionMode;
|
|
22
|
+
prepareArguments?(args: unknown): {
|
|
23
|
+
role?: string;
|
|
24
|
+
};
|
|
25
|
+
structuredOutputSchema?: import("typebox").TSchema;
|
|
26
|
+
callers?: readonly ("model" | "tools")[];
|
|
27
|
+
deferred?: true;
|
|
28
|
+
outputLimits?: {
|
|
29
|
+
readonly maxBytes?: number;
|
|
30
|
+
readonly maxLines?: number;
|
|
31
|
+
readonly retain?: "head" | "tail";
|
|
32
|
+
};
|
|
33
|
+
execute(args: {
|
|
34
|
+
role?: string;
|
|
35
|
+
}, api: import("@earendil-works/pi-durable").ToolExecutionApi<import("@earendil-works/chord").JsonValue>, context: Context): Promise<import("@earendil-works/pi-durable").ToolExecutionResult<import("@earendil-works/chord").JsonValue>>;
|
|
36
|
+
executionHints: {
|
|
37
|
+
nativeOnly: true;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
hooks: import("@earendil-works/pi-durable").HookRegistration[];
|
|
41
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import type { AssistantMessage, Models } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { Conversation } from "@earendil-works/pi-durable";
|
|
4
|
+
/** Provider usage is a baseline, not an exact measurement of the unsent tail. */
|
|
5
|
+
export declare function contextUsage(conversation: Conversation, models: Models, context: Context, answer?: AssistantMessage): Promise<{
|
|
6
|
+
known: boolean;
|
|
7
|
+
reason: string | null;
|
|
8
|
+
model: {
|
|
9
|
+
provider: string;
|
|
10
|
+
id: string;
|
|
11
|
+
} | null;
|
|
12
|
+
usageModel: {
|
|
13
|
+
provider: string;
|
|
14
|
+
id: string;
|
|
15
|
+
} | null;
|
|
16
|
+
contextWindow: number | null;
|
|
17
|
+
usageTokens: number | null;
|
|
18
|
+
trailingTokens: number | null;
|
|
19
|
+
usedTokens: number | null;
|
|
20
|
+
remainingTokens: number | null;
|
|
21
|
+
percentage: number | null;
|
|
22
|
+
estimated: boolean;
|
|
23
|
+
uncertainty: string;
|
|
24
|
+
}>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ConversationId, EntryId, SubmissionId, TaskId, Tx } from "@earendil-works/pi-durable";
|
|
2
|
+
export type WindowIdentity = {
|
|
3
|
+
id: string;
|
|
4
|
+
number: number;
|
|
5
|
+
startedAt: number;
|
|
6
|
+
previous?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const WindowEntry: import("@earendil-works/pi-durable").Entry<WindowIdentity>;
|
|
9
|
+
export type TransitionResult = {
|
|
10
|
+
status: "done";
|
|
11
|
+
window: WindowIdentity;
|
|
12
|
+
continuation?: SubmissionId;
|
|
13
|
+
answer?: EntryId;
|
|
14
|
+
} | {
|
|
15
|
+
status: "failed";
|
|
16
|
+
reason: string;
|
|
17
|
+
} | {
|
|
18
|
+
status: "skipped";
|
|
19
|
+
reason: string;
|
|
20
|
+
} | {
|
|
21
|
+
status: "cancelled";
|
|
22
|
+
};
|
|
23
|
+
export declare const WindowState: import("@earendil-works/pi-durable").RewindableConversationDocToken<{
|
|
24
|
+
window?: WindowIdentity;
|
|
25
|
+
entry?: EntryId;
|
|
26
|
+
settlement?: {
|
|
27
|
+
observedAt: number;
|
|
28
|
+
userEntry: EntryId | null;
|
|
29
|
+
/** Missing on older saved settlements, which cannot authorize automatic cuts. */
|
|
30
|
+
completed?: boolean;
|
|
31
|
+
};
|
|
32
|
+
transition?: TaskId<TransitionResult>;
|
|
33
|
+
reminded: number;
|
|
34
|
+
taskBoundaryReminded?: string;
|
|
35
|
+
}>;
|
|
36
|
+
/** A note is fresh only for the input visible when its original write committed. */
|
|
37
|
+
export declare function latestUserEntry(tx: Tx, conversationId: ConversationId): Promise<EntryId | null>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// packages/context/src/presentation.ts
|
|
2
|
+
function object(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
4
|
+
throw new TypeError("Expected an object");
|
|
5
|
+
return Object.fromEntries(Object.entries(value));
|
|
6
|
+
}
|
|
7
|
+
function string(value) {
|
|
8
|
+
if (typeof value !== "string")
|
|
9
|
+
throw new TypeError("Expected a string");
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function array(value, parse) {
|
|
13
|
+
if (!Array.isArray(value))
|
|
14
|
+
throw new TypeError("Expected an array");
|
|
15
|
+
return value.map(parse);
|
|
16
|
+
}
|
|
17
|
+
function integer(value, minimum = 0) {
|
|
18
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum)
|
|
19
|
+
throw new TypeError("Expected a non-negative safe integer");
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function parseNote(value) {
|
|
23
|
+
const item = object(value);
|
|
24
|
+
const path = string(item["path"]);
|
|
25
|
+
if (!path.startsWith("/notes/") || path.split("/").some((part) => part === "." || part === ".."))
|
|
26
|
+
throw new TypeError("Expected a normalized virtual note path");
|
|
27
|
+
const created_at = string(item["created_at"]);
|
|
28
|
+
const updated_at = string(item["updated_at"]);
|
|
29
|
+
if (!Number.isFinite(Date.parse(created_at)) || !Number.isFinite(Date.parse(updated_at)))
|
|
30
|
+
throw new TypeError("Invalid note timestamp");
|
|
31
|
+
return { path, bytes: integer(item["bytes"]), created_at, updated_at };
|
|
32
|
+
}
|
|
33
|
+
function parseContextPresentationState(value) {
|
|
34
|
+
const state = object(value);
|
|
35
|
+
const files = array(state["files"], parseNote);
|
|
36
|
+
let detail = null;
|
|
37
|
+
if (state["detail"] !== null) {
|
|
38
|
+
const item = object(state["detail"]);
|
|
39
|
+
const start_line = integer(item["start_line"], 1);
|
|
40
|
+
const stop_line = integer(item["stop_line"], 1);
|
|
41
|
+
if (stop_line < start_line)
|
|
42
|
+
throw new TypeError("Invalid note line range");
|
|
43
|
+
detail = {
|
|
44
|
+
...parseNote(item),
|
|
45
|
+
content: string(item["content"]),
|
|
46
|
+
start_line,
|
|
47
|
+
stop_line,
|
|
48
|
+
total_lines: integer(item["total_lines"], 1)
|
|
49
|
+
};
|
|
50
|
+
if (item["total_chars"] !== undefined)
|
|
51
|
+
detail.total_chars = integer(item["total_chars"], detail.content.length);
|
|
52
|
+
if (item["next_offset_chars"] !== undefined) {
|
|
53
|
+
const next = integer(item["next_offset_chars"], 1);
|
|
54
|
+
if (detail.total_chars === undefined || next >= detail.total_chars || next < detail.content.length)
|
|
55
|
+
throw new TypeError("Invalid note character continuation");
|
|
56
|
+
detail.next_offset_chars = next;
|
|
57
|
+
}
|
|
58
|
+
const metadata = files.find((file) => file.path === detail?.path);
|
|
59
|
+
if (!metadata || metadata.bytes !== detail.bytes || metadata.updated_at !== detail.updated_at || metadata.created_at !== detail.created_at)
|
|
60
|
+
throw new TypeError("Note detail does not match the inventory");
|
|
61
|
+
}
|
|
62
|
+
return { files, detail };
|
|
63
|
+
}
|
|
64
|
+
var contextCapability = {
|
|
65
|
+
id: "context.notes",
|
|
66
|
+
version: 1,
|
|
67
|
+
parseState: parseContextPresentationState,
|
|
68
|
+
actions: ["notes"],
|
|
69
|
+
streams: [],
|
|
70
|
+
presentations: ["context.notes.summary", "context.notes.detail"]
|
|
71
|
+
};
|
|
72
|
+
var contextSummaryPresentation = {
|
|
73
|
+
id: "context.notes.summary",
|
|
74
|
+
requests: ["context.notes.detail"],
|
|
75
|
+
select(state) {
|
|
76
|
+
return {
|
|
77
|
+
count: state.files.length,
|
|
78
|
+
bytes: state.files.reduce((sum, file) => sum + file.bytes, 0),
|
|
79
|
+
files: state.files
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var contextDetailPresentation = {
|
|
84
|
+
id: "context.notes.detail",
|
|
85
|
+
requests: [],
|
|
86
|
+
select: (state) => state.detail
|
|
87
|
+
};
|
|
88
|
+
export {
|
|
89
|
+
contextCapability,
|
|
90
|
+
contextDetailPresentation,
|
|
91
|
+
contextSummaryPresentation,
|
|
92
|
+
parseContextPresentationState
|
|
93
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@howaboua/pi-durable-context",
|
|
3
|
+
"version": "0.9.1",
|
|
4
|
+
"description": "Local notes, retained history and durable context rollover",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/IgorWarzocha/pi-durable-components.git",
|
|
10
|
+
"directory": "packages/context"
|
|
11
|
+
},
|
|
12
|
+
"author": "Igor Warzocha",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=22.19.0"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/packages/context/src/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
"./node": {
|
|
20
|
+
"types": "./dist/packages/context/src/node.d.ts",
|
|
21
|
+
"import": "./dist/node.js"
|
|
22
|
+
},
|
|
23
|
+
"./presentation": {
|
|
24
|
+
"types": "./dist/packages/context/src/presentation.d.ts",
|
|
25
|
+
"import": "./dist/presentation.js"
|
|
26
|
+
},
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/packages/context/src/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"NOTICE",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"typebox": "1.3.27"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@earendil-works/chord": "1.4.0",
|
|
44
|
+
"@earendil-works/pi-ai": "1.4.0",
|
|
45
|
+
"@earendil-works/pi-durable": "1.4.0",
|
|
46
|
+
"@modelcontextprotocol/sdk": "1.25.2"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|