@providerprotocol/ai 0.0.36 → 0.0.37
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/README.md +28 -0
- package/dist/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.js +12 -10
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +2 -2
- package/dist/cerebras/index.js +12 -10
- package/dist/cerebras/index.js.map +1 -1
- package/dist/{chunk-UFFJDYCE.js → chunk-7GTWHZY2.js} +2 -2
- package/dist/{chunk-OIEWDFQU.js → chunk-FYSZFIZS.js} +2 -2
- package/dist/chunk-GIDT7C6I.js +37 -0
- package/dist/chunk-GIDT7C6I.js.map +1 -0
- package/dist/{chunk-IDZOVWP3.js → chunk-IK6NRCW5.js} +7 -7
- package/dist/{chunk-VGKZIGVI.js → chunk-LTEMH3CI.js} +2 -2
- package/dist/{chunk-RJGTRQ47.js → chunk-MJI74VEJ.js} +6 -1
- package/dist/chunk-MJI74VEJ.js.map +1 -0
- package/dist/{chunk-AY55T37A.js → chunk-SBGZJVTJ.js} +4 -30
- package/dist/chunk-SBGZJVTJ.js.map +1 -0
- package/dist/{chunk-A2IM7PGT.js → chunk-TUTYMOBL.js} +2 -2
- package/dist/{chunk-BRP5XJ6Q.js → chunk-WU4U6IHF.js} +2 -2
- package/dist/chunk-WU4U6IHF.js.map +1 -0
- package/dist/{chunk-C4JP64VW.js → chunk-YQLR3XOA.js} +2 -2
- package/dist/chunk-ZKNPQBIE.js +265 -0
- package/dist/chunk-ZKNPQBIE.js.map +1 -0
- package/dist/{chunk-4OGB7JZA.js → chunk-ZRVNAET3.js} +2 -2
- package/dist/{embedding-BXA72PlJ.d.ts → embedding-CwZ1ZNWv.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -2
- package/dist/google/index.js +12 -10
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +2 -2
- package/dist/groq/index.js +12 -10
- package/dist/groq/index.js.map +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +2 -1
- package/dist/{image-stream-CCgwB7ve.d.ts → image-stream-CeQHtjxS.d.ts} +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +86 -283
- package/dist/index.js.map +1 -1
- package/dist/{llm-ByUFPcFH.d.ts → llm-DS_-l71X.d.ts} +11 -3
- package/dist/middleware/logging/index.d.ts +2 -2
- package/dist/middleware/parsed-object/index.d.ts +2 -2
- package/dist/middleware/parsed-object/index.js +1 -1
- package/dist/middleware/persistence/index.d.ts +128 -0
- package/dist/middleware/persistence/index.js +144 -0
- package/dist/middleware/persistence/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +4 -4
- package/dist/middleware/pubsub/server/express/index.d.ts +2 -2
- package/dist/middleware/pubsub/server/fastify/index.d.ts +2 -2
- package/dist/middleware/pubsub/server/h3/index.d.ts +2 -2
- package/dist/middleware/pubsub/server/index.d.ts +2 -2
- package/dist/middleware/pubsub/server/webapi/index.d.ts +2 -2
- package/dist/ollama/index.d.ts +2 -2
- package/dist/ollama/index.js +12 -10
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -2
- package/dist/openai/index.js +12 -10
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -2
- package/dist/openrouter/index.js +12 -10
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +4 -4
- package/dist/proxy/index.js +20 -18
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +4 -4
- package/dist/proxy/server/express/index.js +3 -3
- package/dist/proxy/server/fastify/index.d.ts +4 -4
- package/dist/proxy/server/fastify/index.js +3 -3
- package/dist/proxy/server/h3/index.d.ts +4 -4
- package/dist/proxy/server/h3/index.js +3 -3
- package/dist/proxy/server/index.d.ts +4 -4
- package/dist/proxy/server/index.js +9 -9
- package/dist/proxy/server/webapi/index.d.ts +4 -4
- package/dist/proxy/server/webapi/index.js +3 -3
- package/dist/responses/index.d.ts +2 -2
- package/dist/responses/index.js +12 -10
- package/dist/responses/index.js.map +1 -1
- package/dist/{retry-BDMo4AVu.d.ts → retry-CgoBNa51.d.ts} +1 -1
- package/dist/{stream-S7nwQRqM.d.ts → stream-sXhBtWjl.d.ts} +4 -0
- package/dist/{types-CE4B7pno.d.ts → types-Cr4F0tVy.d.ts} +1 -1
- package/dist/xai/index.d.ts +2 -2
- package/dist/xai/index.js +12 -10
- package/dist/xai/index.js.map +1 -1
- package/package.json +6 -1
- package/dist/chunk-AY55T37A.js.map +0 -1
- package/dist/chunk-BRP5XJ6Q.js.map +0 -1
- package/dist/chunk-RJGTRQ47.js.map +0 -1
- /package/dist/{chunk-UFFJDYCE.js.map → chunk-7GTWHZY2.js.map} +0 -0
- /package/dist/{chunk-OIEWDFQU.js.map → chunk-FYSZFIZS.js.map} +0 -0
- /package/dist/{chunk-IDZOVWP3.js.map → chunk-IK6NRCW5.js.map} +0 -0
- /package/dist/{chunk-VGKZIGVI.js.map → chunk-LTEMH3CI.js.map} +0 -0
- /package/dist/{chunk-A2IM7PGT.js.map → chunk-TUTYMOBL.js.map} +0 -0
- /package/dist/{chunk-C4JP64VW.js.map → chunk-YQLR3XOA.js.map} +0 -0
- /package/dist/{chunk-4OGB7JZA.js.map → chunk-ZRVNAET3.js.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -10,6 +10,9 @@ import {
|
|
|
10
10
|
createTurn,
|
|
11
11
|
emptyUsage
|
|
12
12
|
} from "./chunk-NSE7QN3P.js";
|
|
13
|
+
import {
|
|
14
|
+
Thread
|
|
15
|
+
} from "./chunk-ZKNPQBIE.js";
|
|
13
16
|
import {
|
|
14
17
|
Image
|
|
15
18
|
} from "./chunk-N5DX5JW3.js";
|
|
@@ -25,39 +28,39 @@ import {
|
|
|
25
28
|
resolveImageHandler,
|
|
26
29
|
resolveLLMHandler
|
|
27
30
|
} from "./chunk-JA3UZALR.js";
|
|
31
|
+
import "./chunk-SBGZJVTJ.js";
|
|
32
|
+
import {
|
|
33
|
+
StreamEventType,
|
|
34
|
+
contentBlockStart,
|
|
35
|
+
contentBlockStop,
|
|
36
|
+
createStreamResult,
|
|
37
|
+
messageStart,
|
|
38
|
+
messageStop,
|
|
39
|
+
objectDelta,
|
|
40
|
+
textDelta,
|
|
41
|
+
toolCallDelta,
|
|
42
|
+
toolExecutionEnd,
|
|
43
|
+
toolExecutionStart
|
|
44
|
+
} from "./chunk-MJI74VEJ.js";
|
|
28
45
|
import {
|
|
29
46
|
AssistantMessage,
|
|
30
47
|
Message,
|
|
31
48
|
MessageRole,
|
|
32
49
|
ToolResultMessage,
|
|
33
50
|
UserMessage,
|
|
34
|
-
generateId,
|
|
35
51
|
isAssistantMessage,
|
|
36
52
|
isToolResultMessage,
|
|
37
53
|
isUserMessage
|
|
38
|
-
} from "./chunk-
|
|
54
|
+
} from "./chunk-WU4U6IHF.js";
|
|
39
55
|
import {
|
|
40
56
|
isCancelledError,
|
|
41
57
|
toError
|
|
42
|
-
} from "./chunk-
|
|
58
|
+
} from "./chunk-GIDT7C6I.js";
|
|
43
59
|
import {
|
|
44
60
|
ErrorCode,
|
|
45
61
|
ModalityType,
|
|
46
62
|
UPPError
|
|
47
63
|
} from "./chunk-COS4ON4G.js";
|
|
48
|
-
import {
|
|
49
|
-
StreamEventType,
|
|
50
|
-
contentBlockStart,
|
|
51
|
-
contentBlockStop,
|
|
52
|
-
createStreamResult,
|
|
53
|
-
messageStart,
|
|
54
|
-
messageStop,
|
|
55
|
-
objectDelta,
|
|
56
|
-
textDelta,
|
|
57
|
-
toolCallDelta,
|
|
58
|
-
toolExecutionEnd,
|
|
59
|
-
toolExecutionStart
|
|
60
|
-
} from "./chunk-RJGTRQ47.js";
|
|
61
64
|
|
|
62
65
|
// src/types/content.ts
|
|
63
66
|
var ContentBlockType = {
|
|
@@ -160,6 +163,14 @@ async function runToolHook(middlewares, hook, tool, data, ctx) {
|
|
|
160
163
|
}
|
|
161
164
|
}
|
|
162
165
|
}
|
|
166
|
+
async function runTurnHook(middlewares, turn, ctx) {
|
|
167
|
+
const ordered = [...middlewares].reverse();
|
|
168
|
+
for (const mw of ordered) {
|
|
169
|
+
if (mw.onTurn) {
|
|
170
|
+
await mw.onTurn.call(mw, turn, ctx);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
163
174
|
function createStreamTransformer(middlewares, ctx) {
|
|
164
175
|
const streamMiddlewares = middlewares.filter((mw) => mw.onStreamEvent);
|
|
165
176
|
if (streamMiddlewares.length === 0) {
|
|
@@ -218,6 +229,7 @@ function createStreamContext(state) {
|
|
|
218
229
|
|
|
219
230
|
// src/core/llm.ts
|
|
220
231
|
var DEFAULT_MAX_ITERATIONS = 10;
|
|
232
|
+
var TURN_START_INDEX_KEY = "llm:turnStartIndex";
|
|
221
233
|
function validateMediaCapabilities(messages, capabilities, providerName) {
|
|
222
234
|
for (const msg of messages) {
|
|
223
235
|
if (!isUserMessage(msg)) continue;
|
|
@@ -319,6 +331,34 @@ function parseInputs(historyOrInput, inputs) {
|
|
|
319
331
|
const newMessages = allInputs.map(inputToMessage);
|
|
320
332
|
return { history: [], messages: newMessages };
|
|
321
333
|
}
|
|
334
|
+
function resolveTurnStartIndex(messages, history, newMessages, requestMessageCount, overrideIndex) {
|
|
335
|
+
if (history.length > 0) {
|
|
336
|
+
const historyIds = new Set(history.map((message) => message.id));
|
|
337
|
+
let lastHistoryIndex = -1;
|
|
338
|
+
for (let i = 0; i < messages.length; i += 1) {
|
|
339
|
+
if (historyIds.has(messages[i].id)) {
|
|
340
|
+
lastHistoryIndex = i;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (lastHistoryIndex !== -1) {
|
|
344
|
+
return lastHistoryIndex + 1;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (typeof overrideIndex === "number" && Number.isFinite(overrideIndex)) {
|
|
348
|
+
return Math.min(Math.max(0, overrideIndex), requestMessageCount);
|
|
349
|
+
}
|
|
350
|
+
if (newMessages.length > 0) {
|
|
351
|
+
const newMessageIds = new Set(newMessages.map((message) => message.id));
|
|
352
|
+
const index = messages.findIndex((message) => newMessageIds.has(message.id));
|
|
353
|
+
if (index !== -1) {
|
|
354
|
+
return index;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (history.length === 0) {
|
|
358
|
+
return 0;
|
|
359
|
+
}
|
|
360
|
+
return Math.max(0, requestMessageCount - newMessages.length);
|
|
361
|
+
}
|
|
322
362
|
async function executeTools(assistantMessage, tools, toolStrategy, executions, onEvent, middleware = [], ctx) {
|
|
323
363
|
const toolCalls = assistantMessage.toolCalls ?? [];
|
|
324
364
|
const results = [];
|
|
@@ -463,17 +503,13 @@ async function executeTools(assistantMessage, tools, toolStrategy, executions, o
|
|
|
463
503
|
return results;
|
|
464
504
|
}
|
|
465
505
|
async function executeGenerate(model, config, system, params, tools, toolStrategy, structure, history, newMessages, middleware) {
|
|
466
|
-
validateMediaCapabilities(
|
|
467
|
-
[...history, ...newMessages],
|
|
468
|
-
model.capabilities,
|
|
469
|
-
model.provider.name
|
|
470
|
-
);
|
|
471
506
|
const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
472
|
-
|
|
507
|
+
let allMessages = [...history, ...newMessages];
|
|
473
508
|
const toolExecutions = [];
|
|
474
509
|
const usages = [];
|
|
475
510
|
let cycles = 0;
|
|
476
511
|
let structuredData;
|
|
512
|
+
let turnStartIndex = history.length;
|
|
477
513
|
const initialRequest = {
|
|
478
514
|
messages: allMessages,
|
|
479
515
|
system,
|
|
@@ -492,6 +528,17 @@ async function executeGenerate(model, config, system, params, tools, toolStrateg
|
|
|
492
528
|
try {
|
|
493
529
|
await runHook(middleware, "onStart", ctx);
|
|
494
530
|
await runHook(middleware, "onRequest", ctx);
|
|
531
|
+
allMessages = ctx.request.messages;
|
|
532
|
+
const requestMessageCount = allMessages.length;
|
|
533
|
+
const overrideIndex = ctx.state.get(TURN_START_INDEX_KEY);
|
|
534
|
+
turnStartIndex = resolveTurnStartIndex(
|
|
535
|
+
allMessages,
|
|
536
|
+
history,
|
|
537
|
+
newMessages,
|
|
538
|
+
requestMessageCount,
|
|
539
|
+
typeof overrideIndex === "number" ? overrideIndex : void 0
|
|
540
|
+
);
|
|
541
|
+
validateMediaCapabilities(allMessages, model.capabilities, model.provider.name);
|
|
495
542
|
while (cycles < maxIterations + 1) {
|
|
496
543
|
cycles++;
|
|
497
544
|
const request = {
|
|
@@ -537,7 +584,7 @@ async function executeGenerate(model, config, system, params, tools, toolStrateg
|
|
|
537
584
|
}
|
|
538
585
|
const data = structure ? structuredData : void 0;
|
|
539
586
|
const turn = createTurn(
|
|
540
|
-
allMessages.slice(
|
|
587
|
+
allMessages.slice(turnStartIndex),
|
|
541
588
|
toolExecutions,
|
|
542
589
|
aggregateUsage(usages),
|
|
543
590
|
cycles,
|
|
@@ -551,6 +598,7 @@ async function executeGenerate(model, config, system, params, tools, toolStrateg
|
|
|
551
598
|
};
|
|
552
599
|
ctx.endTime = Date.now();
|
|
553
600
|
await runHook(middleware, "onResponse", ctx, true);
|
|
601
|
+
await runTurnHook(middleware, turn, ctx);
|
|
554
602
|
await runHook(middleware, "onEnd", ctx, true);
|
|
555
603
|
return turn;
|
|
556
604
|
} catch (error) {
|
|
@@ -560,19 +608,15 @@ async function executeGenerate(model, config, system, params, tools, toolStrateg
|
|
|
560
608
|
}
|
|
561
609
|
}
|
|
562
610
|
function executeStream(model, config, system, params, tools, toolStrategy, structure, history, newMessages, middleware) {
|
|
563
|
-
validateMediaCapabilities(
|
|
564
|
-
[...history, ...newMessages],
|
|
565
|
-
model.capabilities,
|
|
566
|
-
model.provider.name
|
|
567
|
-
);
|
|
568
611
|
const abortController = new AbortController();
|
|
569
|
-
|
|
612
|
+
let allMessages = [...history, ...newMessages];
|
|
570
613
|
const toolExecutions = [];
|
|
571
614
|
const usages = [];
|
|
572
615
|
let cycles = 0;
|
|
573
616
|
let generatorError = null;
|
|
574
617
|
let structuredData;
|
|
575
618
|
let generatorCompleted = false;
|
|
619
|
+
let turnStartIndex = history.length;
|
|
576
620
|
const initialRequest = {
|
|
577
621
|
messages: allMessages,
|
|
578
622
|
system,
|
|
@@ -629,6 +673,17 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
629
673
|
ensureNotAborted();
|
|
630
674
|
await runHook(middleware, "onStart", ctx);
|
|
631
675
|
await runHook(middleware, "onRequest", ctx);
|
|
676
|
+
allMessages = ctx.request.messages;
|
|
677
|
+
const requestMessageCount = allMessages.length;
|
|
678
|
+
const overrideIndex = ctx.state.get(TURN_START_INDEX_KEY);
|
|
679
|
+
turnStartIndex = resolveTurnStartIndex(
|
|
680
|
+
allMessages,
|
|
681
|
+
history,
|
|
682
|
+
newMessages,
|
|
683
|
+
requestMessageCount,
|
|
684
|
+
typeof overrideIndex === "number" ? overrideIndex : void 0
|
|
685
|
+
);
|
|
686
|
+
validateMediaCapabilities(allMessages, model.capabilities, model.provider.name);
|
|
632
687
|
while (cycles < maxIterations + 1) {
|
|
633
688
|
cycles++;
|
|
634
689
|
ensureNotAborted();
|
|
@@ -733,7 +788,7 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
733
788
|
}
|
|
734
789
|
const data = structure ? structuredData : void 0;
|
|
735
790
|
const turn = createTurn(
|
|
736
|
-
allMessages.slice(
|
|
791
|
+
allMessages.slice(turnStartIndex),
|
|
737
792
|
toolExecutions,
|
|
738
793
|
aggregateUsage(usages),
|
|
739
794
|
cycles,
|
|
@@ -747,6 +802,7 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
747
802
|
};
|
|
748
803
|
ctx.endTime = Date.now();
|
|
749
804
|
await runHook(middleware, "onResponse", ctx, true);
|
|
805
|
+
await runTurnHook(middleware, turn, ctx);
|
|
750
806
|
await runHook(middleware, "onEnd", ctx, true);
|
|
751
807
|
return turn;
|
|
752
808
|
};
|
|
@@ -1851,259 +1907,6 @@ var Video = class _Video {
|
|
|
1851
1907
|
}
|
|
1852
1908
|
};
|
|
1853
1909
|
|
|
1854
|
-
// src/types/thread.ts
|
|
1855
|
-
var Thread = class _Thread {
|
|
1856
|
-
/** Unique thread identifier */
|
|
1857
|
-
id;
|
|
1858
|
-
/** Internal message storage */
|
|
1859
|
-
_messages;
|
|
1860
|
-
/** Creation timestamp */
|
|
1861
|
-
_createdAt;
|
|
1862
|
-
/** Last update timestamp */
|
|
1863
|
-
_updatedAt;
|
|
1864
|
-
/**
|
|
1865
|
-
* Creates a new thread instance.
|
|
1866
|
-
*
|
|
1867
|
-
* @param messages - Optional initial messages to populate the thread
|
|
1868
|
-
*/
|
|
1869
|
-
constructor(messages) {
|
|
1870
|
-
this.id = generateId();
|
|
1871
|
-
this._messages = messages ? [...messages] : [];
|
|
1872
|
-
this._createdAt = /* @__PURE__ */ new Date();
|
|
1873
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1874
|
-
}
|
|
1875
|
-
/**
|
|
1876
|
-
* All messages in the thread (readonly).
|
|
1877
|
-
*/
|
|
1878
|
-
get messages() {
|
|
1879
|
-
return this._messages;
|
|
1880
|
-
}
|
|
1881
|
-
/**
|
|
1882
|
-
* Number of messages in the thread.
|
|
1883
|
-
*/
|
|
1884
|
-
get length() {
|
|
1885
|
-
return this._messages.length;
|
|
1886
|
-
}
|
|
1887
|
-
/**
|
|
1888
|
-
* Appends all messages from a Turn to the thread.
|
|
1889
|
-
*
|
|
1890
|
-
* @param turn - The Turn containing messages to append
|
|
1891
|
-
* @returns This thread instance for chaining
|
|
1892
|
-
*/
|
|
1893
|
-
append(turn) {
|
|
1894
|
-
this._messages.push(...turn.messages);
|
|
1895
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1896
|
-
return this;
|
|
1897
|
-
}
|
|
1898
|
-
/**
|
|
1899
|
-
* Adds raw messages to the thread.
|
|
1900
|
-
*
|
|
1901
|
-
* @param messages - Messages to add
|
|
1902
|
-
* @returns This thread instance for chaining
|
|
1903
|
-
*/
|
|
1904
|
-
push(...messages) {
|
|
1905
|
-
this._messages.push(...messages);
|
|
1906
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1907
|
-
return this;
|
|
1908
|
-
}
|
|
1909
|
-
/**
|
|
1910
|
-
* Adds a user message to the thread.
|
|
1911
|
-
*
|
|
1912
|
-
* @param content - String or array of content blocks
|
|
1913
|
-
* @returns This thread instance for chaining
|
|
1914
|
-
*
|
|
1915
|
-
* @example
|
|
1916
|
-
* ```typescript
|
|
1917
|
-
* thread.user('Hello, world!');
|
|
1918
|
-
* thread.user([
|
|
1919
|
-
* { type: 'text', text: 'Describe this image:' },
|
|
1920
|
-
* { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
|
|
1921
|
-
* ]);
|
|
1922
|
-
* ```
|
|
1923
|
-
*/
|
|
1924
|
-
user(content) {
|
|
1925
|
-
this._messages.push(new UserMessage(content));
|
|
1926
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1927
|
-
return this;
|
|
1928
|
-
}
|
|
1929
|
-
/**
|
|
1930
|
-
* Adds an assistant message to the thread.
|
|
1931
|
-
*
|
|
1932
|
-
* @param content - String or array of content blocks
|
|
1933
|
-
* @returns This thread instance for chaining
|
|
1934
|
-
*
|
|
1935
|
-
* @example
|
|
1936
|
-
* ```typescript
|
|
1937
|
-
* thread.assistant('I can help with that!');
|
|
1938
|
-
* ```
|
|
1939
|
-
*/
|
|
1940
|
-
assistant(content) {
|
|
1941
|
-
this._messages.push(new AssistantMessage(content));
|
|
1942
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1943
|
-
return this;
|
|
1944
|
-
}
|
|
1945
|
-
/**
|
|
1946
|
-
* Filters messages by type.
|
|
1947
|
-
*
|
|
1948
|
-
* @param type - The message type to filter by
|
|
1949
|
-
* @returns Array of messages matching the type
|
|
1950
|
-
*
|
|
1951
|
-
* @example
|
|
1952
|
-
* ```typescript
|
|
1953
|
-
* const userMessages = thread.filter('user');
|
|
1954
|
-
* const assistantMessages = thread.filter('assistant');
|
|
1955
|
-
* ```
|
|
1956
|
-
*/
|
|
1957
|
-
filter(type) {
|
|
1958
|
-
return this._messages.filter((m) => m.type === type);
|
|
1959
|
-
}
|
|
1960
|
-
/**
|
|
1961
|
-
* Returns the last N messages from the thread.
|
|
1962
|
-
*
|
|
1963
|
-
* @param count - Number of messages to return
|
|
1964
|
-
* @returns Array of the last N messages
|
|
1965
|
-
*
|
|
1966
|
-
* @example
|
|
1967
|
-
* ```typescript
|
|
1968
|
-
* const recent = thread.tail(5);
|
|
1969
|
-
* ```
|
|
1970
|
-
*/
|
|
1971
|
-
tail(count) {
|
|
1972
|
-
return this._messages.slice(-count);
|
|
1973
|
-
}
|
|
1974
|
-
/**
|
|
1975
|
-
* Creates a new thread with a subset of messages.
|
|
1976
|
-
*
|
|
1977
|
-
* @param start - Start index (inclusive)
|
|
1978
|
-
* @param end - End index (exclusive)
|
|
1979
|
-
* @returns New Thread containing the sliced messages
|
|
1980
|
-
*
|
|
1981
|
-
* @example
|
|
1982
|
-
* ```typescript
|
|
1983
|
-
* const subset = thread.slice(0, 10);
|
|
1984
|
-
* ```
|
|
1985
|
-
*/
|
|
1986
|
-
slice(start, end) {
|
|
1987
|
-
return new _Thread(this._messages.slice(start, end));
|
|
1988
|
-
}
|
|
1989
|
-
/**
|
|
1990
|
-
* Removes all messages from the thread.
|
|
1991
|
-
*
|
|
1992
|
-
* @returns This thread instance for chaining
|
|
1993
|
-
*/
|
|
1994
|
-
clear() {
|
|
1995
|
-
this._messages = [];
|
|
1996
|
-
this._updatedAt = /* @__PURE__ */ new Date();
|
|
1997
|
-
return this;
|
|
1998
|
-
}
|
|
1999
|
-
/**
|
|
2000
|
-
* Converts the thread to a plain message array.
|
|
2001
|
-
*
|
|
2002
|
-
* @returns Copy of the internal message array
|
|
2003
|
-
*/
|
|
2004
|
-
toMessages() {
|
|
2005
|
-
return [...this._messages];
|
|
2006
|
-
}
|
|
2007
|
-
/**
|
|
2008
|
-
* Serializes the thread to JSON format.
|
|
2009
|
-
*
|
|
2010
|
-
* @returns JSON-serializable representation of the thread
|
|
2011
|
-
*
|
|
2012
|
-
* @example
|
|
2013
|
-
* ```typescript
|
|
2014
|
-
* const json = thread.toJSON();
|
|
2015
|
-
* localStorage.setItem('thread', JSON.stringify(json));
|
|
2016
|
-
* ```
|
|
2017
|
-
*/
|
|
2018
|
-
toJSON() {
|
|
2019
|
-
return {
|
|
2020
|
-
id: this.id,
|
|
2021
|
-
messages: this._messages.map((m) => this.messageToJSON(m)),
|
|
2022
|
-
createdAt: this._createdAt.toISOString(),
|
|
2023
|
-
updatedAt: this._updatedAt.toISOString()
|
|
2024
|
-
};
|
|
2025
|
-
}
|
|
2026
|
-
/**
|
|
2027
|
-
* Deserializes a thread from JSON format.
|
|
2028
|
-
*
|
|
2029
|
-
* @param json - The JSON representation to deserialize
|
|
2030
|
-
* @returns Reconstructed Thread instance
|
|
2031
|
-
*
|
|
2032
|
-
* @example
|
|
2033
|
-
* ```typescript
|
|
2034
|
-
* const json = JSON.parse(localStorage.getItem('thread'));
|
|
2035
|
-
* const thread = Thread.fromJSON(json);
|
|
2036
|
-
* ```
|
|
2037
|
-
*/
|
|
2038
|
-
static fromJSON(json) {
|
|
2039
|
-
const messages = json.messages.map((m) => _Thread.messageFromJSON(m));
|
|
2040
|
-
const thread = new _Thread(messages);
|
|
2041
|
-
thread.id = json.id;
|
|
2042
|
-
thread._createdAt = new Date(json.createdAt);
|
|
2043
|
-
thread._updatedAt = new Date(json.updatedAt);
|
|
2044
|
-
return thread;
|
|
2045
|
-
}
|
|
2046
|
-
/**
|
|
2047
|
-
* Enables iteration over messages with for...of loops.
|
|
2048
|
-
*
|
|
2049
|
-
* @returns Iterator over the thread's messages
|
|
2050
|
-
*
|
|
2051
|
-
* @example
|
|
2052
|
-
* ```typescript
|
|
2053
|
-
* for (const message of thread) {
|
|
2054
|
-
* console.log(message.text);
|
|
2055
|
-
* }
|
|
2056
|
-
* ```
|
|
2057
|
-
*/
|
|
2058
|
-
[Symbol.iterator]() {
|
|
2059
|
-
return this._messages[Symbol.iterator]();
|
|
2060
|
-
}
|
|
2061
|
-
/**
|
|
2062
|
-
* Converts a message to JSON format.
|
|
2063
|
-
*/
|
|
2064
|
-
messageToJSON(m) {
|
|
2065
|
-
const base = {
|
|
2066
|
-
id: m.id,
|
|
2067
|
-
type: m.type,
|
|
2068
|
-
content: [],
|
|
2069
|
-
metadata: m.metadata,
|
|
2070
|
-
timestamp: m.timestamp.toISOString()
|
|
2071
|
-
};
|
|
2072
|
-
if (m instanceof UserMessage) {
|
|
2073
|
-
base.content = m.content;
|
|
2074
|
-
} else if (m instanceof AssistantMessage) {
|
|
2075
|
-
base.content = m.content;
|
|
2076
|
-
base.toolCalls = m.toolCalls;
|
|
2077
|
-
} else if (m instanceof ToolResultMessage) {
|
|
2078
|
-
base.results = m.results;
|
|
2079
|
-
}
|
|
2080
|
-
return base;
|
|
2081
|
-
}
|
|
2082
|
-
/**
|
|
2083
|
-
* Reconstructs a message from JSON format.
|
|
2084
|
-
*/
|
|
2085
|
-
static messageFromJSON(json) {
|
|
2086
|
-
const options = {
|
|
2087
|
-
id: json.id,
|
|
2088
|
-
metadata: json.metadata
|
|
2089
|
-
};
|
|
2090
|
-
switch (json.type) {
|
|
2091
|
-
case "user":
|
|
2092
|
-
return new UserMessage(json.content, options);
|
|
2093
|
-
case "assistant":
|
|
2094
|
-
return new AssistantMessage(
|
|
2095
|
-
json.content,
|
|
2096
|
-
json.toolCalls,
|
|
2097
|
-
options
|
|
2098
|
-
);
|
|
2099
|
-
case "tool_result":
|
|
2100
|
-
return new ToolResultMessage(json.results ?? [], options);
|
|
2101
|
-
default:
|
|
2102
|
-
throw new Error(`Unknown message type: ${json.type}`);
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
|
-
};
|
|
2106
|
-
|
|
2107
1910
|
// src/types/embedding.ts
|
|
2108
1911
|
var EmbeddingInputType = {
|
|
2109
1912
|
/** Input is a document to be stored/indexed */
|