liminal 0.12.2 → 0.14.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/L.ts +1 -1
  3. package/Strand.ts +1 -1
  4. package/assistant.ts +5 -1
  5. package/assistantSchema.ts +60 -0
  6. package/disable.ts +5 -1
  7. package/dist/L.d.ts +1 -1
  8. package/dist/L.js +1 -1
  9. package/dist/Strand.d.ts +1 -1
  10. package/dist/assistant.js +5 -1
  11. package/dist/assistant.js.map +1 -1
  12. package/dist/{assistantStruct.d.ts → assistantSchema.d.ts} +3 -2
  13. package/dist/assistantSchema.js +44 -0
  14. package/dist/assistantSchema.js.map +1 -0
  15. package/dist/disable.d.ts +2 -1
  16. package/dist/disable.js +4 -0
  17. package/dist/disable.js.map +1 -1
  18. package/dist/enable.d.ts +2 -2
  19. package/dist/enable.js +4 -0
  20. package/dist/enable.js.map +1 -1
  21. package/dist/handle.d.ts +1 -1
  22. package/dist/handle.js +1 -1
  23. package/dist/patterns/index.d.ts +1 -0
  24. package/dist/patterns/index.js +2 -0
  25. package/dist/patterns/index.js.map +1 -0
  26. package/dist/patterns/matchGist.d.ts +3 -0
  27. package/dist/patterns/matchGist.js +16 -0
  28. package/dist/patterns/matchGist.js.map +1 -0
  29. package/dist/tsconfig.tsbuildinfo +1 -1
  30. package/dist/userJson.js +4 -1
  31. package/dist/userJson.js.map +1 -1
  32. package/dist/util/JsonValue.d.ts +2 -1
  33. package/dist/util/JsonValue.js +73 -46
  34. package/dist/util/JsonValue.js.map +1 -1
  35. package/dist/util/Sequence.d.ts +2 -2
  36. package/dist/util/Taggable.d.ts +3 -3
  37. package/dist/util/Taggable.js +13 -16
  38. package/dist/util/Taggable.js.map +1 -1
  39. package/dist/util/fixRaw.d.ts +4 -0
  40. package/dist/util/fixRaw.js +75 -0
  41. package/dist/util/fixRaw.js.map +1 -0
  42. package/enable.ts +6 -3
  43. package/handle.ts +1 -1
  44. package/package.json +18 -17
  45. package/patterns/index.ts +1 -0
  46. package/patterns/matchGist.ts +23 -0
  47. package/userJson.ts +4 -2
  48. package/util/JsonValue.ts +101 -54
  49. package/util/Sequence.ts +8 -6
  50. package/util/Taggable.ts +21 -28
  51. package/util/fixRaw.ts +93 -0
  52. package/assistantStruct.ts +0 -34
  53. package/dist/assistantStruct.js +0 -22
  54. package/dist/assistantStruct.js.map +0 -1
package/util/JsonValue.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import * as Effect from "effect/Effect"
2
+ import { pipe } from "effect/Function"
3
+ import * as Option from "effect/Option"
1
4
  import * as Schema from "effect/Schema"
2
5
  import * as SchemaAST from "effect/SchemaAST"
3
6
 
@@ -7,10 +10,10 @@ export type JsonValueArray = Array<JsonValue> | ReadonlyArray<JsonValue>
7
10
 
8
11
  export type JsonValueObject = { [key: string]: JsonValue }
9
12
 
