Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Re-implement basic prompts with Tracery #1090

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion xcode/Subconscious/Shared/Components/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3189,7 +3189,10 @@ struct AppEnvironment {
var gatewayProvisioningService: GatewayProvisioningService

var pasteboard = UIPasteboard.general


/// Service for generating creative prompts and oblique strategies
var prompt = PromptService.default

/// Create a long polling publisher that never completes
static func poll(every interval: Double) -> AnyPublisher<Date, Never> {
Timer.publish(
Expand Down
4 changes: 2 additions & 2 deletions xcode/Subconscious/Shared/Components/Deck/DeckView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,8 @@ struct DeckModel: ModelProtocol {
context: nil
)

let prompt = Prompt.connect.randomElement()!.message
let prompt = await environment.prompt.generate(start: "#connect#")

return CardModel(
card: .prompt(
message: prompt,
Expand Down
6 changes: 0 additions & 6 deletions xcode/Subconscious/Shared/Library/Mapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

import Foundation

extension Substring {
func toString() -> String {
String(self)
}
}

extension Data {
func toString(encoding: String.Encoding = .utf8) -> String? {
String(data: self, encoding: encoding)
Expand Down
12 changes: 12 additions & 0 deletions xcode/Subconscious/Shared/Library/StringUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

import Foundation

extension Substring {
func toString() -> String {
String(self)
}
}

extension String {
func toSubstring() -> Substring {
self[...]
}
}

extension String {
/// Remove leading `prefix` from string if it exists.
/// - Returns: new string without prefix
Expand Down
81 changes: 81 additions & 0 deletions xcode/Subconscious/Shared/Library/Tracery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// MicroTracery.swift
// SubconsciousExperiments
//
// Created by Gordon Brander on 1/29/24.
//

import Foundation

public typealias Grammar = Dictionary<String, Array<String>>

/// Create a Tracery with an optional registry of modifiers
public struct Tracery {
private static let token = /#(\w+)(\.(\w+))?#/

private var modifiers: Dictionary<String, (String) -> String>
private let maxDepth = 500

/// Create a Tracery parser
/// - Parameters
/// - modifiers: a dictionary of named functions that can be used to
/// post-process Tracery tokens.
public init(
modifiers: Dictionary<String, (String) -> String> = Dictionary()
) {
self.modifiers = modifiers
}

/// Recursively flatten a grammar. Internal.
/// Call by public method `flatten(grammar:start:)`.
/// - Parameters
/// - depth: keeps track of recursion depth so we can exit if exceeding
/// - grammar: the Tracery grammar
/// - start: the start string
/// - Returns: the flattened string
private func flatten(
depth: Int,
grammar: Grammar,
start: String = "#start#"
) -> String {
// Stop flattening at depth > max
guard depth <= maxDepth else {
return start
}
return start.replacing(Self.token, with: { match in
let key = String(match.1)
guard let value = grammar[key]?.randomElement() else {
return match.0
}
guard
let modifierKey = match.3?.toString(),
let postprocess = modifiers[modifierKey]
else {
return flatten(
depth: depth + 1,
grammar: grammar,
start: value
).toSubstring()
}
return postprocess(
flatten(
depth: depth + 1,
grammar: grammar,
start: value
)
).toSubstring()
})
}

/// Flatten the start string using the given Tracery grammar.
/// - Parameters
/// - grammar: the Tracery grammar
/// - start: the start string
/// - Returns: the flattened string
public func flatten(
grammar: Grammar,
start: String = "#start#"
) -> String {
flatten(depth: 0, grammar: grammar, start: start)
}
}
113 changes: 0 additions & 113 deletions xcode/Subconscious/Shared/Models/Prompt.swift

This file was deleted.

Loading
Loading