liminal 0.12.2 → 0.13.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.
package/util/fixRaw.ts ADDED
@@ -0,0 +1,93 @@
1
+ const LEADING_SPACE_RE = /^([ \t]*)/
2
+ const INDENTATION_RE = /^\n([ \t]+)/
3
+ const ESCAPE_SEQ_RE = /\\([`${\\]|\n)/g
4
+ const LAST_INDENTATION_RE = /\n[ \t]+$/
5
+
6
+ export const fixRaw = (strings: TemplateStringsArray, values: Array<unknown>): string => {
7
+ const fixedStrings = fixTemplateStrings(strings)
8
+ const rawArr = fixedStrings.raw
9
+ const valuesLength = values.length
10
+
11
+ const resultParts = new Array(Math.max(1, rawArr.length * 2 - 1))
12
+ let resultIndex = 0
13
+
14
+ for (let i = 0; i < rawArr.length; i++) {
15
+ const str = rawArr[i] || ""
16
+ resultParts[resultIndex++] = str
17
+
18
+ // Only process values for non-final segments.
19
+ if (i < valuesLength) {
20
+ const value = String(values[i])
21
+
22
+ // If value has line breaks, we should indent it.
23
+ if (value.includes("\n")) {
24
+ const lastNewlineIndex = str.lastIndexOf("\n")
25
+
26
+ if (lastNewlineIndex !== -1) {
27
+ // Extract the indentation after the last newline.
28
+ const textAfterLastNewline = str.substring(lastNewlineIndex + 1)
29
+ const leadingSpaceMatch = LEADING_SPACE_RE.exec(textAfterLastNewline)
30
+ const indentationToApply = leadingSpaceMatch && leadingSpaceMatch[1] ? leadingSpaceMatch[1] : ""
31
+
32
+ if (indentationToApply) {
33
+ // Split the value into lines once.
34
+ const lines = value.split("\n")
35
+ const linesLength = lines.length
36
+
37
+ // First line doesn't need indentation.
38
+ resultParts[resultIndex++] = lines[0]
39
+
40
+ // Apply indentation to subsequent lines.
41
+ for (let j = 1; j < linesLength; j++) {
42
+ resultParts[resultIndex++] = "\n" + indentationToApply + lines[j]
43
+ }
44
+ continue
45
+ }
46
+ }
47
+ }
48
+ // For simple values or when no indentation is needed.
49
+ resultParts[resultIndex++] = value
50
+ }
51
+ }
52
+
53
+ // Combine all parts at once
54
+ return resultParts.slice(0, resultIndex).join("")
55
+ }
56
+
57
+ export const fixTemplateStrings = (template: TemplateStringsArray): {
58
+ readonly raw: readonly string[]
59
+ } => {
60
+ const leadingIndentMatch = INDENTATION_RE.exec(template.raw[0]!)
61
+ const indentation = leadingIndentMatch?.[1]
62
+
63
+ const rawLength = template.raw.length
64
+ const raw = new Array(rawLength)
65
+
66
+ for (let i = 0; i < rawLength; i++) {
67
+ let str = template.raw[i]!
68
+ // Only perform common indentation replacements if needed.
69
+ if (indentation) {
70
+ // Remove leading newline and indentation in the first segment.
71
+ if (i === 0) {
72
+ str = str.slice(indentation.length + 1)
73
+ }
74
+ // Use a simple string replacement with a regular expression for better performance.
75
+ str = str.replaceAll(`\n${indentation}`, "\n")
76
+ }
77
+
78
+ // Replace common escape sequences in a single pass.
79
+ str = str.replace(ESCAPE_SEQ_RE, (_match, char) => {
80
+ // Keep the escaped newline replacement separate for clarity.
81
+ return char === "\n" ? "" : char
82
+ })
83
+
84
+ // Handle trailing spaces only for the last segment.
85
+ if (indentation && i === rawLength - 1) {
86
+ str = str.replace(LAST_INDENTATION_RE, "")
87
+ }
88
+
89
+ raw[i] = str
90
+ }
91
+
92
+ return { raw }
93
+ }
@@ -1,34 +0,0 @@
1
- import type { AiError } from "@effect/ai/AiError"
2
- import { AssistantMessage, TextPart } from "@effect/ai/AiInput"
3
- import { AiLanguageModel } from "@effect/ai/AiLanguageModel"
4
- import * as Effect from "effect/Effect"
5
- import * as Option from "effect/Option"
6
- import * as Schema from "effect/Schema"
7
- import { append } from "./append.ts"
8
- import { Strand } from "./Strand.ts"
9
-
10
- /** Infer a structured assistant message and append its JSON representation to the conversation. */
11
- export const assistantStruct: {
12
- <F extends Record<string, Schema.Schema.AnyNoContext>>(
13
- fields: F,
14
- ): Effect.Effect<{ [K in keyof F]: Schema.Schema.Type<F[K]> }, AiError, AiLanguageModel | Strand>
15
- <O, I extends Record<string, unknown>>(
16
- schema: Schema.Schema<O, I, never>,
17
- ): Effect.Effect<O, AiError, AiLanguageModel | Strand>
18
- } = Effect.fnUntraced(
19
- function*(schema) {
20
- const model = yield* AiLanguageModel
21
- const { system, messages } = yield* Strand
22
- const { value, text } = yield* model.generateObject({
23
- system: Option.getOrUndefined(system),
24
- schema: Schema.isSchema(schema) ? schema as Schema.Schema.AnyNoContext : Schema.Struct(schema),
25
- prompt: messages,
26
- })
27
- yield* append(
28
- new AssistantMessage({
29
- parts: [new TextPart({ text })],
30
- }),
31
- )
32
- return value
33
- },
34
- )
@@ -1,22 +0,0 @@
1
- import { AssistantMessage, TextPart } from "@effect/ai/AiInput";
2
- import { AiLanguageModel } from "@effect/ai/AiLanguageModel";
3
- import * as Effect from "effect/Effect";
4
- import * as Option from "effect/Option";
5
- import * as Schema from "effect/Schema";
6
- import { append } from "./append.js";
7
- import { Strand } from "./Strand.js";
8
- /** Infer a structured assistant message and append its JSON representation to the conversation. */
9
- export const assistantStruct = Effect.fnUntraced(function* (schema) {
10
- const model = yield* AiLanguageModel;
11
- const { system, messages } = yield* Strand;
12
- const { value, text } = yield* model.generateObject({
13
- system: Option.getOrUndefined(system),
14
- schema: Schema.isSchema(schema) ? schema : Schema.Struct(schema),
15
- prompt: messages,
16
- });
17
- yield* append(new AssistantMessage({
18
- parts: [new TextPart({ text })],
19
- }));
20
- return value;
21
- });
22
- //# sourceMappingURL=assistantStruct.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"assistantStruct.js","sourceRoot":"","sources":["../assistantStruct.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,mGAAmG;AACnG,MAAM,CAAC,MAAM,eAAe,GAOxB,MAAM,CAAC,UAAU,CACnB,QAAQ,CAAC,EAAC,MAAM;IACd,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,eAAe,CAAA;IACpC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,CAAC,MAAM,CAAA;IAC1C,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,cAAc,CAAC;QAClD,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC;QACrC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAoC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;QAC9F,MAAM,EAAE,QAAQ;KACjB,CAAC,CAAA;IACF,KAAK,CAAC,CAAC,MAAM,CACX,IAAI,gBAAgB,CAAC;QACnB,KAAK,EAAE,CAAC,IAAI,QAAQ,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;KAChC,CAAC,CACH,CAAA;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CACF,CAAA"}