Action

Claude: Converse

Posted by agiletortoise, Last update 9 days ago

This action allows you to conduct an ongoing conversation with Anthropic’s Claude AI in a single draft. When you run the action, any lines with no prefix are treated as your input prompts, text starting with > (Markdown quotes) will be treated as previous responses from the AI…and any new responses will be appended at the end of the document, leaving you ready to type your next prompt in the conversation.

An example draft might look like:

What are the three best examples of film noir?

> 1. Double Indemnity (1944) - Directed by Billy Wilder, this film is considered a quintessential example of film noir with its complex characters, moral ambiguity, and cynical view of society.
> 
> 2. The Maltese Falcon (1941) - Directed by John Huston and starring Humphrey Bogart, this classic film noir features a hard-boiled detective, a femme fatale, and a mystery surrounding a valuable object.
> 
> 3. Touch of Evil (1958) - Directed by Orson Welles, this film noir is known for its innovative cinematography, atmospheric setting, and morally ambiguous characters. It is considered one of the last great films of the classic film noir era.

Tell me more about the first one.

> "Double Indemnity" is a 1944 film directed by Billy Wilder and based on a novel by James M. Cain. The film follows a smitten insurance agent, Walter Neff (played by Fred MacMurray), who becomes involved in a plot to kill an unhappy housewife's husband in order to collect on a double indemnity insurance policy. 
> 
> Barbara Stanwyck plays the seductive Phyllis Dietrichson, the femme fatale who manipulates Neff into carrying out the murder. As the plan unravels, tension builds as the two characters try to outwit each other and avoid detection by a suspicious claims investigator, Barton Keyes (played by Edward G. Robinson).
> 
> "Double Indemnity" is considered a classic example of film noir for its shadowy cinematography, morally ambiguous characters, and themes of betrayal, greed, and lust. The film was nominated for several Academy Awards and has stood the test of time as a landmark in the film noir genre.

For more information and examples incorporating Antropic Claude, visit the integration guide

Steps

  • script

    // default inputs
    const model = AnthropicAI.knownModels()[0] // haiku
    const maxTokens = 1024
    const chatResponsePrefix = "> "
    const ignoreLinePrefixes = ["#", "--", "//"] // skip lines with these prefixes
    const initialText = editor.getText().trim()
    
    // splits the text into messages for OpenAI's API
    function parseChat(s) {
    	let messages = []
    	let current = []
    	let lastRole = ""
    	let currentRole = "user"
    	
    	for (let line of s.split("\n")) {
    		let ln = line
    		// skip empty lines
    		if (ln.length == 0) { continue }
    		// skip ignore prefixes
    		if (ignoreLinePrefixes.some(s => ln.startsWith(s))) { continue }
    
    		if (ln.startsWith(chatResponsePrefix)) { // assistant text
    			currentRole = "assistant"
    			ln = ln.slice(chatResponsePrefix.length).trim()
    		}
    		else {
    			currentRole = "user"
    			ln = ln.trim()
    		}
    		if (ln.length == 0) { continue }		
    	
    		if (currentRole != lastRole) {
    			if (current.length > 0) {
    				messages.push({
    					"role": lastRole,
    					"content": current.join("\n")
    				})
    			}
    			current = []
    			lastRole = currentRole
    		}		
    		current.push(ln)
    	}
    	if (current.length > 0) {
    		messages.push({
    			"role": lastRole,
    			"content": current.join("\n")
    		})
    	}
    	alert(JSON.stringify(messages, null, 2))
    	return messages
    }
    
    let f = () => {
    	let messages = parseChat(initialText)
    	if (messages.length == 0) {
    		alert("No input. Please type some text to submit in the editor.")
    		return false
    	}
    
    	let ai = new AnthropicAI()
    	let response = ai.request({  
            "path": "/messages",  
            "method": "POST",  
            "data": {  
                "model": model,
                "max_tokens": maxTokens,
                "messages": messages
            }  
        })
    	if (!response.success) {
    		console.log(`ERROR: ${response.statusCode}
    ${response.responseText}`)
    		context.fail()
    		return false
    	}
    
    	let newMessage = response.responseData.content[0].text
    	let newContent = `${initialText}
    	
    ${newMessage.split("\n").map(ln => `${chatResponsePrefix}${ln}`).join("\n")}
    
    `
    	editor.setText(newContent)
    	editor.activate()
    	editor.setSelectedRange(100000, 0) // move cursor to end
    	return true
    }
    
    if (!f()) {
    	context.cancel()
    }
    
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.