10
- export const encodeJsonc: <A, I extends JsonValue>(schema: Schema.Schema<A, I>) => (value: A) => string = (
11
- schema,
12
- ) => {
13
- const encoder = encodeAstJsonc(SchemaAST.encodedAST(schema.ast))
13
+ export const encodeJsonc: <A, I extends JsonValue>(
14
+ schema: Schema.Schema<A, I>,
15
+ ) => (value: A) => Effect.Effect<string> = (schema) => {
16
+ const encoder = encodeAst(SchemaAST.encodedBoundAST(schema.ast))
14
17
  return (value) => encoder(value, new EncodeJsoncContext(0))
15
18
  }
16
19
 
@@ -26,72 +29,116 @@ class EncodeJsoncContext {
26
29
 
27
30
  next = () => new EncodeJsoncContext(this.depth + 1)
28
31
 
29
- comment = (type: SchemaAST.AST): string => {
30
- const description = extractAnnotation(type)?.trim()
31
- if (description) {
32
- return `// ${description}\n${this.childIndentation}`
33
- }
34
- return ""
35
- }
32
+ comment = (type: SchemaAST.AST): string =>
33
+ Option.match(SchemaAST.getDescriptionAnnotation(type), {
34
+ onSome: (v) => {
35
+ v = v.trim()
36
+ if (v) {
37
+ return `// ${v}\n${this.childIndentation}`
38
+ }
39
+ return ""
40
+ },
41
+ onNone: () => "",
42
+ })
36
43
  }
37
44
 
38
- const encodeAstJsonc: (ast: SchemaAST.AST) => (value: unknown, ctx: EncodeJsoncContext) => string = (ast) => {
45
+ const encodeAst: (
46
+ ast: SchemaAST.AST,
47
+ refinement?: SchemaAST.Refinement,
48
+ ) => (value: unknown, ctx: EncodeJsoncContext) => Effect.Effect<string> = (ast, refinement) => {
39
49
  switch (ast._tag) {
40
50
  case "TypeLiteral": {
41
- return (value, ctx) => {
42
- const props = ast.propertySignatures
43
- .map(({ name, type }) => {
44
- if (typeof name === "symbol") throw 0
45
-
46
- const child = encodeAstJsonc(type)((value as never)[name], ctx.next())
47
- return `${ctx.comment(type)}${name}: ${child}`
48
- })
49
- .join(`,\n${ctx.childIndentation}`)
51
+ return Effect.fnUntraced(function*(value, ctx) {
52
+ const props = yield* Effect.all(
53
+ ast.propertySignatures.map(
54
+ Effect.fnUntraced(function*({ name, type }) {
55
+ if (typeof name === "symbol") throw 0
56
+ const child = yield* encodeAst(type)((value as never)[name], ctx.next())
57
+ return `${ctx.comment(refinement ?? type)}${name}: ${child}`
58
+ }),
59
+ ),
60
+ ).pipe(
61
+ Effect.map((v) => v.join(`,\n${ctx.childIndentation}`)),
62
+ )
50
63
  return `{\n${ctx.childIndentation}${props}\n${ctx.indentation}}`
51
- }
64
+ })
52
65
  }
53
66
  case "StringKeyword": {
54
- return (value) => `"${value}"`
67
+ return (value) => Effect.succeed(`"${value}"`)
55
68
  }
69
+ case "BooleanKeyword":
56
70
  case "NumberKeyword": {
57
- return (value) => `${value}`
71
+ return (value) => Effect.succeed(String(value))
72
+ }
73
+ case "Refinement": {
74
+ return encodeAst(ast.from, refinement ?? ast)
75
+ }
76
+ case "UnknownKeyword":
77
+ case "AnyKeyword": {
78
+ return (value, ctx) =>
79
+ Effect.succeed(
80
+ JSON
81
+ .stringify(value, null, 2)
82
+ .split("\n")
83
+ .map((line, i) => i ? ctx.indentation.concat(line) : line)
84
+ .join("\n"),
85
+ )
86
+ }
87
+ case "Union": {
88
+ const { types } = ast
89
+ const guards = types.map((ast) =>
90
+ pipe(
91
+ ast,
92
+ Schema.make,
93
+ Schema.is,
94
+ )
95
+ )
96
+ return (value, ctx) => {
97
+ for (let i = 0; i < types.length; i++) {
98
+ const guard = guards[i]!
99
+ if (guard(value)) {
100
+ return encodeAst(types[i]!)(value, ctx)
101
+ }
102
+ }
103
+ throw 0
104
+ }
58
105
  }
59
106
  case "Literal": {
60
107
  const { literal } = ast
61
- if (typeof literal === "string") {
62
- return () => `"${literal}"`
63
- }
64
- if (typeof literal === "number" || typeof literal === "boolean") {
65
- return () => String(literal)
66
- }
67
- if (literal === null) {
68
- return () => "null"
69
- }
70
- throw 0
108
+ const v = (() => {
109
+ switch (typeof literal) {
110
+ case "boolean":
111
+ case "number": {
112
+ return String(literal)
113
+ }
114
+ case "string": {
115
+ return `"${literal}"`
116
+ }
117
+ }
118
+ if (literal === null) {
119
+ return "null"
120
+ }
121
+ console.log({ literal })
122
+ throw 0
123
+ })()
124
+ return () => Effect.succeed(v)
71
125
  }
72
126
  case "TupleType": {
73
127
  const { elements, rest } = ast
74
- return (value, ctx) => {
75
- const e = (elements.length ? elements : rest).map((element, i) => {
76
- const child = encodeAstJsonc(element.type)((value as never)[i]!, ctx.next())
77
- return `${ctx.comment(element.type)}${child}`
78
- }).join(`,\n${ctx.childIndentation}`)
79
- return `[\n${ctx.childIndentation}${e}\n${ctx.indentation}]`
80
- }
128
+ return (value, ctx) =>
129
+ Effect.all((elements.length ? elements : rest).map(
130
+ Effect.fnUntraced(function*(element, i) {
131
+ const child = yield* encodeAst(element.type)((value as never)[i]!, ctx.next())
132
+ return `${ctx.comment(refinement ?? element.type)}${child}`
133
+ }),
134
+ )).pipe(
135
+ Effect.map((v) => `[\n${ctx.childIndentation}${v.join(`,\n${ctx.childIndentation}`)}\n${ctx.indentation}]`),
136
+ )
81
137
  }
82
- }
83
- console.log(ast._tag)
84
- throw 0
85
- }
86
-
87
- const extractAnnotation = ({ annotations }: SchemaAST.AST): string | undefined => {
88
- if (SchemaAST.JSONSchemaAnnotationId in annotations) {
89
- const jsonSchemaAnnotations = annotations[SchemaAST.JSONSchemaAnnotationId] as
90
- | SchemaAST.Annotations
91
- | undefined
92
- if (jsonSchemaAnnotations) {
93
- return jsonSchemaAnnotations.description as string
138
+ case "Suspend": {
139
+ return encodeAst(ast.f())
94
140
  }
95
141
  }
96
- return
142
+ console.log({ ast })
143
+ throw 0
97
144
  }
package/util/Sequence.ts CHANGED
@@ -1,9 +1,11 @@
1
- import type { Effect } from "effect/Effect"
1
+ import type * as Effect from "effect/Effect"
2
2
 
3
- export type Sequence<I = never, O = never> = <Arg extends Array<Effect<any, any, any>>>(
3
+ export type Sequence<I = never, O = never> = <Arg extends Array<Effect.All.EffectAny>>(
4
4
  ...steps: Arg
5
- ) => Effect<
6
- Arg extends [] ? void : Arg extends [...infer _0, infer L extends Effect<any, any, any>] ? Effect.Success<L> : never,
7
- Effect.Error<Arg[number]>,
8
- Exclude<Effect.Context<Arg[number]>, I> | O
5
+ ) => Effect.Effect<
6
+ Arg extends [] ? void
7
+ : Arg extends [...infer _0, infer L extends Effect.All.EffectAny] ? Effect.Effect.Success<L>
8
+ : never,
9
+ Effect.Effect.Error<Arg[number]>,
10
+ Exclude<Effect.Effect.Context<Arg[number]>, I> | O
9
11
  >
package/util/Taggable.ts CHANGED
@@ -1,12 +1,8 @@
1
1
  import * as Effect from "effect/Effect"
2
+ import { normalizeRaw } from "./fixRaw.ts"
2
3
 
3
- /** Append a user message to the conversation. */
4
4
  export type Taggable<A, E, R> = <L extends Array<unknown>, E1 = never, R1 = never>(
5
- template?:
6
- | TemplateStringsArray
7
- | string
8
- | Effect.Effect<string | undefined, E1, R1>
9
- | undefined,
5
+ template?: TaggableArg0 | Effect.Effect<string | undefined, E1, R1>,
10
6
  ...substitutions: L
11
7
  ) => Effect.Effect<
12
8
  A,
@@ -14,34 +10,31 @@ export type Taggable<A, E, R> = <L extends Array<unknown>, E1 = never, R1 = neve
14
10
  ([ExtractEffect<L>] extends [never] ? never : Effect.Effect.Context<ExtractEffect<L>>) | R | R1
15
11
  >
16
12
 
13
+ export type TaggableArg0 = TemplateStringsArray | string | undefined
14
+
17
15
  type ExtractEffect<T extends Array<unknown>> = Extract<T[number], Effect.All.EffectAny>
18
16
 
19
17
  export const normalize: <
20
- A0 extends
21
- | TemplateStringsArray
22
- | string
23
- | Effect.Effect<string | undefined, any, any>
24
- | undefined,
18
+ A0 extends TaggableArg0 | Effect.Effect<string | undefined, any, any>,
25
19
  L extends Array<unknown>,
26
20
  >(
27
21
  a0: A0,
28
- ...aRest: L
29
- ) => Effect.Effect<string | (undefined extends A0 ? undefined : never)> = Effect.fnUntraced(function*(a0, ...aRest) {
30
- let a0_: TemplateStringsArray | string | undefined
31
- if (Effect.isEffect(a0)) {
32
- a0_ = yield* a0 as Effect.Effect<TemplateStringsArray | string | undefined>
33
- } else {
34
- a0_ = a0
35
- }
22
+ aRest: L,
23
+ ) => Effect.Effect<string | (undefined extends A0 ? undefined : never)> = Effect.fnUntraced(function*(a0, aRest) {
24
+ const a0_: TaggableArg0 = Effect.isEffect(a0)
25
+ ? yield* a0 as Effect.Effect<TaggableArg0>
26
+ : a0
36
27
  if (!a0_) return undefined as never
37
28
  const aRest_ = yield* Effect.all(
38
- aRest.map((v) => Effect.isEffect(v) ? v : Effect.succeed(v)),
39
- ) as never as Effect.Effect<Array<unknown>>
40
- if (typeof a0_ === "string") {
41
- if (aRest_.length === 0) {
42
- return a0_
43
- }
44
- return [a0_, ...aRest_].join("")
45
- }
46
- return String.raw(a0_, ...aRest)
29
+ aRest.map((v) =>
30
+ Effect.isEffect(v)
31
+ ? v
32
+ : Effect.succeed(v)
33
+ ),
34
+ ) as Effect.Effect<Array<unknown>>
35
+ return typeof a0_ === "string"
36
+ ? aRest_.length === 0
37
+ ? a0_
38
+ : [a0_, ...aRest_].join("")
39
+ : normalizeRaw(a0_, aRest_)
47
40
  })
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 normalizeRaw = (strings: TemplateStringsArray, values: Array<unknown>): string => {
7
+ const fixedStrings = normalizeTemplateStrings(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 normalizeTemplateStrings = (template: TemplateStringsArray): {
58
+ readonly raw: ReadonlyArray<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"}