@lloyal-labs/sdk 1.0.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.
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Session = void 0;
4
+ const deltas_1 = require("./deltas");
5
+ /**
6
+ * Session - Trunk lifecycle + conversation delta helpers
7
+ *
8
+ * Owns the current "trunk" branch and provides promote() to crown a winner,
9
+ * plus delta helpers that centralize the sep + formatChat + tokenize + prefill
10
+ * pattern for injecting new turns into an ongoing conversation.
11
+ *
12
+ * Session does NOT own the SessionContext or BranchStore — the consumer
13
+ * creates those and passes them in. dispose() prunes trunk only.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const session = new Session({ ctx, store });
18
+ * session.trunk = initialBranch;
19
+ *
20
+ * // After verification, promote the best attempt
21
+ * await session.promote(bestAttempt.branch);
22
+ *
23
+ * // Inject a user turn and generate
24
+ * await session.prefillUser('What about X?');
25
+ * for await (const { text } of session.trunk) {
26
+ * process.stdout.write(text);
27
+ * }
28
+ *
29
+ * // Cleanup
30
+ * await session.dispose();
31
+ * ctx.dispose();
32
+ * ```
33
+ *
34
+ * @category Branching
35
+ */
36
+ class Session {
37
+ _ctx;
38
+ _store;
39
+ _trunk;
40
+ constructor({ ctx, store }) {
41
+ this._ctx = ctx;
42
+ this._store = store;
43
+ this._trunk = null;
44
+ }
45
+ /** Current trunk branch */
46
+ get trunk() {
47
+ return this._trunk;
48
+ }
49
+ /** Assign initial trunk (no promote) */
50
+ set trunk(branch) {
51
+ this._trunk = branch;
52
+ }
53
+ /**
54
+ * Promote a winner to trunk — retainOnly + reassign
55
+ *
56
+ * Safe even if winner is the only branch (resets topology, no-op on KV).
57
+ */
58
+ async promote(winner) {
59
+ await this._store.retainOnly(winner);
60
+ this._trunk = winner;
61
+ }
62
+ /**
63
+ * Dispose trunk only — consumer owns ctx and other resources
64
+ */
65
+ async dispose() {
66
+ if (this._trunk && !this._trunk.disposed) {
67
+ await this._trunk.prune();
68
+ }
69
+ this._trunk = null;
70
+ }
71
+ /**
72
+ * Prefill a user turn into trunk
73
+ *
74
+ * @param content - User message content
75
+ * @param opts - Optional tools JSON string
76
+ */
77
+ async prefillUser(content, opts = {}) {
78
+ const tokens = (0, deltas_1.buildUserDelta)(this._ctx, content, opts);
79
+ await this._trunk.prefill(tokens);
80
+ }
81
+ /**
82
+ * Prefill a tool result turn into trunk
83
+ *
84
+ * @param resultStr - JSON-stringified tool result
85
+ * @param callId - Tool call ID
86
+ */
87
+ async prefillToolResult(resultStr, callId) {
88
+ const tokens = (0, deltas_1.buildToolResultDelta)(this._ctx, resultStr, callId);
89
+ await this._trunk.prefill(tokens);
90
+ }
91
+ }
92
+ exports.Session = Session;
93
+ //# sourceMappingURL=Session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Session.js","sourceRoot":"","sources":["../src/Session.ts"],"names":[],"mappings":";;;AAGA,qCAAgE;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAa,OAAO;IACV,IAAI,CAAiB;IACrB,MAAM,CAAc;IACpB,MAAM,CAAgB;IAE9B,YAAY,EAAE,GAAG,EAAE,KAAK,EAA+C;QACrE,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,2BAA2B;IAC3B,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,wCAAwC;IACxC,IAAI,KAAK,CAAC,MAAqB;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,MAAc;QAC1B,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAAO;QACX,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACzC,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC5B,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACrB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,OAAe,EAAE,OAA2B,EAAE;QAC9D,MAAM,MAAM,GAAG,IAAA,uBAAc,EAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QACxD,MAAM,IAAI,CAAC,MAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,iBAAiB,CAAC,SAAiB,EAAE,MAAc;QACvD,MAAM,MAAM,GAAG,IAAA,6BAAoB,EAAC,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAClE,MAAM,IAAI,CAAC,MAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;CACF;AA9DD,0BA8DC"}
@@ -0,0 +1,37 @@
1
+ import type { SessionContext } from './types';
2
+ /**
3
+ * Build a token delta for a user turn
4
+ *
5
+ * Composes `getTurnSeparator()` + `formatChatSync()` + `tokenizeSync()` into a
6
+ * single token array suitable for `branch.prefill()`. Usable with any
7
+ * branch — not tied to {@link Session}'s trunk.
8
+ *
9
+ * This is the canonical way to build a user-turn delta for warm prefill
10
+ * in multi-turn conversations.
11
+ *
12
+ * @param ctx - Active session context
13
+ * @param content - User message content
14
+ * @param opts - Optional tools JSON for tool-aware formatting
15
+ * @returns Token array ready for `branch.prefill()`
16
+ *
17
+ * @category Agents
18
+ */
19
+ export declare function buildUserDelta(ctx: SessionContext, content: string, opts?: {
20
+ tools?: string;
21
+ }): number[];
22
+ /**
23
+ * Build a token delta for a tool result turn
24
+ *
25
+ * Composes `getTurnSeparator()` + `formatChatSync()` + `tokenizeSync()` into a
26
+ * single token array suitable for `branch.prefill()`. Used by
27
+ * {@link useAgentPool} to inject tool results back into agent context.
28
+ *
29
+ * @param ctx - Active session context
30
+ * @param resultStr - JSON-serialized tool result
31
+ * @param callId - Tool call identifier from the model's parsed output
32
+ * @returns Token array ready for `branch.prefill()`
33
+ *
34
+ * @category Agents
35
+ */
36
+ export declare function buildToolResultDelta(ctx: SessionContext, resultStr: string, callId: string): number[];
37
+ //# sourceMappingURL=deltas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deltas.d.ts","sourceRoot":"","sources":["../src/deltas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,cAAc,EACnB,OAAO,EAAE,MAAM,EACf,IAAI,GAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GAC5B,MAAM,EAAE,CASV;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,cAAc,EACnB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,MAAM,EAAE,CAUV"}
package/dist/deltas.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildUserDelta = buildUserDelta;
4
+ exports.buildToolResultDelta = buildToolResultDelta;
5
+ /**
6
+ * Build a token delta for a user turn
7
+ *
8
+ * Composes `getTurnSeparator()` + `formatChatSync()` + `tokenizeSync()` into a
9
+ * single token array suitable for `branch.prefill()`. Usable with any
10
+ * branch — not tied to {@link Session}'s trunk.
11
+ *
12
+ * This is the canonical way to build a user-turn delta for warm prefill
13
+ * in multi-turn conversations.
14
+ *
15
+ * @param ctx - Active session context
16
+ * @param content - User message content
17
+ * @param opts - Optional tools JSON for tool-aware formatting
18
+ * @returns Token array ready for `branch.prefill()`
19
+ *
20
+ * @category Agents
21
+ */
22
+ function buildUserDelta(ctx, content, opts = {}) {
23
+ const sep = ctx.getTurnSeparator();
24
+ const fmtOpts = opts.tools ? { tools: opts.tools } : {};
25
+ const { prompt } = ctx.formatChatSync(JSON.stringify([{ role: 'system', content: '' }, { role: 'user', content }]), fmtOpts);
26
+ const delta = ctx.tokenizeSync(prompt, false);
27
+ return [...sep, ...delta];
28
+ }
29
+ /**
30
+ * Build a token delta for a tool result turn
31
+ *
32
+ * Composes `getTurnSeparator()` + `formatChatSync()` + `tokenizeSync()` into a
33
+ * single token array suitable for `branch.prefill()`. Used by
34
+ * {@link useAgentPool} to inject tool results back into agent context.
35
+ *
36
+ * @param ctx - Active session context
37
+ * @param resultStr - JSON-serialized tool result
38
+ * @param callId - Tool call identifier from the model's parsed output
39
+ * @returns Token array ready for `branch.prefill()`
40
+ *
41
+ * @category Agents
42
+ */
43
+ function buildToolResultDelta(ctx, resultStr, callId) {
44
+ const sep = ctx.getTurnSeparator();
45
+ const { prompt } = ctx.formatChatSync(JSON.stringify([
46
+ { role: 'system', content: '' },
47
+ { role: 'tool', content: resultStr, tool_call_id: callId },
48
+ ]));
49
+ const delta = ctx.tokenizeSync(prompt, false);
50
+ return [...sep, ...delta];
51
+ }
52
+ //# sourceMappingURL=deltas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deltas.js","sourceRoot":"","sources":["../src/deltas.ts"],"names":[],"mappings":";;AAmBA,wCAaC;AAgBD,oDAcC;AA5DD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,cAAc,CAC5B,GAAmB,EACnB,OAAe,EACf,OAA2B,EAAE;IAE7B,MAAM,GAAG,GAAG,GAAG,CAAC,gBAAgB,EAAE,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACxD,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,cAAc,CACnC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,EAC5E,OAAO,CACR,CAAC;IACF,MAAM,KAAK,GAAG,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,oBAAoB,CAClC,GAAmB,EACnB,SAAiB,EACjB,MAAc;IAEd,MAAM,GAAG,GAAG,GAAG,CAAC,gBAAgB,EAAE,CAAC;IACnC,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC,cAAc,CACnC,IAAI,CAAC,SAAS,CAAC;QACb,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE;QAC/B,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE;KAC3D,CAAC,CACH,CAAC;IACF,MAAM,KAAK,GAAG,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,CAAC,GAAG,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,9 @@
1
+ export { Branch } from './Branch';
2
+ export { BranchStore } from './BranchStore';
3
+ export { Session } from './Session';
4
+ export { Rerank } from './Rerank';
5
+ export { buildUserDelta, buildToolResultDelta } from './deltas';
6
+ export { PoolingType, CHAT_FORMAT_CONTENT_ONLY, CHAT_FORMAT_GENERIC, ReasoningFormat, GrammarTriggerType } from './types';
7
+ export type { ChatFormat } from './types';
8
+ export type { GpuVariant, KvCacheType, LoadOptions, ContextOptions, FormatChatOptions, GrammarTrigger, FormattedChatResult, ParseChatOutputOptions, ParsedToolCall, ParseChatOutputResult, PenaltyParams, MirostatParams, DryParams, XtcParams, AdvancedSamplingParams, SamplingParams, SessionContext, Produced, RerankOptions, RerankResult, RerankProgress, } from './types';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAGhE,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,mBAAmB,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAG1H,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,YAAY,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,mBAAmB,EACnB,sBAAsB,EACtB,cAAc,EACd,qBAAqB,EACrB,aAAa,EACb,cAAc,EACd,SAAS,EACT,SAAS,EACT,sBAAsB,EACtB,cAAc,EACd,cAAc,EACd,QAAQ,EACR,aAAa,EACb,YAAY,EACZ,cAAc,GACf,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GrammarTriggerType = exports.ReasoningFormat = exports.CHAT_FORMAT_GENERIC = exports.CHAT_FORMAT_CONTENT_ONLY = exports.PoolingType = exports.buildToolResultDelta = exports.buildUserDelta = exports.Rerank = exports.Session = exports.BranchStore = exports.Branch = void 0;
4
+ var Branch_1 = require("./Branch");
5
+ Object.defineProperty(exports, "Branch", { enumerable: true, get: function () { return Branch_1.Branch; } });
6
+ var BranchStore_1 = require("./BranchStore");
7
+ Object.defineProperty(exports, "BranchStore", { enumerable: true, get: function () { return BranchStore_1.BranchStore; } });
8
+ var Session_1 = require("./Session");
9
+ Object.defineProperty(exports, "Session", { enumerable: true, get: function () { return Session_1.Session; } });
10
+ var Rerank_1 = require("./Rerank");
11
+ Object.defineProperty(exports, "Rerank", { enumerable: true, get: function () { return Rerank_1.Rerank; } });
12
+ var deltas_1 = require("./deltas");
13
+ Object.defineProperty(exports, "buildUserDelta", { enumerable: true, get: function () { return deltas_1.buildUserDelta; } });
14
+ Object.defineProperty(exports, "buildToolResultDelta", { enumerable: true, get: function () { return deltas_1.buildToolResultDelta; } });
15
+ // ── Enums + constants ────────────────────────────────────────
16
+ var types_1 = require("./types");
17
+ Object.defineProperty(exports, "PoolingType", { enumerable: true, get: function () { return types_1.PoolingType; } });
18
+ Object.defineProperty(exports, "CHAT_FORMAT_CONTENT_ONLY", { enumerable: true, get: function () { return types_1.CHAT_FORMAT_CONTENT_ONLY; } });
19
+ Object.defineProperty(exports, "CHAT_FORMAT_GENERIC", { enumerable: true, get: function () { return types_1.CHAT_FORMAT_GENERIC; } });
20
+ Object.defineProperty(exports, "ReasoningFormat", { enumerable: true, get: function () { return types_1.ReasoningFormat; } });
21
+ Object.defineProperty(exports, "GrammarTriggerType", { enumerable: true, get: function () { return types_1.GrammarTriggerType; } });
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,6CAA4C;AAAnC,0GAAA,WAAW,OAAA;AACpB,qCAAoC;AAA3B,kGAAA,OAAO,OAAA;AAChB,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,mCAAgE;AAAvD,wGAAA,cAAc,OAAA;AAAE,8GAAA,oBAAoB,OAAA;AAE7C,gEAAgE;AAChE,iCAA0H;AAAjH,oGAAA,WAAW,OAAA;AAAE,iHAAA,wBAAwB,OAAA;AAAE,4GAAA,mBAAmB,OAAA;AAAE,wGAAA,eAAe,OAAA;AAAE,2GAAA,kBAAkB,OAAA"}