conversationalist 0.0.5 → 0.0.7

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/dist/index.js CHANGED
@@ -3514,6 +3514,8 @@ function copyContent(content) {
3514
3514
  }
3515
3515
  return content.map(copyMultiModalContent);
3516
3516
  }
3517
+ // src/types.ts
3518
+ var CURRENT_SCHEMA_VERSION = 1;
3517
3519
  // src/schemas.ts
3518
3520
  import { z } from "zod";
3519
3521
  var multiModalContentSchema = z.discriminatedUnion("type", [
@@ -3578,6 +3580,7 @@ var conversationStatusSchema = z.enum([
3578
3580
  "deleted"
3579
3581
  ]);
3580
3582
  var conversationShape = {
3583
+ schemaVersion: z.number().int().min(1),
3581
3584
  id: z.string(),
3582
3585
  title: z.string().optional(),
3583
3586
  status: conversationStatusSchema,
@@ -3588,29 +3591,54 @@ var conversationShape = {
3588
3591
  updatedAt: z.string()
3589
3592
  };
3590
3593
  var conversationSchema = z.object(conversationShape);
3591
- // src/utilities.ts
3592
- var import_gray_matter = __toESM(require_gray_matter(), 1);
3593
- function pairToolCallsWithResults(messages) {
3594
- const pairs = [];
3595
- const resultsMap = new Map;
3596
- for (const msg of messages) {
3597
- if (msg.toolResult) {
3598
- resultsMap.set(msg.toolResult.callId, msg.toolResult);
3599
- }
3594
+ // src/utilities/content.ts
3595
+ function toMultiModalArray(input) {
3596
+ if (typeof input === "string")
3597
+ return [{ type: "text", text: input }];
3598
+ return Array.isArray(input) ? input : [input];
3599
+ }
3600
+ function normalizeContent(content) {
3601
+ if (content === undefined)
3602
+ return;
3603
+ if (typeof content === "string")
3604
+ return content;
3605
+ return Array.isArray(content) ? content : [content];
3606
+ }
3607
+ // src/utilities/deterministic.ts
3608
+ function sortObjectKeys(obj) {
3609
+ if (obj === null || typeof obj !== "object") {
3610
+ return obj;
3600
3611
  }
3601
- for (const msg of messages) {
3602
- if (msg.toolCall) {
3603
- pairs.push({
3604
- call: msg.toolCall,
3605
- result: resultsMap.get(msg.toolCall.id)
3606
- });
3607
- }
3612
+ if (Array.isArray(obj)) {
3613
+ return obj.map(sortObjectKeys);
3608
3614
  }
3609
- return pairs;
3615
+ const sorted = {};
3616
+ const keys = Object.keys(obj).sort();
3617
+ for (const key of keys) {
3618
+ sorted[key] = sortObjectKeys(obj[key]);
3619
+ }
3620
+ return sorted;
3610
3621
  }
3622
+ function sortMessagesByPosition(messages) {
3623
+ return [...messages].sort((a, b) => {
3624
+ if (a.position !== b.position) {
3625
+ return a.position - b.position;
3626
+ }
3627
+ if (a.createdAt !== b.createdAt) {
3628
+ return a.createdAt.localeCompare(b.createdAt);
3629
+ }
3630
+ return a.id.localeCompare(b.id);
3631
+ });
3632
+ }
3633
+ // src/utilities/markdown.ts
3634
+ var import_gray_matter = __toESM(require_gray_matter(), 1);
3635
+
3636
+ // src/utilities/type-helpers.ts
3611
3637
  function toReadonly(value) {
3612
3638
  return value;
3613
3639
  }
3640
+
3641
+ // src/utilities/message.ts
3614
3642
  function createMessage(props) {
3615
3643
  const content = Array.isArray(props.content) ? toReadonly([...props.content]) : props.content;
3616
3644
  const message = {
@@ -3628,18 +3656,6 @@ function createMessage(props) {
3628
3656
  };
3629
3657
  return toReadonly(message);
3630
3658
  }
3631
- function toMultiModalArray(input) {
3632
- if (typeof input === "string")
3633
- return [{ type: "text", text: input }];
3634
- return Array.isArray(input) ? input : [input];
3635
- }
3636
- function normalizeContent(content) {
3637
- if (content === undefined)
3638
- return;
3639
- if (typeof content === "string")
3640
- return content;
3641
- return Array.isArray(content) ? content : [content];
3642
- }
3643
3659
  function messageParts(message) {
3644
3660
  if (typeof message.content === "string") {
3645
3661
  return message.content ? [{ type: "text", text: message.content }] : [];
@@ -3656,7 +3672,9 @@ function messageText(message, joiner = `
3656
3672
  function messageHasImages(message) {
3657
3673
  return messageParts(message).some((p) => p.type === "image");
3658
3674
  }
3659
- var ROLE_DISPLAY_NAMES = {
3675
+
3676
+ // src/utilities/markdown.ts
3677
+ var ROLE_LABELS = {
3660
3678
  user: "User",
3661
3679
  assistant: "Assistant",
3662
3680
  system: "System",
@@ -3665,7 +3683,8 @@ var ROLE_DISPLAY_NAMES = {
3665
3683
  "tool-result": "Tool Result",
3666
3684
  snapshot: "Snapshot"
3667
3685
  };
3668
- var DISPLAY_NAME_TO_ROLE = {
3686
+ var ROLE_DISPLAY_NAMES = ROLE_LABELS;
3687
+ var LABEL_TO_ROLE = {
3669
3688
  User: "user",
3670
3689
  Assistant: "assistant",
3671
3690
  System: "system",
@@ -3674,6 +3693,13 @@ var DISPLAY_NAME_TO_ROLE = {
3674
3693
  "Tool Result": "tool-result",
3675
3694
  Snapshot: "snapshot"
3676
3695
  };
3696
+ var DISPLAY_NAME_TO_ROLE = LABEL_TO_ROLE;
3697
+ function getRoleLabel(role) {
3698
+ return ROLE_LABELS[role];
3699
+ }
3700
+ function getRoleFromLabel(label) {
3701
+ return LABEL_TO_ROLE[label];
3702
+ }
3677
3703
  function formatMessageContent(message) {
3678
3704
  if (typeof message.content === "string")
3679
3705
  return message.content;
@@ -3876,7 +3902,63 @@ function parseMarkdownSimple(body) {
3876
3902
  };
3877
3903
  return toReadonly(conversation);
3878
3904
  }
3879
-
3905
+ // src/utilities/tool-calls.ts
3906
+ function pairToolCallsWithResults(messages) {
3907
+ const pairs = [];
3908
+ const resultsMap = new Map;
3909
+ for (const msg of messages) {
3910
+ if (msg.toolResult) {
3911
+ resultsMap.set(msg.toolResult.callId, msg.toolResult);
3912
+ }
3913
+ }
3914
+ for (const msg of messages) {
3915
+ if (msg.toolCall) {
3916
+ pairs.push({
3917
+ call: msg.toolCall,
3918
+ result: resultsMap.get(msg.toolCall.id)
3919
+ });
3920
+ }
3921
+ }
3922
+ return pairs;
3923
+ }
3924
+ // src/utilities/transient.ts
3925
+ function isTransientKey(key) {
3926
+ return key.startsWith("_");
3927
+ }
3928
+ function stripTransientFromRecord(metadata) {
3929
+ const result = {};
3930
+ for (const [key, value] of Object.entries(metadata)) {
3931
+ if (!isTransientKey(key)) {
3932
+ result[key] = value;
3933
+ }
3934
+ }
3935
+ return result;
3936
+ }
3937
+ function stripTransientMetadata(conversation) {
3938
+ const strippedMessages = conversation.messages.map((message) => toReadonly({
3939
+ id: message.id,
3940
+ role: message.role,
3941
+ content: message.content,
3942
+ position: message.position,
3943
+ createdAt: message.createdAt,
3944
+ metadata: toReadonly(stripTransientFromRecord({ ...message.metadata })),
3945
+ hidden: message.hidden,
3946
+ toolCall: message.toolCall,
3947
+ toolResult: message.toolResult,
3948
+ tokenUsage: message.tokenUsage,
3949
+ goalCompleted: message.goalCompleted
3950
+ }));
3951
+ return toReadonly({
3952
+ id: conversation.id,
3953
+ title: conversation.title,
3954
+ status: conversation.status,
3955
+ metadata: toReadonly(stripTransientFromRecord({ ...conversation.metadata })),
3956
+ tags: conversation.tags,
3957
+ messages: strippedMessages,
3958
+ createdAt: conversation.createdAt,
3959
+ updatedAt: conversation.updatedAt
3960
+ });
3961
+ }
3880
3962
  // src/environment.ts
3881
3963
  function simpleTokenEstimator(message) {
3882
3964
  const text = messageText(message);
@@ -4230,33 +4312,88 @@ function redactMessageAtPosition(conversation, position, placeholder = "[REDACTE
4230
4312
  const next = { ...conversation, messages, updatedAt: now };
4231
4313
  return toReadonly(next);
4232
4314
  }
4233
- function serializeConversation(conversation) {
4234
- return {
4235
- id: conversation.id,
4236
- title: conversation.title,
4237
- status: conversation.status,
4238
- metadata: { ...conversation.metadata },
4239
- tags: [...conversation.tags],
4240
- messages: conversation.messages.map((m) => ({
4315
+ var REDACTED = "[REDACTED]";
4316
+ function migrateConversationJSON(json) {
4317
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
4318
+ return {
4319
+ schemaVersion: CURRENT_SCHEMA_VERSION,
4320
+ id: "",
4321
+ status: "active",
4322
+ metadata: {},
4323
+ tags: [],
4324
+ messages: [],
4325
+ createdAt: new Date().toISOString(),
4326
+ updatedAt: new Date().toISOString()
4327
+ };
4328
+ }
4329
+ const data = json;
4330
+ if (!("schemaVersion" in json)) {
4331
+ return {
4332
+ ...data,
4333
+ schemaVersion: CURRENT_SCHEMA_VERSION
4334
+ };
4335
+ }
4336
+ return data;
4337
+ }
4338
+ function serializeConversation(conversation, options2 = {}) {
4339
+ const {
4340
+ deterministic = false,
4341
+ stripTransient = false,
4342
+ redactToolArguments = false,
4343
+ redactToolResults = false
4344
+ } = options2;
4345
+ let metadata = { ...conversation.metadata };
4346
+ if (stripTransient) {
4347
+ metadata = stripTransientFromRecord(metadata);
4348
+ }
4349
+ let messages = conversation.messages.map((m) => {
4350
+ let msgMetadata = { ...m.metadata };
4351
+ if (stripTransient) {
4352
+ msgMetadata = stripTransientFromRecord(msgMetadata);
4353
+ }
4354
+ return {
4241
4355
  id: m.id,
4242
4356
  role: m.role,
4243
4357
  content: copyContent(m.content),
4244
4358
  position: m.position,
4245
4359
  createdAt: m.createdAt,
4246
- metadata: { ...m.metadata },
4360
+ metadata: msgMetadata,
4247
4361
  hidden: m.hidden,
4248
- toolCall: m.toolCall ? { ...m.toolCall } : undefined,
4249
- toolResult: m.toolResult ? { ...m.toolResult } : undefined,
4362
+ toolCall: m.toolCall ? {
4363
+ ...m.toolCall,
4364
+ arguments: redactToolArguments ? REDACTED : m.toolCall.arguments
4365
+ } : undefined,
4366
+ toolResult: m.toolResult ? {
4367
+ ...m.toolResult,
4368
+ content: redactToolResults ? REDACTED : m.toolResult.content
4369
+ } : undefined,
4250
4370
  tokenUsage: m.tokenUsage ? { ...m.tokenUsage } : undefined,
4251
4371
  goalCompleted: m.goalCompleted
4252
- })),
4372
+ };
4373
+ });
4374
+ if (deterministic) {
4375
+ messages = sortMessagesByPosition(messages);
4376
+ }
4377
+ let result = {
4378
+ schemaVersion: CURRENT_SCHEMA_VERSION,
4379
+ id: conversation.id,
4380
+ title: conversation.title,
4381
+ status: conversation.status,
4382
+ metadata,
4383
+ tags: [...conversation.tags],
4384
+ messages,
4253
4385
  createdAt: conversation.createdAt,
4254
4386
  updatedAt: conversation.updatedAt
4255
4387
  };
4388
+ if (deterministic) {
4389
+ result = sortObjectKeys(result);
4390
+ }
4391
+ return result;
4256
4392
  }
4257
4393
  function deserializeConversation(json) {
4394
+ const migrated = migrateConversationJSON(json);
4258
4395
  try {
4259
- json.messages.reduce((state, message, index) => {
4396
+ migrated.messages.reduce((state, message, index) => {
4260
4397
  if (message.position !== index) {
4261
4398
  throw createInvalidPositionError(index, message.position);
4262
4399
  }
@@ -4270,16 +4407,16 @@ function deserializeConversation(json) {
4270
4407
  }
4271
4408
  return state;
4272
4409
  }, { toolUses: new Map });
4273
- const messages = json.messages.map((m) => createMessage(m));
4410
+ const messages = migrated.messages.map((m) => createMessage(m));
4274
4411
  const conv = {
4275
- id: json.id,
4276
- title: json.title,
4277
- status: json.status,
4278
- metadata: { ...json.metadata },
4279
- tags: [...json.tags],
4412
+ id: migrated.id,
4413
+ title: migrated.title,
4414
+ status: migrated.status,
4415
+ metadata: { ...migrated.metadata },
4416
+ tags: [...migrated.tags],
4280
4417
  messages,
4281
- createdAt: json.createdAt,
4282
- updatedAt: json.updatedAt
4418
+ createdAt: migrated.createdAt,
4419
+ updatedAt: migrated.updatedAt
4283
4420
  };
4284
4421
  return toReadonly(conv);
4285
4422
  } catch (error) {
@@ -4943,6 +5080,10 @@ export {
4943
5080
  toMultiModalArray,
4944
5081
  toMarkdown,
4945
5082
  toChatMessages,
5083
+ stripTransientMetadata,
5084
+ stripTransientFromRecord,
5085
+ sortObjectKeys,
5086
+ sortMessagesByPosition,
4946
5087
  simpleTokenEstimator,
4947
5088
  serializeConversation,
4948
5089
  searchConversationMessages,
@@ -4953,13 +5094,17 @@ export {
4953
5094
  pairToolCallsWithResults,
4954
5095
  normalizeContent,
4955
5096
  multiModalContentSchema,
5097
+ migrateConversationJSON,
4956
5098
  messageRoleSchema,
4957
5099
  messageJSONSchema,
4958
5100
  messageInputSchema,
5101
+ isTransientKey,
4959
5102
  isStreamingMessage,
4960
5103
  hasSystemMessage,
4961
5104
  getSystemMessages,
4962
5105
  getStreamingMessage,
5106
+ getRoleLabel,
5107
+ getRoleFromLabel,
4963
5108
  getRecentMessages,
4964
5109
  getMessageByIdentifier,
4965
5110
  getMessageAtPosition,
@@ -4992,9 +5137,12 @@ export {
4992
5137
  appendStreamingMessage,
4993
5138
  appendMessages,
4994
5139
  appendAssistantMessage,
5140
+ ROLE_LABELS,
4995
5141
  MarkdownParseError,
5142
+ LABEL_TO_ROLE,
4996
5143
  ConversationalistError,
4997
- ConversationHistory
5144
+ ConversationHistory,
5145
+ CURRENT_SCHEMA_VERSION
4998
5146
  };
4999
5147
 
5000
- //# debugId=57E19802F1DD380C64756E2164756E21
5148
+ //# debugId=5DC3B590F98C078D64756E2164756E21